[MWB] Fix helper process termination issue in service mode #36892
+9
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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