Skip to content

Handle SIGINT when running "up" command to shutdown gracefully #1148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions newsfragments/sigint-up.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed handling SIGINT when running "up" command to shutdown gracefully
15 changes: 14 additions & 1 deletion podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down