nbdd0121 / wsld

WSL Daemon - Stable X11 connection and time synchronisation for WSL2
Apache License 2.0
299 stars 26 forks source link

X still closes all linux apps after laptop sleeps #7

Closed gomarion closed 3 years ago

gomarion commented 3 years ago

Good day! I tried out the wsld in WSL 2 under Windows 10 and Ubuntu 20.04, followed all the instructions, but X still closes all the app after closing the laptop's lid. I'm running DWM using VcXsrv Server, and was hoping to persist the windows I opened in DWM that's why I stumbled upon your app.

wsld is running in both Ubuntu, and I ran wsldhost.exe in Windows, but somehow it doesn't persist. Is there anything that I'm doing wrong? I do notice though that when I run "netstat -abno" in cmd, I see that vcxsrv.exe is listening in 0.0.0.0:7680 rather than 6000. Does it have something to do with that? Thanks!

nbdd0121 commented 3 years ago

A few possibilities:

  1. wsldhost.exe being stopped. How did you run wsldhost.exe? If you run it through task scheduler, you might want to check "Conditions" and "Settings" to ensure it won't be stopped by task scheduler when power situation changes.
  2. wsld is not being used. Check /tmp/.X0-lock to see if the PID there matches that of wsld. Also DISPLAY to ensure it is set properly.
  3. If none of the above works, run both wsld and wsldhost.exe inside console to see if they output some error message.
oxc commented 3 years ago

Please be aware that if the "Stop if the computer switches to battery power" checkbox is checked, it seems to apply even if it is disabled. To disable, temporarily enable "Start the task only if the computer is on AC power".

image

I've stumbled across this myself multiple times.

gomarion commented 3 years ago

Hi! I just want to confirm that WSLD is now running successfully! What I did before was run both the Windows daemon and Linux daemon in console, but for some reason (probably some mistake in the configuration) apps still close after closing and opening the lid of my laptop. I revisited this setup again this time via Task Scheduler and running both daemons automatically, and things are looking good so far! What did help me was oxc's suggestion to tick the "Stop if the computer switches to battery power" option even though the parent option is disabled. He is correct in pointing out that somehow the said option is still enabled even if the parent option is disabled. I'm very grateful for your help! @oxc

I'll continue to observe this in the following weeks. But just out of curiosity, how have you guys been experiencing using this? Does it work every time w/o any issues? Or are there rare instances when it fails? Because prior to having run this successfully today, I made it a habit to save my VIM sessions before I put my laptop to sleep just to address my previous issues with X closing all the apps.

nbdd0121 commented 3 years ago

Did you originally run wsld in a terminal emulator? If so, that particular terminal might still talk to your vcxsrv via TCP, while all further connections go through wsld. Then when you sleep/resume, the terminal emulator's X connection drops, and it could have killed wsld (since it's not disowned).

gomarion commented 3 years ago

As a matter of fact, I did. I was running Linux wsld in gnome-terminal. And I think you were right in pointing that out. I was making sure that the daemons were working before I add them permanently as services in my system. But seems like adding them as services during startup would've spared my in the first place! So far right now, I'm not having any issues anymore with X and can confirm that WSLD is working flawlessly. Very brilliant work! Thank you @nbdd0121!

oxc commented 3 years ago

On a side node, consider running vim in tmux. That will definitely survive as long as wsl is not shutdown.

fkguo commented 3 years ago

@nbdd0121 Hello, first thanks for developing this tool. I also have the issue that the linux GUI apps get closed after waking up the laptop. I checked that both wsld (I run this in windows terminal by sudo ~/.cargo/bin/wsld) and wslhost.exe are still running. The issue seems to be the one you mentioned above: the PID in /tmp/.X0-lock does not match that of wsld shown by ps a. Any suggestion on how I can solve this issue is highly appreciated.

nbdd0121 commented 3 years ago

The issue seems to be the one you mentioned above: the PID in /tmp/.X0-lock does not match that of wsld shown by ps a. Any suggestion on how I can solve this issue is highly appreciated.

