-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
[MWB] Fix helper process termination issue in service mode #36892
base: main
Are you sure you want to change the base?
Conversation
@microsoft-github-policy-service agree |
5639d15
to
ce41453
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for opening this pull request. Very interesting investigation 😄
I've merged latest main and merged it in, since it caused a build error with a recent refactor.
I've given it a try and it seems to fix it on my Windows 10 machine when going through Ctrl+Alt+Delete. On Windows 11, the Helper process never gets restarted.
On both Windows 10 and Windows 11, after going through the Lock Screen and having to login again, the Helper process doesn't get restarted.
So I think this fix is still incomplete. Can you please take a look? Thanks, in advance.
@jaimecbernardo Hmm, that's weird... Screen record of the helper process restarting through the Lock Screen
Is there any additional context needed to reproduce? Thanks, |
@YDKK , Gave it another try. After just starting, indeed it's working well, but after a couple of copy pastes from one side to the other and then going to Ctrl-Alt-Del screen, after I cancel that screen Helper doesn't come back. Trying to get a more detailed repro here's what I did. Having pasted on the PC I press Ctrl+Alt+Del on seems to be causing this issue? 🤔 |
@jaimecbernardo After further investigation, I found that the fixes for the threads generated by sockets and clipboard related were missing, so I fixed them in the same way. I'm sorry to bother you, but could you check again? |
Summary of the Pull Request
This PR addresses an issue reported in #30259, where running MWB (Mouse Without Borders) in service mode causes the helper process to remain terminated at various points, resulting in functionalities such as clipboard sharing becoming non-functional.
Detailed Description of the Pull Request / Additional comments
When MWB is launched in service mode, events such as desktop switches (e.g., UAC prompts) cause the helper process to be killed and subsequently restarted. To restart the helper process within the user's desktop session, the
CreateProcessInInputDesktopSession
method uses APIs such asWTSQueryUserToken
, which require privileges and the LocalSystem account to function properly.Link to the relevant code:
PowerToys/src/modules/MouseWithoutBorders/App/Class/Common.Launch.cs
Lines 88 to 202 in 315059f
However, in the scenario where this issue occurs, the main thread is impersonated with user-level permissions, causing these API calls to fail. As a result, the helper process remains terminated, and features like clipboard sharing stop working.
Through debugging, I found that the execution permissions of the main thread are impersonated (and not reverted) after a delegate is invoked via
DoSomethingInUIThread
from other threads (e.g.,InputCallback Thread
). The root cause of this behavior is unknown at this time, but I personally feel like it's due to a framework-side issue rather than an implementation issue.After experimenting with several approaches, I discovered that suppressing the flow of the execution context in the calling thread (using
ExecutionContext.SuppressFlow();
) before invokingDoSomethingInUIThread
prevents this issue. Given that suppressing the execution context flow from non-main threads does not appear to pose any problems in the current MWB implementation, this PR applies this fix.Validation Steps Performed