Skip to content

Commit 0cf1378

Browse files
authored
Merge pull request #1148 from mokazemi/fix/sigint-down
Handle SIGINT when running "up" command to shutdown gracefully
2 parents 8b1bd01 + f5a6df6 commit 0cf1378

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

Diff for: .pylintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[MESSAGES CONTROL]
22
# C0111 missing-docstring: missing-class-docstring, missing-function-docstring, missing-method-docstring, missing-module-docstrin
33
# consider-using-with: we need it for color formatter pipe
4-
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
4+
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
55
# allow _ for ignored variables
66
# allow generic names like a,b,c and i,j,k,l,m,n and x,y,z
77
# allow k,v for key/value

Diff for: newsfragments/sigint-up.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fixed handling SIGINT when running "up" command to shutdown gracefully

Diff for: podman_compose.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -2778,9 +2778,22 @@ async def compose_up(compose: PodmanCompose, args):
27782778
max_service_length = curr_length if curr_length > max_service_length else max_service_length
27792779

27802780
tasks = set()
2781+
2782+
async def handle_sigint():
2783+
log.info("Caught SIGINT or Ctrl+C, shutting down...")
2784+
try:
2785+
log.info("Shutting down gracefully, please wait...")
2786+
down_args = argparse.Namespace(**dict(args.__dict__, volumes=False))
2787+
await compose.commands["down"](compose, down_args)
2788+
except Exception as e:
2789+
log.error("Error during shutdown: %s", e)
2790+
finally:
2791+
for task in tasks:
2792+
task.cancel()
2793+
27812794
if sys.platform != 'win32':
27822795
loop = asyncio.get_event_loop()
2783-
loop.add_signal_handler(signal.SIGINT, lambda: [t.cancel("User exit") for t in tasks])
2796+
loop.add_signal_handler(signal.SIGINT, lambda: asyncio.create_task(handle_sigint()))
27842797

27852798
for i, cnt in enumerate(compose.containers):
27862799
# Add colored service prefix to output by piping output through sed

0 commit comments

Comments
 (0)