Skip to content

Fix signal forwarding issue - #3240

Closed
creack wants to merge 13 commits into
moby:masterfrom
creack:kill_issue
Closed

creack wants to merge 13 commits into
moby:masterfrom
creack:kill_issue

Conversation

@creack

@creack creack commented Dec 17, 2013

Copy link
Copy Markdown
Contributor

The issue is that we can't kill pid 1. By default, now dockerinit forkexec instead of exec. Resulting in the process started to be pid 2.

The previous behavior is still available via the '-fork' option of docker run.

Note that disabling fork will result in uninterruptible process.

I also noticed that this PR make some races within the test more obvious. I'll work on it, but it is a separate issue.

Comment thread sysinit/sysinit.go

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If our "pid 1" can't receive any signals, why even bother with this signal proxying?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Well, this is the trick, it does receive the signals when it has a child. Maybe if we put the child in its own sid this won't be necessary?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hrm, indeed - that sounds more like an issue related to our syscall.Exec madness than to being "pid 1" to me, but I'm only spit-balling of course. Processes and signals are tricky, and namespaces only make them more so. :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is indeed likely. It would be interesting to see the internals for signal handling. It is possible that the Go runtime catches all signal before 'giving' them to the process. If so, then yes, the Exec would override the go's internal signal handling without restoring ISIG.

@vieux

vieux commented Dec 18, 2013

Copy link
Copy Markdown
Contributor

I see multiple tests failures.

--- FAIL: TestRunDisconnectTty (10.04 seconds)
    commands_test.go:89: Waiting for the container to be started timed out
--- FAIL: TestRunDetach (10.05 seconds)
    commands_test.go:89: Waiting for the container to be started timed out
--- FAIL: TestAttachDetach (10.05 seconds)
    commands_test.go:89: Waiting for the container to be started timed out
--- FAIL: TestAttachDetachTruncatedID (10.34 seconds)
    commands_test.go:89: Waiting for the container to be started timed out
--- FAIL: TestAttachDisconnect (10.06 seconds)

@vieux

vieux commented Dec 18, 2013

Copy link
Copy Markdown
Contributor

An the tests seems to never finish

@creack

creack commented Dec 18, 2013

Copy link
Copy Markdown
Contributor Author

@vieux, Try again, it appens some time, but it is not linked to the PR. It just make the race a bit more obvious.
The tests never finished on failure is a known issue also not linked to the PR.

@vieux

vieux commented Dec 18, 2013

Copy link
Copy Markdown
Contributor

LGTM

@gabrtv

gabrtv commented Dec 18, 2013

Copy link
Copy Markdown

+1. Tested SIGSTP and SIGINT. This makes working on containers in the foreground much easier.

@codeaholics

Copy link
Copy Markdown
Contributor

Do you think this might help with #2878? If so, I can test it tomorrow for you.

@jpoimboe

Copy link
Copy Markdown
Contributor

FYI I did something very similar in my long-running dockerinit branch.

Also, if dockerinit is pid 1 then it will be responsible for reaping zombies, otherwise you can get a lot of defunct processes in the container. See jpoimboe/docker@affa9a0.

@creack

creack commented Dec 20, 2013

Copy link
Copy Markdown
Contributor Author

thx @jpoimboe

@shykes

shykes commented Dec 20, 2013

Copy link
Copy Markdown
Contributor

I would like to avoid running the application as pid!=1 if possible, because it breaks "docker run /sbin/init" and forces us to make a special case.

I don't like this "-fork" flag, what's the point of it for the user?

On Thu, Dec 19, 2013 at 11:59 PM, Guillaume J. Charmes
[email protected] wrote:

thx @jpoimboe

Reply to this email directly or view it on GitHub:
#3240 (comment)

@creack

creack commented Dec 20, 2013

Copy link
Copy Markdown
Contributor Author

The only motivation behing the -fork is /sbin/init. pid != 1 breaks it and -fork allows for people to make it work.

@shykes

shykes commented Dec 20, 2013

Copy link
Copy Markdown
Contributor

Ok. So if we make it so pid==1 there's no more need for that flag. Can't we simply exec() without forking?

On Fri, Dec 20, 2013 at 7:46 AM, Guillaume J. Charmes
[email protected] wrote:

The only motivation behing the -fork is /sbin/init. pid != 1 breaks it and -fork allows for people to make it work.

Reply to this email directly or view it on GitHub:
#3240 (comment)

@creack

creack commented Dec 20, 2013

Copy link
Copy Markdown
Contributor Author

Well, this was a suggestion on how to fix the signal issue. I can try to find an other one.
It had the advantage to go in the direction of long running docker init, solves the signal issue, open a window for proper pty management.
For people needing pid == 1, there is the -fork flag.

@jpoimboe

Copy link
Copy Markdown
Contributor

From a Linux kernel standpoint, pid 1 (whether real or namespaced) will see a SIGTERM, so it can register a SIGTERM handler if it wants. However, pid 1 also has flag SIGNAL_UNKILLABLE which means that a SIGTERM won't kill it, even if the signal came from the parent namespace.

So if dockerinit execs the app as pid 1, there's no way to kill it with a SIGTERM, unless the app happens to have its own SIGTERM handler (which would be unusual).

@jpoimboe

Copy link
Copy Markdown
Contributor

BTW an alternative to the "-fork" flag would be to only exec if the app name is "/sbin/init" or "systemd", which is what I did in my long-running dockerinit branch.

@shykes

shykes commented Dec 20, 2013

Copy link
Copy Markdown
Contributor

I see the problem now. I didn't realize the rules for signals to pid=1 applied to namespaced processes too, but it makes sense.

Is there any way for the app to inherit handlers setup by dockerinit before exec? As I write this it seems highly doubtful (where would the handlers even live in memory after exec?) but worth asking :)

By the way this is the first sensible argument I've seen in favor of long-running init.

On Fri, Dec 20, 2013 at 9:06 AM, Josh Poimboeuf [email protected]
wrote:

BTW an alternative to the "-fork" flag would be to only fork if the app name is "/sbin/init" or "systemd", which is what I did in my long-running dockerinit branch.

Reply to this email directly or view it on GitHub:
#3240 (comment)

@jpoimboe

Copy link
Copy Markdown
Contributor

No, all signal handlers are wiped out by exec.

I hadn't known about this issue before, but yeah this definitely adds to the argument for a long-running dockerinit.

@creack

creack commented Dec 20, 2013

Copy link
Copy Markdown
Contributor Author

@shykes I'll make some test with "raw" signal handling in CGO to see if I can manage something. However, as you mentioned it is doubtful.
An other argument in favor of long-running init is the PTY management. In order for the PTY to be created within the namespace (without forking os/exec), we need to have a long-running dockerinit.

@jpoimboe

Copy link
Copy Markdown
Contributor

From "man 2 execve":

The dispositions of any signals that are being caught are reset to the default (signal(7)).

@creack

creack commented Dec 20, 2013

Copy link
Copy Markdown
Contributor Author

thanks @jpoimboe for the infos.

@shykes

shykes commented Dec 20, 2013

Copy link
Copy Markdown
Contributor

On Fri, Dec 20, 2013 at 10:04 AM, Guillaume J. Charmes <
[email protected]> wrote:

@shykes https://github.com/shykes I'll make some test with "raw" signal
handling in CGO to see if I can manage something. However, as you mentioned
it is doubtful.
An other argument in favor of long-running init is the PTY management. In
order for the PTY to be created within the namespace (without forking
os/exec), we need to have a long-running dockerinit.

But this is not a real system limitation: docker could create the pty after
forking/namespacing but before exec-ing. Unlike signal handlers, file
descriptors can be inherited.

My understanding is that you're talking about a limitation in the language
runtime (difficult for Golang to reliably clone() without execing, etc
etc). I think that's a secondary concern, there are ways around that that
don't require a long-running dockerinit.

@shykes

