Skip to content

Fix #10661: docker compose up always kills the containers on second Ctrl-C#11718

Merged
ndeloof merged 4 commits intodocker:mainfrom
jsoriano:kill-stopped-containers
Apr 14, 2024
Merged

Fix #10661: docker compose up always kills the containers on second Ctrl-C#11718
ndeloof merged 4 commits intodocker:mainfrom
jsoriano:kill-stopped-containers

Conversation

@jsoriano
Copy link
Contributor

@jsoriano jsoriano commented Apr 11, 2024

What I did

Hitting Ctrl-C twice in docker compose up is supposed to immediately kill the containers.
Kill executed on the second Ctrl-C was filtering containers depending on its state. In some
cases the containers can reach an state for what these filters don't get any container, and
the command hangs while printing no container to kill.

Remove this filtering, so it tries to kill any container it finds, independently of their state.

Related issue

Fixes #10661

Kill executed on the second Ctrl-C of docker compose up  was
filtering containers depending on its state. In some cases the
containers can reach an state for what these filters don't get
any container, and the command keeps reporting `no container to
kill`.

Remove this filtering, so it tries to kill any container it
finds, independently of its state.

Fixes docker#10661

Signed-off-by: Jaime Soriano Pastor <jaime.soriano@elastic.co>
@ndeloof
Copy link
Contributor

ndeloof commented Apr 12, 2024

AFAICT kill on a stopped container will fail

Error response from daemon: cannot kill container: dafcbcd023c1: container dafcbcd023c1c1bbed6b0a35a7e1e2420129672c327519698ef8cbd644fefd06 is not running

would need to ignore such an error, assuming this is the right fix for this race condition

@jsoriano
Copy link
Contributor Author

AFAICT kill on a stopped container will fail

Error response from daemon: cannot kill container: dafcbcd023c1: container dafcbcd023c1c1bbed6b0a35a7e1e2420129672c327519698ef8cbd644fefd06 is not running

You are right, this was briefly showing an error. I have changed the code to use the internal kill method, that doesn't show the progress. And to ignore the returned error.

Signed-off-by: Jaime Soriano Pastor <jaime.soriano@elastic.co>
@jsoriano jsoriano force-pushed the kill-stopped-containers branch from bb43c4a to 33bf583 Compare April 12, 2024 14:56
return s.Kill(context.Background(), project.Name, api.KillOptions{
// Intentionally ignore errors, for cases where some
// of the containers are already stopped.
s.kill(context.Background(), project.Name, api.KillOptions{
Copy link
Contributor

Choose a reason for hiding this comment

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

this should not ignore all errors, but only the "not found" one

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't find a reliable way to distinguish errors here, as they don't seem to be typed. Should I add the check based on the text of the error? I guess it would be to check if the error message contains is not running.

Also, with the current code, even if it returns the error, it doesn't seem to be doing anything about it. If the kill fails, the loop is still finished and the command hangs if there are still containers running. I could try to fix this too if I add error handling.

Copy link
Contributor

Choose a reason for hiding this comment

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

have you tried using errdefs.IsNotFound(err) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! This worked. I also checked errdefs.IsConflict, which happens if one of the containers is already stopped.

Project: project,
})
})
return nil
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am removing this return, so it is possible now to try to kill the containers again after one kill failed.

Copy link
Contributor

Choose a reason for hiding this comment

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

This should not be necessary, I hardly can imagine process could survive a SIGKILL
Also I'm not sure about the potential side-effects doing so, so if you don't mind I'd prefer to be conservative here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, indeed hopefully no process is going to survive a SIGKILL 🙂 But this is sending requests to kill containers to the docker daemon through HTTP, so there are other things that could fail in the way.

In any case I agree with being conservative and keep previous behaviour, I will revert this.

Signed-off-by: Jaime Soriano Pastor <jaime.soriano@elastic.co>
@jsoriano jsoriano force-pushed the kill-stopped-containers branch from f09eb66 to e22fe02 Compare April 12, 2024 17:42
Signed-off-by: Jaime Soriano Pastor <jaime.soriano@elastic.co>
@ndeloof ndeloof merged commit 7d36164 into docker:main Apr 14, 2024
@jsoriano
Copy link
Contributor Author

@ndeloof thanks for the reviews and the help on this change. Could you also take a look to #11719? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] docker compose up not returning shell and erroneously detecting error

2 participants