wslg icon indicating copy to clipboard operation
wslg copied to clipboard

New WSL session loses focus on startup

Open bobf32 opened this issue 3 years ago • 22 comments

Windows build number:

19045.2604

Your Distribution version:

22.04

Your WSL versions:

WSL version: 1.1.3.0 Kernel version: 5.15.90.1 WSLg version: 1.0.49 MSRDC version: 1.2.3770 Direct3D version: 1.608.2-61064218 DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp Windows version: 10.0.19045.2604

Steps to reproduce:

See https://github.com/mintty/wsltty/issues/329

WSL logs:

No response

WSL dumps:

No response

Expected behavior:

No response

Actual behavior:

On starting a new WSL session, the focus should be set to the window containing that session. This is not the case.

bobf32 avatar Mar 05 '23 20:03 bobf32

@bobf32, thanks for reporting the issue, I do not observe the issue reported, After launching the mintty, the new "Ubuntu-20.04" window open, then the focus is moved to that window from the window launched mintty, So if I type something from keyboard, the input goes to newly opened "Ubuntu-20.04" window, as below shows as I typed "hello". Is this the expected behavior? thanks!

image

hideyukn88 avatar Mar 09 '23 20:03 hideyukn88

That's pretty much it, but I can simplify. To reproduce:

  1. Ensure .wslconfig file does not contain line guiApplications=false
  2. From a command prompt: wsl --shutdown
  3. wsl

The window housing the command prompt and now bash session somehow loses focus at this point, and I have to alt-tab or click in it to type into the terminal. Mintty was a red herring, not required to reproduce this.

Further invocations of wsl occasionally but not always exhibit the same issue, but the numbered steps above are reproducible, i.e. after a shutdown. If I include guiApplications=false in .wslconfig all is fine.

bobf32 avatar Mar 09 '23 22:03 bobf32

@bob32, I still don't observe the focus lost with wsl.exe, but starting up WSL with guiApplications enabled involves launching msrdc.exe in background, so it possible that the focus is changed by that, although it might vary by other factors such as host Window's version. Would you please share log files from /mnt/wslg? I would like to double check if there are some unexpected errors, thanks!

hideyukn88 avatar Mar 09 '23 22:03 hideyukn88

Sure, please see attached.

wslg.zip

bobf32 avatar Mar 09 '23 22:03 bobf32

@bobf32, thanks for sharing the log, and I do see some clue there, below line in your log indicates there is some UI being created in Linux side, and this is most likely the reason of focus change, and for me, I don't see this until I actually run some GUI apps, like firefox, do you have some custom script to run at startup? thanks!

[22:23:12.933] rdp_rail_wake_handler is called on peer_ctx:0x5559cbf733a0

hideyukn88 avatar Mar 09 '23 23:03 hideyukn88

I have a .bashrc but nothing else that I am aware of. And this does not start any gui stuff I don't think. I don't tend to use gui apps with wsl.

bobf32 avatar Mar 10 '23 14:03 bobf32

@bobf32, is it possible you can install fresh distro and see if same issue still occurs? and if you have any customization made by c:\Users\[your user name]\.wslconfig or /etc/wsl.conf, please share with us, thanks!

hideyukn88 avatar Mar 10 '23 19:03 hideyukn88

I have just installed a fresh copy of Ubuntu 22.04.2 LTS and the problem still occurs.

Problem occurs now in two distros, and in both cases whether .wslconfig exists or not.

In neither case do I have /etc/wsl.conf.

bobf32 avatar Mar 10 '23 21:03 bobf32

@bobf32, thanks for trying out. I think, at this point, I will need to have some additional instrumentation to understand this further, and at same time, I will be checking if any repro internally, I will post here if any updates, thanks!

hideyukn88 avatar Mar 10 '23 22:03 hideyukn88

@bobf32, update: I'm able to reproduce the issue on Windows 10 (while I cannot on Windows 11), and it looks you are on Windows 10 as well, and in this case, the window focus is moved to a window created by msrdc.exe (msrdc.exe is indirectly launched by wsl.exe in background). Do you happen to have separate environment on Windows 11? if so, please check if this does not occur on Windows 11, thanks!

image

hideyukn88 avatar Mar 28 '23 18:03 hideyukn88

Thank you for the update @hideyukn88. I am indeed on Windows 10 but have not got a Windows 11 environment right now. I'll try and spin up a VM when I get some time.

bobf32 avatar Mar 28 '23 21:03 bobf32

The fix for this focus lost issue went into msrdc.exe code, thanks!

hideyukn88 avatar Apr 05 '23 22:04 hideyukn88

@hideyukn88 Great, is it available yet? What do I need to do to get the fix?

bobf32 avatar Apr 05 '23 22:04 bobf32

I'm having this problem. It doesn't seem like this fix has been released yet? Is there an ETA?

decriptor avatar Jun 09 '23 16:06 decriptor

@hideyukn88 That's great news! How does msrdc.exe get updated? Can I do anything to get it now (maybe wsl --update --pre-release)? Thanks!

jacktose avatar Oct 24 '23 19:10 jacktose

I'm having this exact issue on Windows 11 10.0.22631 N/A Build 22631, but it's happening with mstsc.exe rather than msrdc.exe. Could this fix be implemented there too please?

HeyItsJono avatar Oct 24 '24 09:10 HeyItsJono

I'm currently still having this issue on Windows 11, WSL2 Ubuntu, and :

[wsl2]
guiApplications=false

doesn't fix it

orzcode avatar Jan 12 '25 02:01 orzcode

I have this issue too and it's extremely easy to reproduce when simply switching between distros.

C:\Users\patnelson\Desktop>wsl --distribution Debian
patnelson@compy386:/mnt/c/Users/patnelson/Desktop$ exit
logout

C:\Users\patnelson\Desktop>wsl --distribution Ubuntu
###  focus is lost here ###
patnelson@compy386:/mnt/c/Users/patnelson/Desktop$ exit
logout

For reference, I'm on a fresh Windows 11 install (fresh from employer, that is) version 10.0.22631.4602 and Terminal version 1.12.10983.0. As explained above, it's also lost if you're starting from scratch as well, but you can very easily repro via switching between distros. In my case, I was just moving away from the default Ubuntu and installing Debian instead.

patricknelson avatar Jan 16 '25 02:01 patricknelson

I installed WSL almost a month ago and since the first day this has been happening, and is very annoying. The only thing that "fixed" this issue was putting this in my .wslconfig file:

[wsl2]
guiApplications=false

If this doesn't exist in that file there's a chance the window just randomly looses focus!! Even worse is the fact that it doesn't matter what launches WSL, this will happen anyways. This setting literally disables GUI-based applications, and is kind of inconvenient that to not loose focus when launching WSL, you need to disable GUI apps, and to use GUI apps you need to remove this setting, causing this issue to come back!

I hope this can get fixed...

STGamer24YT avatar May 20 '25 01:05 STGamer24YT

@hideyukn88 Did that fix ship? Would any recent occurrences of this constitute a regression or a new bug?

jacktose avatar May 29 '25 20:05 jacktose

Still having the issue, so probably fix wasn't released :(

misiek08 avatar Jul 24 '25 08:07 misiek08

still an issue on Windows 11, regardless of where it was launched: As a Windows Terminal instance, or from Powershell/Cmd. I'm using Debian.

minhperry avatar Aug 29 '25 09:08 minhperry