microsoft / WSL

Issues found on WSL
https://docs.microsoft.com/windows/wsl
MIT License
17.26k stars 812 forks source link

WSL terminates systemd service docker containers when last terminal is closed #9667

Open Newbrict opened 1 year ago

Newbrict commented 1 year ago

Version

Microsoft Windows [Version 10.0.22621.1265]

WSL Version

Kernel Version

5.15.79.1-microsoft-standard-WSL2

Distro Version

Ubuntu 22.04

Other Software

docker with systemd option enabled on wsl2

Repro Steps

sudo apt install docker.io docker-compose

start any docker container (a simple one is an nginx container)

close all terminal windows connected to the wsl instance/ Ubuntu 22.04

wait 10-20 seconds

open a new terminal window to the Ubuntu 22.04

run docker ps and notice that everything was restarted (indicated by the uptime being only since you opened the new terminal window)

Expected Behavior

I expected the systemd services, like docker, to continue running even when I close my terminals.

Actual Behavior

The services shut down when the terminals are closed.

Diagnostic Logs

No response

benhillis commented 1 year ago

This is by design. The VM hosting WSL will idle-terminate when:

  1. There are no open terminal windows and
  2. No backgroud processes were launched by the user explicitly (things launched by systemd don't count)

We do have an undocumented config option that disables this behavior, let me chat with the team a bit to see if that's something we should officially support.

Tristan971 commented 1 year ago

We do have an undocumented config option that disables this behavior, let me chat with the team a bit to see if that's something we should officially support.

Oh please, that would be truly amazing

Newbrict commented 1 year ago

This is by design. The VM hosting WSL will idle-terminate when:

  1. There are no open terminal windows and
  2. No backgroud processes were launched by the user explicitly (things launched by systemd don't count)

We do have an undocumented config option that disables this behavior, let me chat with the team a bit to see if that's something we should officially support.

That would be perfect.

Can you share how to do that, unofficially -- just between the two of us here, on this GitHub issue 😅

MorphBonehunter commented 1 year ago

Mhhh...maybe this hidden feature is something which would also solve https://github.com/microsoft/WSL/issues/6782 😄

Tristan971 commented 1 year ago

I mean the unstoppable idle termination is probably amazing for battery-powered devices, but on a desktop where the power use variation is so small in the grand scheme of things, it doesn't really have any upside...

DanSheps commented 6 months ago

@benhillis Do you happen to have an update on this (specifically, the undocumented config option)?

iannacone commented 6 months ago

since i do not want to install systemd or anything else just as a workaround for this stupid issue, i prefer to run a single line on every work day start. here mine 1 line preferred solutionS (try 1 at the time in this order) which requires no new sofware to be installed:

from windows host

wsl screen

from windows host

wsl tmux new -s backgroundsession

from wsl

nohup sleep 1d

WARNING: this is set to run for 1 day!

source1 source2

in case you want to terminate, or rollback without restart wsl, you can run htop and kill the relative process (screen, tmux, sleep)

MartinEmrich commented 6 months ago

Just experienced the same.

To be clear: This behaviour causes data loss for users!

My tmux session was not a systemd process, but a normal backgrounded process by me, which got ignored by this.

IMHO the lifecycle of the WSL process tree (VM,...) should not be tied to a frontend application interacting with it (Terminal), at least not with the default configuration.

p10tyr commented 4 months ago

This is by design. The VM hosting WSL will idle-terminate when:

  1. There are no open terminal windows and
  2. No backgroud processes were launched by the user explicitly (things launched by systemd don't count)

We do have an undocumented config option that disables this behavior, let me chat with the team a bit to see if that's something we should officially support.

You forgot to ask the users when designing this 🤣 Can you PLEASE 🙏 surface this option so that we can have docker running natively in wsl2 .. without having to have a shell open the whole time?

and no. not everyone wants to pay for Docker Desktop - actually - now it makes me think how they manage to do that now on their hidden vm??

p10tyr commented 4 months ago

This must be the best work around.

https://github.com/microsoft/WSL/issues/8854#issuecomment-1437845230

I made a ps1 file that runs with Windows Task Scheduler,

It flashes a terminal briefly but I find that to be a very acceptable "solution"

wsl-keepalive.ps1

$commandToExecute = "wsl -e screen -d"

# wsl -e Execute the specified command without using the default Linux shell.
#  screen A linux virtual terminal that lets you create mulitple virtual terminal sessions in your system
#  -d Detach screen immediately

Invoke-Expression $commandToExecute
codefaux commented 4 months ago

Is anyone willing to spill the beans on this secret setting? It's kind of shocking that it's partway through 2024 and this fairly basic feature is completely hidden to the user/system admin.