Closed
Fix NullReferenceException crash in PowerToys Run when launching UWP apps#47412
Conversation
…odBase.GetCurrentMethod().DeclaringType with typeof(UWPApplication) Agent-Logs-Url: https://github.com/microsoft/PowerToys/sessions/0c6f59e1-c203-4f58-aa54-7b7443a3f672 Co-authored-by: MuyuanMS <116717757+MuyuanMS@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix crash in PowerToys Run when launching applications
Fix NullReferenceException crash in PowerToys Run when launching UWP apps
Apr 29, 2026
Contributor
|
Needing more information from author as log is missing. Closing this generated PR for now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
MethodBase.GetCurrentMethod()returnsnullinside aTask.Run()lambda under JIT optimization, causing.DeclaringTypeto throwNullReferenceExceptionin thecatchblock ofUWPApplication.Launch. Since that exception escapes anasync voidmethod, it crashes PowerToys Run entirely.Fix: Replace
MethodBase.GetCurrentMethod().DeclaringTypewithtypeof(UWPApplication)— a compile-time constant, always safe:Applied in two locations in
UWPApplication.cs:Launch()— the direct crash site (insideTask.Run()lambda catch block)IfApplicationCanRunElevated()— same unsafe pattern in a regular catch blockPR Checklist
Detailed Description of the Pull Request / Additional comments
GetCurrentMethod()is documented to be unreliable in inlined or compiler-transformed contexts (lambdas, async state machines). WhenActivateApplicationthrows and the catch block callsGetCurrentMethod()on the inlined lambda, it returnsnull. The resultingNullReferenceExceptionis not caught by the surrounding try/catch (it originates in the catch itself), surfaces as a faultedTask, and re-throws on theSynchronizationContextvia theasync voidframe — crashing the process.Using
typeof(UWPApplication)is the correct pattern: it is resolved at compile time, is always accurate, and avoids the reflection overhead.Validation Steps Performed