-
Notifications
You must be signed in to change notification settings - Fork 847
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
Distro shut down even with running processes #8854
Comments
Unfortunately, this is the new documented behavior, per blog post:
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:
That was actually surprising to me, as Windows 11's Perhaps we should have a feature-request for a |
Or just "until |
Fair points - I think an option to keep distros running (and not idle terminate them) is a good idea. |
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?) |
But what does this mean for systemd, then? I mean |
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
Basically, I think the flag would say "Work like it did in Windows 11 GA" ;-) |
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.
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 Just throwing it out there - I'm not tied to it by any means. |
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. |
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 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
(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
WSL is presumably quietly running |
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:
then run it with
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. |
Hmmmm. I tested it once upon a time when I was fixing an issue relating to interop and shell lifetimes. But haven't touched it since then. I can check again. I don't remember what all I tested with. |
FWIW, I think it may have worked in the past (pre-systemd-support), as I used daemonize to run my bottled systemd when writing genie, and the instance didn't go away then. But when using the current native systemd support, daemonized processes don't appear to keep instances around. |
Thank-you, I'll look into that. Shouldn't have changed because of systemd support. Edit: Oh no... I'll update after I look into it but I might know why. |
The reason seems fairly straight-forward: when you enable systemd, nothing is going to keep the instance alive except for an interactive shell, and this is by design. |
My personal preference at the moment to keep a distro running is to:
That ensures that even if you the distribution as To terminate the keepalive |
Lol! By design. Those are my words to utter :), or more specifically Ben's. My smoke tests with Perhaps I don't see what you see? |
Thanks for your script.
|
I just nuked my wsl instance thinking there was a problem with my distro, as a recent update seemed to have killed KDE's gui. I use wsl to persist a btrfs drive partition, and since redoing my wsl distro, the connection is severed frequently due to wsl shutting down. There was never a custom service needed. I also run SUSE, not ubuntu. |
@biggestsonicfan If you start the WSL 2 instance like this, it will keep on running even if you close all the shells:
|
@biggestsonicfan You can check whether systemd feature was enabled in your distro. When systemd feature is enabled, the init process will be |
@firejox The WSL 2 instance automatic shutdown is happening because Microsoft decided that's how they wanted it to behave by default. Closing your shells have no effect on the init process. WSL 2 instances run systemd by default now. |
@aki-k It does matter. In fact, |
@firejox After installing x11-utils in the WSL 2 instance, you can run:
and xev opens. But this doesn't make any change to how Microsoft has designed the WSL 2 instance auto-shutdown. |
@aki-k |
Thanks, but I don't want to start my instance like that.
Do I check the logs for that? |
@biggestsonicfan firejox doesn't know how systemd in WSL 2 and the WSL 2 auto-shutdown work. |
@biggestsonicfan It means systemd enabled in your distro. There are several ways you can try in your scenario.
The third way you can put the command in profile script which will automatically run when you start an interactive shell (see what NotTheDr01ds does). If you don' want spawn new running process for every time you start an interactive shell, you can use flock to control it. Here is an example. # put below in your profile script ( /etc/profile, /etc/profile.d/ ...)
nohup flock -nxF / keep-wsl-alive-script >/dev/null & # keep-wsl-alive-script can be replace by `flock -xF / true` due to double locking mechanism. If you want to use systemd service, I will recommend you to spawn sleeping Windows process in systemd service script. Because systemd service script will spawn child process of @aki-k Please give helpful information. Among these discussions, you just keep saying this is by design and don't explain the workflow of the design. It is important for those people who have the same problem and want to seek the solution. |
It is not. As you state. systemd requires the Continuing on, I am running a SUSE distro. All three patterns of To go the absolute furthest step, I feel that the suggested "invisible shells" and "heartbeat checks" are only band-aids to a problem which needs a solution. Unsubscribing from this as I don't think I'm going to get a real answer until I accidentally discover it as a feature pushed upstream. |
Honestly the best way to know whether systemd is enabled is to run
A systemd distro will show a much more:
(These are both Ubuntu 22.04) As previously mentioned, seeing PID 1 being |
This is false.
This is temper tantrum. |
Autostart (enable) didn't work for me.
But the service itself is fine. Seems like the interop layer is not ready during wsl startup. |
@codeart1st It looks like systemd-analyze plot > startup_order.svg
If keep-distro-alive.service is loaded before those
This will make resources: |
Thank you, you're right. |
@firejox Your solution works well when there is only one active distribution, but I am working with two, when starting the second one, the process in the first one is interrupted. |
@everson-plantae It is because the |
@firejox It seems WSL2 has become more aggressive when trying to shutdown itself. I added the systemd service suggested above (with the Any suggestions would be greatly appreciated. |
Like I said way back in the thread, the only processes that will keep the WSL session from being auto-terminated are children of the Microsoft init (not the pid 1 init, the WSL-supplied init). No systemd service can be this, so you won't get anywhere with those. To be clear, here's a pstree:
What you are looking for is the Relay(number) parent. A process which has that above it in the tree (here, that would be wait-forever.sh, machinectl, and sleep) will keep the WSL instance running; one that doesn't, won't. _nohup_ing or _daemonize_ing a waitforever script is still the best option, I believe. |
I used to use |
After following this saga since the beginning: "No." |
#8854 (comment) , but since it's got such a simple workaround, I can't imagine it's anywhere near the top of the WSL team's list of possible new features. |
@cerebrate I tried _nohup_ing and WSL2 (Ubuntu 22.04) still stopped after a night. Previously
|
Are there still plans to implement this? |
Small update for Windows 11 24H2 the waitfor.exe solution won't work anymore.
I think it's a problem with the PreStart
I disabled the PreStart part for now. |
This workaround that got inspiration from other posts such as this one, is guaranteed to be working. I am running some dockerized application stack using compose on
Enjoy that your app lives 💓 and will keep on living. Test it and see for yourself from windows 👀
$❯
From wsl terminal:
|
Version
Microsoft Windows [Version 10.0.22621.521]
WSL Version
Kernel Version
5.15.62.1
Distro Version
Ubuntu 20.04
Other Software
Docker Engine version 20.10.18
Repro Steps
docker run -d -p 80:80 nginx
)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:
Diagnostic Logs
No response
The text was updated successfully, but these errors were encountered: