Skip to content

master process running in container doesn't get SIGTERM on stop with lxc #2436

Closed

Description

@SamSaffron

running:

Linux ubuntu 3.11.0-12-generic #19-Ubuntu SMP Wed Oct 9 16:20:46 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

(ubuntu latest)

root@ubuntu:/home/sam# lxc-version 
lxc version: 1.0.0.alpha1

run: docker run -i -t ubuntu /bin/bash

attempt to stop the container:

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

Activity

  1. shykes commented on Oct 29, 2013

    @shykes
    Contributor

    cc @crosbymichael @vieux have you seen this on lxc versions other than 1.0.0?

  2. SvenDowideit commented on Oct 29, 2013

    @SvenDowideit
    Contributor

    I'm running 1.0.0.alpha1 and can confirm the same issue

    docker run -t -i ubuntu /bin/bash -c "trap 'echo term ; exit' SIGTERM ; trap 'echo kill ; exit' SIGKILL; echo waiting ;  read"
    

    kill bash-pid works well

    the tests all pass, so the CmdKill / CmdStop tests need to check to see how the process exited, not just that they did.

  3. crosbymichael commented on Oct 29, 2013

    @crosbymichael
    Contributor

    No but I have seen a few other bug reports from ppl running newer lxc versions. ( interop issues with docker )

    Michael Crosby

    On Oct 28, 2013, at 10:49 PM, Solomon Hykes [email protected] wrote:

    cc @crosbymichael @vieux have you seen this on lxc versions other than 1.0.0?

    —
    Reply to this email directly or view it on GitHub.

  4. kiorky commented on Oct 29, 2013

    @kiorky
    Contributor

    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.

  5. SamSaffron commented on Oct 30, 2013

    @SamSaffron
    Author

    @kiorky I just compiled from git and am still getting the same issue

  6. kiorky commented on Dec 8, 2013

    @kiorky
    Contributor

    This may fix #1960 & is related to #2276

  7. crosbymichael commented on Mar 6, 2014

    @crosbymichael
    Contributor

    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.

  8. removed this from the 0.7.1 milestone on May 15, 2014
  9. mpetazzoni commented on Jul 17, 2014

    @mpetazzoni

    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:

    #!/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:

    $ python test.py
    SLEEPING
    ^Z
    $ kill -TERM %1
    $ GOT SIGNAL 15
    $
    

    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
    
  10. lcdavidson commented on Jul 18, 2014

    @lcdavidson

    Python 3 /sbin/my_init doesn't seem to shutdown child processes either within the container.

  11. mpaolini commented on Jul 21, 2014

    @mpaolini

    confirmed also here. docker stop does not deliver SIGTERM to circusd running inside the container

  12. kuon commented on Jul 24, 2014

    @kuon
    Contributor

    I am having the same issue:

    root@maze ~ # docker version
    Client version: 1.1.2
    Client API version: 1.13
    Go version (client): go1.2.1
    Git commit (client): d84a070
    Server version: 1.1.2
    Server API version: 1.13
    Go version (server): go1.2.1
    Git commit (server): d84a070
    root@maze ~ # docker info
    Containers: 9
    Images: 337
    Storage Driver: aufs
     Root Dir: /var/lib/docker/aufs
     Dirs: 355
    Execution Driver: native-0.2
    Kernel Version: 3.13.0-32-generic
    WARNING: No swap limit support
    root@maze ~ # uname -a
    Linux maze 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
    
  13. lorenz commented on Jul 25, 2014

    @lorenz

    Any progress on this? It' really bad for stuff like database servers, which just get killed without any chance to save the data.

  14. 11 remaining items

  15. crosbymichael commented on Jul 25, 2014

    @crosbymichael
    Contributor

    ping @dineshs-altiscale

    This is an LXC only issue, care to take care of this now that you are the maintainer? ;)

    Thanks!

  16. crosbymichael commented on Jul 27, 2014

    @crosbymichael
    Contributor
  17. 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
  18. k4nar commented on Oct 29, 2014

    @k4nar

    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.

  19. SvenDowideit commented on Oct 30, 2014

    @SvenDowideit
    Contributor

    @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)

  20. bercab commented on Nov 19, 2014

    @bercab

    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.

    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):

    CMD /bin/start.sh
    

    If you do a ps axu inside container:

    root@2ba4910736e4:/# ps axu
    USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root         1  0.2  0.0   4444   652 ?        Ss   16:26   0:00 /bin/sh -c /bin/start.sh
    root         8  3.5  0.4 195604 17312 ?        Sl   16:26   0:00 /usr/bin/python /usr/local/bin/cir
    

    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:

    docker run -d  apsl/circusbase /bin/start.sh
    

    Or changing CMD in your Dockerfile:

    CMD ["/bin/start.sh"]
    

    Then ps axu inside container shows cirusd process with PID 1. And now docker stops works well and receives QUIT/TERM signals.

    root@b3fdd509c577:/# ps axu
    USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root         1  0.9  0.4 195604 17312 ?        Ssl  16:25   0:00 /usr/bin/python /usr/local/bin/cir
    

    Hope this helps.

  21. k4nar commented on Nov 19, 2014

    @k4nar

    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.sh Circus do get killed killed right away, but for example docker run -it python:3.4 doesn't get killed by the SIGTERM.

  22. bercab commented on Nov 19, 2014

    @bercab

    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.

  23. jessfraz commented on Feb 26, 2015

    @jessfraz
    Contributor

    ping @ashahab-altiscale is this still an issue

    can anyone here reproduce with latest docker and lxc 1.0.7

  24. skozin commented on Feb 26, 2015

    @skozin

    @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.

  25. jessfraz commented on Mar 2, 2015

    @jessfraz
    Contributor

    i cannot reproduce with lxc 1.0.7 and latest docker

  26. added a commit that references this issue on Sep 6, 2017
  27. added a commit that references this issue on May 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions