Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support running as PID 1 #140

Closed
Marwes opened this issue Nov 7, 2019 · 12 comments · Fixed by #497
Closed

Support running as PID 1 #140

Marwes opened this issue Nov 7, 2019 · 12 comments · Fixed by #497
Labels
has workaround Bug has a workaround, so it might not need urgent attention improvement Making an existing feature better, rather than a new one OS: Linux Affects Linux specifically

Comments

@Marwes
Copy link

Marwes commented Nov 7, 2019

When running watchexec (really, cargo-watch) in a docker container I would like that the watchexec process shutsdown when I request that the docker container shutsdown.

Currently SIGTERM is handled and sent to the child process which shutsdown my program as expected, but after that nothing happens. What I would like is that watchexec, after seeing the child process shutdown also shutsdown itself.

@passcod
Copy link
Member

passcod commented Nov 7, 2019

That sounds reasonable

@passcod passcod added improvement Making an existing feature better, rather than a new one need repro Needs a reproduction and some investigation labels Nov 7, 2019
@passcod
Copy link
Member

passcod commented Nov 7, 2019

Actually I can't reproduce this. Sending SIGTERM to watchexec kills it (and the child). Can you provide more detail as to how and what you're running?

@Marwes
Copy link
Author

Marwes commented Nov 7, 2019

Right, forgot that detail. It only seems to happen when running it as the main process in docker (PID 1) which has some special behaviour around signals.

To reproduce

Dockerfile

FROM rust:1.38

RUN cargo install watchexec

COPY test.sh ./

test.sh

trap 'echo "Received SIGTERM" && exit 1' TERM
while true; do sleep 1; done

Build and run docker

docker build . -t test
docker run -it --rm --name test test watchexec --verbose 'sh ./test.sh'

With the container running docker kill -s SIGTERM test can be ran after which watchexec says that it received the signal.

Remove the watchexec invocation and run the bare sh ./test.sh to sanity check that the script exits on SIGTERM.

@passcod
Copy link
Member

passcod commented Nov 11, 2019

(Sorry for taking so long, I am looking at this and haven't forgotten.)

@passcod passcod added help wanted Help is wanted everywhere, but here in particular OS: Linux Affects Linux specifically and removed need repro Needs a reproduction and some investigation labels Nov 18, 2019
@passcod
Copy link
Member

passcod commented Nov 18, 2019

Tried fixing this for a few days but ran into issues and it's delaying release, so I'm shelving this for now. Not happy about it, but I have to get other stuff out of the door before coming back to it.

@LucasPickering
Copy link

@passcod I'm still running into this issue, did you ever get a chance to do more debugging? Is there anything I can do to help with the debugging?

@passcod
Copy link
Member

passcod commented Nov 5, 2020

Haven't done anything more since. Workaround is obviously to not run as PID 1 (not hard, just wrap it in a shell), but I haven't looked deeper at how the way signals work for PID 1 affects its handling in watchexec.

It's been a year and I've forgotten things (also I'm busy, november is basically the worst time as I do nanowrimo); if you want to help better repro it from scratch and look at the handling code in signal.rs.

@LucasPickering
Copy link

ok thanks, I will use an init process in the mean time and I'll try to do some debugging next week

@passcod passcod changed the title watchexec should shutdown on SIGTERM Support running as PID 1 Apr 11, 2021
@passcod passcod removed the help wanted Help is wanted everywhere, but here in particular label Apr 11, 2021
@passcod
Copy link
Member

passcod commented Aug 4, 2021

Dropping a note for future visitors that for the specific case of Docker, running with the --init flag solves this without needing your own init/shell/wrapper. It would still be nice to properly support being PID1 at some point though.

@tgrondier
Copy link

If you're using watchexec as a sidecar in a kubernetes pod, a workaround for that (but only if your scripts can handle ungraceful stops), a workaround is to add the following to your watchexec container Dockerfile:

STOPSIGNAL SIGKILL

@passcod passcod added the has workaround Bug has a workaround, so it might not need urgent attention label Jan 26, 2022
@matu3ba
Copy link

matu3ba commented Aug 23, 2022

@Marwes pid 1 ignores most signaling to not "accidentially shutting down the system", because child processes may use pid 1 as process group to signal the other children.

Thus SIGKILL should be expected. Can you confirm that SIGKILL works? If yes, then please close this issue.

@passcod
Copy link
Member

passcod commented Feb 14, 2023

This will be in 1.21.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has workaround Bug has a workaround, so it might not need urgent attention improvement Making an existing feature better, rather than a new one OS: Linux Affects Linux specifically
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants