microsoft / wslg

Enabling the Windows Subsystem for Linux to include support for Wayland and X server related scenarios
MIT License
9.9k stars 296 forks source link

WSL VM boot up steals focus from the foreground app #1212

Open tusharsnx opened 3 months ago

tusharsnx commented 3 months ago

Windows Version

Microsoft Windows [Version 10.0.22631.3235]

WSL Version

2.1.4.0

Are you using WSL 1 or WSL 2?

Kernel Version

5.15.146.1-2

Distro Version

No response

Other Software

No response

Repro Steps

  1. To ensure wsl isn't running in the background, run wsl --shutdown.
  2. Start wsl: wsl
  3. Focus is lost from the terminal app.

This repros on VScode and Visual Studio integrated terminals, and Windows Terminal also.

Expected Behavior

Focus is still on the foreground (terminal) app.

Actual Behavior

Focus is lost from the foreground app.

Diagnostic Logs

No response

github-actions[bot] commented 3 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

tusharsnx commented 3 months ago

As wsl is a background service, it shouldn't steal focus when starting up.

OneBlue commented 3 months ago

This is most likely caused by wslg, transferring.

hideyukn88 commented 2 months ago

@tusharsnx, would you please collect log and share with us? thanks!

  1. download foregroundTracing.txt and save as foregroundTracing.wprp at local disk.
  2. run wpr -start foregroundTracing.wprp from Windows's command prompt
  3. launch wsl and observe window focus issue.
  4. run wpr -stop foregroundTracing.etl
  5. share foregroundTracing.etl with us.
bersbersbers commented 2 months ago

@hideyukn88 I am using Windows 11 23H2 on two systems. Could reproduce that issue on both system this afternoon. On one system, I had the impression that when the focus got lost, the language selector (the ENG guy below) flickered; but that may have been a consequence of the focus switch itself. Disabling it did not help.

image

Now, I cannot reproduce this behavior on either system. Will update this issue with traces if it occurs again.

tusharsnx commented 2 months ago

@hideyukn88 TBH, I couldn't reproduce it at the moment, but I do see it sometimes. I think after I created this issue the severity has gone down a little. Previously, I used to see it at every wsl boot, but now seems like it only happens at the first wsl boot up after the system is restarted. I'm also on the latest pre-release version, so maybe something was fixed between this version and the earlier versions.

I'll try to submit the trace if I see it again. For now, since bersbersbers can repro it, I should keep it open?

tusharsnx commented 2 months ago

Okay, I've got a perfect repro. When I'm debugging any application and run wsl --shutdown && wsl while the debugging session is on, the windows terminal loses focus 100% of the time.

Steps to repro:

  1. Create a scratch project in VS
    
    #include <iostream>

int main() { std::cout << "hello world"; // I tried to put a breakpoint to keep the program running, // but that doesn't repro the issue. // So, it seems like program must be in running state, // which we do by blocking on cin.get(). std::cin.get(); return 0; }


2. Start a debugging session by clicking on "Local Windows Debugger".
3. A terminal window would open with the output of "hello world".
4. Create a new terminal tab within the same window.
5. run `wsl --shutdown && wsl`.
6. In a moment, the terminal window would lose focus.

---

### Trace:

_I repeated the step (5) 3 times._

![image](https://github.com/microsoft/wslg/assets/55626797/71d2b0e7-5be7-43ac-acc8-2de3f445f1e0)

[foregroundTracing.zip](https://github.com/microsoft/wslg/files/15164706/foregroundTracing.zip)
[wsl-focus-lost-repro-video.zip](https://github.com/microsoft/wslg/files/15164928/wsl-focus-lost-video.zip)

---

> On one system, I had the impression that when the focus got lost, the language selector (the ENG guy below) flickered; but that may have been a consequence of the focus switch itself.

@bersbersbers You're right, I just realized that too (I usually have my taskbar autohide so I couldn't notice it earlier) you can see in the repro video 😄