Merge pull request #1148 from mokazemi/fix/sigint-down

Handle SIGINT when running "up" command to shutdown gracefully
This commit is contained in:
Povilas Kanapickas 2025-03-20 00:14:11 +02:00 committed by GitHub
commit 0cf1378cb5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 2 deletions

View File

@ -1,7 +1,7 @@
[MESSAGES CONTROL]
# C0111 missing-docstring: missing-class-docstring, missing-function-docstring, missing-method-docstring, missing-module-docstrin
# consider-using-with: we need it for color formatter pipe
disable=too-many-lines,too-many-branches,too-many-locals,too-many-statements,too-many-arguments,too-many-instance-attributes,fixme,multiple-statements,missing-docstring,line-too-long,consider-using-f-string,consider-using-with,unnecessary-lambda-assignment
disable=too-many-lines,too-many-branches,too-many-locals,too-many-statements,too-many-arguments,too-many-instance-attributes,fixme,multiple-statements,missing-docstring,line-too-long,consider-using-f-string,consider-using-with,unnecessary-lambda-assignment,broad-exception-caught
# allow _ for ignored variables
# allow generic names like a,b,c and i,j,k,l,m,n and x,y,z
# allow k,v for key/value

View File

@ -0,0 +1 @@
- Fixed handling SIGINT when running "up" command to shutdown gracefully

View File

@ -2778,9 +2778,22 @@ async def compose_up(compose: PodmanCompose, args):
max_service_length = curr_length if curr_length > max_service_length else max_service_length
tasks = set()
async def handle_sigint():
log.info("Caught SIGINT or Ctrl+C, shutting down...")
try:
log.info("Shutting down gracefully, please wait...")
down_args = argparse.Namespace(**dict(args.__dict__, volumes=False))
await compose.commands["down"](compose, down_args)
except Exception as e:
log.error("Error during shutdown: %s", e)
finally:
for task in tasks:
task.cancel()
if sys.platform != 'win32':
loop = asyncio.get_event_loop()
loop.add_signal_handler(signal.SIGINT, lambda: [t.cancel("User exit") for t in tasks])
loop.add_signal_handler(signal.SIGINT, lambda: asyncio.create_task(handle_sigint()))
for i, cnt in enumerate(compose.containers):
# Add colored service prefix to output by piping output through sed