This could happen if WSL utility VM got rebooted (WSL will preserve things in /tmp across reboot, so /tmp/.X0-lock may contain a PID from last time, which may be a different process). To address this you could add force = true inside the X11 section in config file. This will force wsld to ignore the X11 locking mechanism and listen to the X11 unix domain socket.

fkguo commented 3 years ago

To address this you could add force = true inside the X11 section in config file. This will force wsld to ignore the X11 locking mechanism and listen to the X11 unix domain socket.

@nbdd0121 This seems to have solved the problem! Maybe you could write this also in the readme file. Thanks a lot!

fkguo commented 3 years ago

@nbdd0121 It worked well yesterday. But this morning, after I opened my laptop, the GUI app still got disappeared... Then after restarting the laptop, running wsld gave the error message Failed to listen: /tmp/.X0-lock does not contain a valid PID. Any clue for solving this issue? Thank you.

nbdd0121 commented 3 years ago

It worked well yesterday. But this morning, after I opened my laptop, the GUI app still got disappeared... Then after restarting the laptop, running wsld gave the error message Failed to listen: /tmp/.X0-lock does not contain a valid PID. Any clue for solving this issue? Thank you.

That's really weird. What's the content of your /tmp/.X0-lock file? Do you have any other X server running in wsld which could modify the file? As for the GUI app disappear issue, you can try the troubleshooting steps on my earliest reply above, and additionally verify that the GUI app is indeed connected to the Unix domain socket at /tmp/.X11-unix/X0.

fkguo commented 3 years ago

What's the content of your /tmp/.X0-lock file?

@nbdd0121 Thanks for the prompt reply. it was empty... After I typed in some random number in that file, it went back to normal. However, still switching networks kills the wsl firefox. Following the steps in your earliest reply, running wsldhost.ex inside a console gives Failed to listen: Only one usage of each socket address (protocol/network address/port) is normally permitted. (os error 10048). But I think this is due to the task of wsldhost.exe is already running...

nbdd0121 commented 3 years ago

After I typed in some random number in that file, it went back to normal. However, still switching networks kills the wsl firefox.

This sounds like it still connects via TCP. What did you set for DISPLAY?

fkguo commented 3 years ago

I have export DISPLAY = :0 in the .profile file as suggested in the readme. echo $DISPLAY gives ip address:0, where ip address matches the one for Ethernet adapter vEthernet (WSL) shown by ipconfig /all under windows. Is this the correct behavior?

nbdd0121 commented 3 years ago

No, that's wrong. TCP is used if it is set to an address. Check you .bash_profile or .bashrc to check if you set DISPLAY at a different place.

fkguo commented 3 years ago

Thanks for the hint. I have the following line in the .zshrc file: export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0 After commenting out this line, I got the following error for trying to open a GUI app Error: no DISPLAY environment variable specified. On the other hand, I have the export DISPLAY = :0 section in the .profile file. Sorry for the maybe too naive question, how can I do to make it work now? Thank you.

nbdd0121 commented 3 years ago

https://superuser.com/questions/187639/zsh-not-hitting-profile

fkguo commented 3 years ago

Thank you again. Now the problem becomes Unable to init server: Could not connect: Connection refused Error: cannot open display: :0

nbdd0121 commented 3 years ago

You probably ran wsld as root, then with default permission being 755 non-root programs couldn't access /tmp/.X11-unix/X0. I've just pushed a commit to set the mode to 777, you can download the latest version from Github Actions.

fkguo commented 3 years ago

Perfect!! It works. Thank you!

ihtiuh commented 2 years ago

Hi @nbdd0121, I am having issues with running Terminator GUI via WSL2 Ubuntu.

Here is my setup:

Which leads to the error You need to run terminator in an X environment. Make sure $DISPLAY is properly set when running wsl.exe bash -c "terminator"

I can see the correct PID in /tmp/.X0-lock for the wsld process I started. And the /tmp/.X11-unix/X0 also have correct permissions(777):

Any ideas? Thanks in advance :)

nbdd0121 commented 2 years ago

You need to set DISPLAY for terminator as well. bash -c would not set any environment variables that you might have set in your profile/bashrc as it's not interactive.

Please create a new issue for further problems.