Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed `--abort-on-container-exit` and `--exit-code-from` so that remaining containers are explicitly stopped when one container exits, instead of being left running in the background.
9 changes: 8 additions & 1 deletion podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -1936,6 +1936,7 @@ async def run( # pylint: disable=dangerous-default-value
log_formatter: str | None = None,
*,
suppress_output: bool = False,
container_name: str | None = None,
# Intentionally mutable default argument to hold references to tasks
task_reference: set[asyncio.Task] = set(),
) -> int | None:
Expand Down Expand Up @@ -1988,6 +1989,12 @@ async def run( # pylint: disable=dangerous-default-value
except asyncio.CancelledError:
log.info("Sending termination signal")
p.terminate()
if container_name:
log.info("Stopping container %s", container_name)
try:
await self.run([], "stop", ["-t", "10", container_name])
except Exception as e: # pylint: disable=broad-exception-caught
log.warning("Error stopping container %s: %s", container_name, e)
try:
exit_code = await wait_with_timeout(p.wait(), 10)
except TimeoutError:
Expand Down Expand Up @@ -3893,7 +3900,7 @@ async def run_container(
# start the container
log.debug("Starting task for container %s", name)
return await compose.podman.run( # type: ignore[misc]
*command, log_formatter=log_formatter, suppress_output=suppress_output
*command, log_formatter=log_formatter, suppress_output=suppress_output, container_name=name
)


Expand Down
40 changes: 40 additions & 0 deletions tests/integration/exit_from/test_podman_compose_exit_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,43 @@ def test_podman_compose_exit_from(self) -> None:

self.run_subprocess_assert_returncode(up_cmd + ["--exit-code-from", "sh1"], 1)
self.run_subprocess_assert_returncode(up_cmd + ["--exit-code-from", "sh2"], 2)

def test_abort_all_containers_on_container_exit(self) -> None:
try:
out, err, returncode = self.run_subprocess(
[
podman_compose_path(),
"-f",
compose_yaml_path(),
"up",
"--abort-on-container-exit",
],
timeout=30,
)
# After one container exits, the others should be stopped.
# The command should complete within the timeout (all containers stop).
self.assertNotEqual(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use assertEqual

returncode, -9, "Command was killed by timeout, containers were not stopped"
)

# Verify all containers are stopped
out2, _, _ = self.run_subprocess([

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better count running containers, e.g. using compose ps

"podman",
"ps",
"-a",
"--filter",
"label=io.podman.compose.project=exit_from",
"--format",
"{{.Names}} {{.Status}}",
])
self.assertEqual(
out2.decode("utf-8").strip().count("Exited"),
2,
)
finally:
self.run_subprocess_assert_returncode([
podman_compose_path(),
"-f",
compose_yaml_path(),
"down",
])