shykes commented Dec 20, 2013

Copy link
Copy Markdown
Contributor

@creack don't waste your time for the signal thing, this seems pretty
certain and obvious in retrospect. CGO or not, if pid=1 can't be killed
without explicit signal handlers, then that's that.

On Fri, Dec 20, 2013 at 11:34 AM, Solomon Hykes [email protected] wrote:

On Fri, Dec 20, 2013 at 10:04 AM, Guillaume J. Charmes <
[email protected]> wrote:

@shykes https://github.com/shykes I'll make some test with "raw"
signal handling in CGO to see if I can manage something. However, as you
mentioned it is doubtful.
An other argument in favor of long-running init is the PTY management. In
order for the PTY to be created within the namespace (without forking
os/exec), we need to have a long-running dockerinit.

But this is not a real system limitation: docker could create the pty
after forking/namespacing but before exec-ing. Unlike signal handlers, file
descriptors can be inherited.

My understanding is that you're talking about a limitation in the language
runtime (difficult for Golang to reliably clone() without execing, etc
etc). I think that's a secondary concern, there are ways around that that
don't require a long-running dockerinit.

@creack

creack commented Dec 20, 2013

Copy link
Copy Markdown
Contributor Author

@shykes indeed, that's why I say 'without forking os/exec' in the sense of code fork. We'd need to reimplement a lot from it in order to do this.

@crosbymichael

Copy link
Copy Markdown
Contributor

ping @creack can we close this?

@mschulkind

Copy link
Copy Markdown

I'm not really sure what the fix is other than the proposed init process, but I think this needs a fix.

My current use case is that I'm trying to run a docker registry under foreman for development purposes. I can't seem to figure out any way to get the registry to shutdown when I ctrl-c docker without an explicit 'docker stop' after. That is, nothing short of modifying the docker image to wrap everything in something that responds to SIGTERM. It's not super hard, something like 'CMD trap exit TERM; & wait' works, but that's not terribly user friendly.

@anacrolix

Copy link
Copy Markdown

Why is this closed? It doesn't appear to be fixed.

@phemmer

phemmer commented Sep 21, 2015

Copy link
Copy Markdown
Contributor

@anacrolix This is a pull request. Issues are tracked separately. See #3793

@rafaelcapucho

Copy link
Copy Markdown

why this PR is not accepted yet? we need a definitive way to solve it

https://issues.apache.org/jira/browse/MESOS-3829

@giuffresoft79

Copy link
Copy Markdown

Hello, i have this problem with SIGTERM....

@cpuguy83

Copy link
Copy Markdown
Member

If you're apps aren't receiving signals, it's probably because the app has been inited improperly OR it drops default signal handlers when run as PID 1.

@LantosIstvan

Copy link
Copy Markdown

My solution for the PID1 problem:

The Dockerfile ends with:

ENTRYPOINT ["/bin/bash", "-c"]

Than I run with this shell script, which has the filename of node and have chmod +x:

#!/bin/bash

docker run --rm -it -p 8083:80 -v $HOME/node/work/:/root/node/:rw node_node "echo pid1 > /dev/null && node $@"

The trick is "echo pid1 > /dev/null && node $@" which is the command. $@ is a shell script to accept user input from command line for example: ./node -v which will return the version of Node.js inside the running container.

So echo will trap PID1 and sending output to /dev/null.

Here is my Dockerised Node.js development environment: https://github.com/DJviolin/Node.js-Development-Environment

@tobilg

tobilg commented Feb 5, 2016

Copy link
Copy Markdown

@rafaelcapucho This problem still seems to exist in current Mesos versions, together with Docker 1.8 IMHO...

Is there any suggested fix/workaround?

cegeka-jenkins pushed a commit to cegeka/puppet-docker that referenced this pull request Oct 23, 2017
The previous upstart method did not stop containers when the upstart service was stopped. This is a result of moby/moby#3240

As a workaround for the issue, on start we write the container ID to a file, and then use a post-stop script to kill that container.
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.