Distro shut down even with running processes
Version
Microsoft Windows [Version 10.0.22621.521]
WSL Version
- [X] WSL 2
- [ ] WSL 1
Kernel Version
5.15.62.1
Distro Version
Ubuntu 20.04
Other Software
Docker Engine version 20.10.18
Repro Steps
- Create fresh Ubuntu 20.04 image
- Enable systemd
- Install Docker into Ubuntu (Ansible example)
- Run nginx in Docker (
docker run -d -p 80:80 nginx) - Verify nginx is running in your browser (http://localhost/)
- Exit the shell
Expected Behavior
Since nginx is running, the distro should not shut down.
Prior to upgrading to 0.67.6 w/ systemd support, this was the (presumably correct, but definitely desired) behavior.
Actual Behavior
After a short period of time, the distro is forcefully shut down by the system.
Logging back in, you can see it was forcefully shut down:
sh$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad20314283c5 nginx "/docker-entrypoint.…" 14 minutes ago Exited (255) 4 minutes ago 0.0.0.0:80->80/tcp, :::80->80/tcp admiring_bardeen
Diagnostic Logs
No response
Unfortunately, this is the new documented behavior, per blog post:
It is also important to note that with these change, systemd services will NOT keep your WSL instance alive.
You've currently got to have something running as a child of init - the Microsoft init, not pid 1 - to keep the distro running. Not my preference, but.
This is (fairly) explicitly stated in Craig's DevBlog announcement:
It is also important to note that with these change, systemd services will NOT keep your WSL instance alive. Your WSL instance will stay alive in the same way it did before, which you can read more about here.
That was actually surprising to me, as Windows 11's wsl.conf boot.command used to keep services running after exiting the shell. It was only in the Preview releases that it was changed to terminate when all processing started interactively had terminated. Hence I filed #8661, thinking this was a regression. Based on the text above, it seems that this might have been an intentional change.
Perhaps we should have a feature-request for a /etc/wsl.conf flag to allow the instance to continue running when any background task is still running.
Perhaps we should have a feature-request for a
/etc/wsl.confflag to allow the instance to continue running when any background task is still running.
Or just "until systemctl poweroff", which is pretty much how the above should behave when systemd support is enabled.
Fair points - I think an option to keep distros running (and not idle terminate them) is a good idea.
systemd services will NOT keep your WSL instance alive
Is a process launched by Docker considered to be a "systemd service"? Docker itself is, obviously, but things launched by Docker, I would've expected to keep the distro alive.
It seems to me like they implemented one thing (always shut it down when there's no interactive shell) but documented something else (don't let systemd services keep it alive).
(BTW, hearing the description of "we only stay alive for things rooted on Microsoft's init" helps me understand the exact implementation. It's unfortunate that the blog post described in a way that doesn't seems to match the actual implementation. Why not just describe it in the technically correct way?)
Fair points - I think an option to keep distros running (and not idle terminate them) is a good idea.
But what does this mean for systemd, then? I mean ps aux on a systemd-enabled distro has, as expected, a ton of stuff running. How would the proposed flag change the behavior? Would it basically always leave systemd distros always running, because of all those systemd processes? Or would the implementation better match the blog post description ("systemd services will NOT keep your WSL instance alive" but anything else will...?)
Fair points - I think an option to keep distros running (and not idle terminate them) is a good idea.
@benhillis
Just for clarity, it would be great if that option still allowed distros to terminate if no other processes were running, similar to the way it worked with the [boot].command section in the GA Windows 11 release. I'd only expect/want it to stay running if there's some background process, whether it was started through:
- Docker
- Systemd
/etc/wsl.conf[boot].command (#8661)- Etc.?
Basically, I think the flag would say "Work like it did in Windows 11 GA" ;-)
@bradwilson
It's unfortunate that the blog post described in a way that doesn't seems to match the actual implementation. Why not just describe it in the technically correct way?
I'm not sure they realized they had ever changed it to work any other way. The way I read the description, it seems Craig thought that was the way it always worked.
But what does this mean for systemd, then?
I would expect that any background process would count as a keep-alive mechanism. And yes, that's one of the unfortunate things about Systemd - It typically starts a lot. How would you tell the difference between the System dbus instance vs. Nginx, for instance?
Answering my own question above, I guess it could be a "whitelist process" implementation, where /etc/wsl.conf had a keep-alive (or something named better) option with a list of processes that would prevent the distro from self-terminating.
Just throwing it out there - I'm not tied to it by any means.
Basically, I think the flag would say "Work like it did in Windows 11 GA" ;-)
I have doubts about this being what I want, though I recognize that may be where this falls back to (see my question above about systemd services in general keeping things alive with this new proposed flag).
Having Docker keep the distro alive in Win11 GA is acceptable but not ideal. It's something that's I've come to accept, and I have quick commands to kill the things that are most likely to be "daemonized" in a pre-systemd world (notably, Docker and the GPG agent). In a world where we have systemd (and I have Docker's life cycle owned by systemd), I wouldn't want Docker itself to be something that would keep the distro alive, just the case of running Docker containers.
@bradwilson Yes, our replies are getting out of sync ;-) - I saw that you post that concern above and posted a separate reply (actually two now) regarding it.
How would you tell the difference between the System dbus instance vs. Nginx, for instance?
That's why I'm asking. A mode where "any background process keeps the distro alive" + systemd means the distro will never shut itself down, and I'll be running a lot of wsl -t distro to keep things clean. In that case, I'm better off without systemd (despite it ostensibly providing other value that I might want).
That means it would also be critical to ensure that systemd is something I can opt out of. Microsoft makes it sounds like it will eventually become the default, which is fine, so long as the wsl.conf flag to disable it doesn't disappear.
To throw another point into the decimal, there may also be use for the option value that says "never terminate this distro unless explicitly instructed".
It's not something I have to care about now myself, given systemd, but there is a mode of working when you don't necessarily want to work from a WSL prompt, but also enjoy the ability to throw out WSL commands in the current working directory with
$ do-something-linuxy
(using RunInWSL, but you get the idea). If you do this all day, stopping and restarting the distro every time you take too long between entering WSL-handled commands is just a lot of wasted overhead.
On the above discussion, for my money, if you're using systemd support, you're pretty much declaring that your distro is system-like, not throwaway-shell-like, so I'd argue with keeping the distro alive as long as systemd is running; i.e., until you explicitly systemctl poweroff. Especially since, given that the same announcement points out:
Additional modifications had to be made to ensure a clean shutdown (as that shutdown is controlled by systemd now)
WSL is presumably quietly running systemctl poweroff every time it idles out.
That means it would also be critical to ensure that systemd is something I can opt out of. Microsoft makes it sounds like it will eventually become the default, which is fine, so long as the wsl.conf flag to disable it doesn't disappear.
Definitely agree (and was just about to post that). I definitely want it opt-in regardless, since I think I'll prefer to run without it most of the time (and I have some distros that absolutely don't use Systemd at all).
But what about the "whitelist" option I mentioned above? Would that work for the scenarios you envision?
You're always going to want distros without systemd, because Linux namespaces (which separate the distros) don't cover everything; things systemd does in one distro will and do leak to the others. For that reason alone, I can't see it ever becoming compulsory.
(This is why I put an explicit mention in the systemd-genie docs that running more than one distro at a time with systemd was unsupported. You don't want them fighting over whose sysctl values, whose loaded kernel modules, whose AppArmor profiles, or whose network configuration is the validest. That way lies many subtle and impossible-to-diagnose issues.)
@cerebrate - linux namespaces really should cover everything, maybe someday they will (looking at you binfmt!).
WSL is interesting in some ways because we run each distro as a privileged container (root is actually root). This makes the user powerful enough to be harmful to other tenants of the VM. But since they're all owned by the same Windows user, we've decided that's ok.
Having mentioned this elsewhere, for anyone who finds it of interest - the current workaround for this is to create a process that runs forever and detach it from the calling terminal such that it will persist (as a child of the init) even when the terminal is closed. So you can create a script, call it wait-forever.sh:
#!/bin/sh
while true
do
sleep 1h
done
then run it with
nohup wait-forever.sh > /dev/null &
and then when you exit the terminal that script will keep running as a child of init, and that will keep the instance open.
(Make sure you don't run it inside a tmux session or anything else that might kill its children in ways nohup won't stop.)
Or use https://linux.die.net/man/1/daemonize to accomplish this? I'd recommend this.
@pmartincic
Have you tested this one? And if so, what did you use as the process to daemonize?
Because as far as I know, the criteria for "keeps the instance open" requires that a process be a child of the Microsoft init to do so, and since daemonize (unlike nohup) disassociates the daemonized process from its process group, it stops being a child of its init and becomes a child of pid 1 (systemd), which shouldn't work to keep the instance running.
And when I tested it, sure enough, it didn't.