You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
master process running in container doesn't get SIGTERM on stop with lxc #2436
container fails to stop properly and reverts to kill
2013/10/29 16:24:47 error killing container 9d01aa5543a8df028df004c440240e72080b17eada1bf0cd9c5a092ae61139a4 (lxc-kill: failed to get the init pid
, exit status 255)
2013/10/29 16:24:57 Container 9d01aa5543a8df028df004c440240e72080b17eada1bf0cd9c5a092ae61139a4 failed to exit within 10 seconds of lxc SIGKILL - trying direct SIGKILL
I'd like to bring this issue up for attention, with a test case (/cc @jpetazzo). As this issue says, it seems like the process running inside the container isn't getting the SIGTERM sent by Docker.
I have the following Python script:
#!/usr/bin/env python
from __future__ import print_function
import signal
import subprocess
import sys
import time
def handler(signum, frame):
print('GOT SIGNAL {}'.format(signum))
sys.exit(1)
signal.signal(signal.SIGTERM, handler)
print('SLEEPING')
time.sleep(1000)
print('DONE')
The expected behavior is SLEEPING, followed by a 1000s pause (a long long time), followed by either one of DONE or GOT SIGNAL 15 if SIGTERM was received.
Running the script outside of any container, we get the correct behavior:
I created an image, available on the public registry as mpetazzoni/sigterm-test, that includes this script and simply executes it when started:
FROM base
RUN apt-get -y install python
ADD test.py /
CMD ["python", "test.py"]
Starting the container, but killing the process manually as a host process, we get the correct behavior:
$ docker run -i -t mpetazzoni/sigterm-test
SLEEPING
> In another term:
> $ sudo kill -TERM <pid of `python test.py`>
GOT SIGNAL 15
$
But using docker stop or docker kill -TERM both result in the incorrect behavior of the process being killed, but without having the chance to catch the SIGTERM signal:
$ docker run -i -t --name=test mpetazzoni/sigterm-test
SLEEPING
> In another term:
> $ docker stop test
$
The Docker daemon log simply shows:
2014/07/17 22:55:30 POST /v1.13/containers/test/stop?t=10
[8912818a] +job stop(test)
[8912818a] -job attach(68976db5bca5f0ea7634b95004625a962a931cc5237e1b28372d2fc3f2170530) = OK (0)
[8912818a] +job release_interface(68976db5bca5f0ea7634b95004625a962a931cc5237e1b28372d2fc3f2170530)
[8912818a] -job release_interface(68976db5bca5f0ea7634b95004625a962a931cc5237e1b28372d2fc3f2170530) = OK (0)
[8912818a] -job stop(test) = OK (0)
2014/07/17 22:55:30 GET /v1.13/containers/68976db5bca5f0ea7634b95004625a962a931cc5237e1b28372d2fc3f2170530/json
[8912818a] +job container_inspect(68976db5bca5f0ea7634b95004625a962a931cc5237e1b28372d2fc3f2170530)
[8912818a] -job container_inspect(68976db5bca5f0ea7634b95004625a962a931cc5237e1b28372d2fc3f2170530) = OK (0)
For reference:
$ docker info
Containers: 25
Images: 298
Storage Driver: aufs
Root Dir: /var/lib/docker/aufs
Dirs: 348
Execution Driver: lxc-1.0.3
Kernel Version: 3.13.0-29-generic
Username: mpetazzoni
Registry: [https://index.docker.io/v1/]
WARNING: No swap limit support
$ docker version
Client version: 1.1.0
Client API version: 1.13
Go version (client): go1.2.1
Git commit (client): 79812e3
Server version: 1.1.0
Server API version: 1.13
Go version (server): go1.2.1
Git commit (server): 79812000
Reacted by CJ Keeney, Marcello and Miroslav Batchkarov
changed the title [-]Master process inside container not getting SIGTERM on docker stop[/-][+]master process running in container doesn't get SIGTERM on stop with lxc[/+]on Oct 4, 2014
I'm experiencing the exact same issue, but without LXC.
Containers: 27
Images: 341
Storage Driver: btrfs
Execution Driver: native-0.2
Kernel Version: 3.17.1-1-ARCH
Operating System: Arch Linux
WARNING: No swap limit support
Pretty much all my containers don't get killed immediately after running docker stop. Some of them run shell scripts, some of them run Python script. It doesn't change anything if I launch the command with sh -c in the container.
I ran into the same issue with circusd, and I think it has to do with CMD exec form
As stated on main docs, CMD can be called in three forms:
CMD ["executable","param1","param2"] (exec form, this is the preferred form)
CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
CMD command param1 param2 (shell form)
if CMD in your Dockerfile is called in shell form, your top process (PID 1) will be /bin/sh/ -c my_cmd
And I have seen that my_cmd does not receive the docker stop signal
So I think that's the solution, change from shell form to exec form. It works for me.
Hi Yannick,
Good job for your patch. circusd is amazing.
Circus handles the right way :), but yes, you have an issue if circus does not receive signal at all.
This does not happen if you avoid shell mode, like in docker dun -d apsl/circusbase /bin/start.sh. But if you execute default command, and CMD in Dockerfile is CMD /bin/start.sh, this gets executed in shell mode, and the result is:
/bin/sh -c /bin/start.sh
which executes with PID 1, and traps SIGTERM, which does not arrive to circusd when issuing docker stop
just try it, at least in docker >1.0 with lxc driver.
@jfrazelle, see nodejs/node-v0.x-archive#9131. Maybe that is another issue, but it prevents Node.js v0.11.16 and v0.12 from exiting on SIGTERM and SIGINT. Node tries to forward trapped signal to itself, but that fails because Node runs as PID 1 and cannot be terminated that way.
Activity
shykes commentedon Oct 29, 2013
cc @crosbymichael @vieux have you seen this on lxc versions other than 1.0.0?
SvenDowideit commentedon Oct 29, 2013
I'm running 1.0.0.alpha1 and can confirm the same issue
kill bash-pidworks wellthe tests all pass, so the CmdKill / CmdStop tests need to check to see how the process exited, not just that they did.
crosbymichael commentedon Oct 29, 2013
No but I have seen a few other bug reports from ppl running newer lxc versions. ( interop issues with docker )
Michael Crosby
kiorky commentedon Oct 29, 2013
mainstream lxc has fixed some bugs around that yes, and release in ubuntu saucy is scheduled (SRU process already in progress)
Your options now are so to test with lxc from git.
SamSaffron commentedon Oct 30, 2013
@kiorky I just compiled from git and am still getting the same issue
kiorky commentedon Dec 8, 2013
This may fix #1960 & is related to #2276
crosbymichael commentedon Mar 6, 2014
In general this is an issue with signals and PID 1. I'll try to find the other issue where this is discussed more in depth.
mpetazzoni commentedon Jul 17, 2014
Hi all,
I'd like to bring this issue up for attention, with a test case (/cc @jpetazzo). As this issue says, it seems like the process running inside the container isn't getting the SIGTERM sent by Docker.
I have the following Python script:
The expected behavior is
SLEEPING, followed by a 1000s pause (a long long time), followed by either one ofDONEorGOT SIGNAL 15if SIGTERM was received.Running the script outside of any container, we get the correct behavior:
I created an image, available on the public registry as
mpetazzoni/sigterm-test, that includes this script and simply executes it when started:Starting the container, but killing the process manually as a host process, we get the correct behavior:
But using
docker stopordocker kill -TERMboth result in the incorrect behavior of the process being killed, but without having the chance to catch the SIGTERM signal:The Docker daemon log simply shows:
For reference:
lcdavidson commentedon Jul 18, 2014
Python 3 /sbin/my_init doesn't seem to shutdown child processes either within the container.
mpaolini commentedon Jul 21, 2014
confirmed also here.
docker stopdoes not deliverSIGTERMtocircusdrunning inside the containerkuon commentedon Jul 24, 2014
I am having the same issue:
lorenz commentedon Jul 25, 2014
Any progress on this? It' really bad for stuff like database servers, which just get killed without any chance to save the data.
11 remaining items
crosbymichael commentedon Jul 25, 2014
ping @dineshs-altiscale
This is an LXC only issue, care to take care of this now that you are the maintainer? ;)
Thanks!
crosbymichael commentedon Jul 27, 2014
For a correct example when you are testing see:
https://github.com/crosbymichael/signaltest-docker
[-]Master process inside container not getting SIGTERM on docker stop[/-][+]master process running in container doesn't get SIGTERM on stop with lxc[/+]k4nar commentedon Oct 29, 2014
I'm experiencing the exact same issue, but without LXC.
Pretty much all my containers don't get killed immediately after running
docker stop. Some of them run shell scripts, some of them run Python script. It doesn't change anything if I launch the command withsh -cin the container.SvenDowideit commentedon Oct 30, 2014
@k4nar please start a new issue, yours isn't remotely related to lxc, and very few people are going to read a closed issue.
I'm going to guess however, that you'll be very interested in the documentation I wrote abour ENTRYPOINT - https://github.com/docker/docker/blob/master/docs/sources/reference/builder.md#entrypoint (not in the released docs yet)
bercab commentedon Nov 19, 2014
I ran into the same issue with circusd, and I think it has to do with CMD exec form
As stated on main docs, CMD can be called in three forms:
if CMD in your Dockerfile is called in shell form, your top process (PID 1) will be /bin/sh/ -c my_cmd
And I have seen that my_cmd does not receive the docker stop signal
So I think that's the solution, change from shell form to exec form. It works for me.
Example:
circusd is startef from a script /bin/start.sh, which does some stuff and then execs circusd.
https://registry.hub.docker.com/u/apsl/circusbase/dockerfile/
When using default CMD (shell mode):
If you do a ps axu inside container:
You can see /bin/start.sh is executed with /bin/sh -c, which has PID 1.
Also you can see circusd has PID 8, altrough it was called with exec on start.sh.
https://github.com/APSL/docker-circusbase/blob/master/start.sh
But when executed with exec mode:
Or changing CMD in your Dockerfile:
Then ps axu inside container shows cirusd process with PID 1. And now docker stops works well and receives QUIT/TERM signals.
Hope this helps.
k4nar commentedon Nov 19, 2014
You don't have any issue because Circus is explicitly handling SIGTERM: https://github.com/circus-tent/circus/blob/master/circus/sighandler.py (fun fact, I am the last contributor to this file :) ).
If I run
docker run -d apsl/circusbase /bin/start.shCircus do get killed killed right away, but for exampledocker run -it python:3.4doesn't get killed by the SIGTERM.bercab commentedon Nov 19, 2014
Hi Yannick,
Good job for your patch. circusd is amazing.
Circus handles the right way :), but yes, you have an issue if circus does not receive signal at all.
This does not happen if you avoid shell mode, like in
docker dun -d apsl/circusbase /bin/start.sh. But if you execute default command, and CMD in Dockerfile isCMD /bin/start.sh, this gets executed in shell mode, and the result is:which executes with PID 1, and traps SIGTERM, which does not arrive to circusd when issuing
docker stopjust try it, at least in docker >1.0 with lxc driver.
jessfraz commentedon Feb 26, 2015
ping @ashahab-altiscale is this still an issue
can anyone here reproduce with latest docker and lxc 1.0.7
skozin commentedon Feb 26, 2015
@jfrazelle, see nodejs/node-v0.x-archive#9131. Maybe that is another issue, but it prevents Node.js v0.11.16 and v0.12 from exiting on
SIGTERMandSIGINT. Node tries to forward trapped signal to itself, but that fails because Node runs as PID 1 and cannot be terminated that way.jessfraz commentedon Mar 2, 2015
i cannot reproduce with lxc 1.0.7 and latest docker
fix: remove CMD from Dockerfile
Merge pull request moby#2436 from arkodg/revert-syscall-reduction