Fix NullReferenceException when launching UWP apps (e.g. Codex) via PowerToys Run#47408
Draft
Fix NullReferenceException when launching UWP apps (e.g. Codex) via PowerToys Run#47408
Conversation
…ng MethodBase.GetCurrentMethod().DeclaringType with pre-captured GetType() Agent-Logs-Url: https://github.com/microsoft/PowerToys/sessions/87876c2e-8ba8-4c42-bba7-6360705dc671 Co-authored-by: MuyuanMS <116717757+MuyuanMS@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix NullReferenceException when starting Codex APP with PowerToys
Fix NullReferenceException when launching UWP apps (e.g. Codex) via PowerToys Run
Apr 29, 2026
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 compiler-generated closures under .NET 9+ JIT inlining. InUWPApplication.Launch, the catch block called.DeclaringTypeon that potentially-null return value, producing aNullReferenceExceptionwhenever a UWP app failed to activate — masking the real activation error and preventing the error dialog from appearing.PR Checklist
Detailed Description of the Pull Request / Additional comments
Root cause: Inside
Task.Run(() => { ... }), the JIT can inline the closure method. When that happens,MethodBase.GetCurrentMethod()returnsnull, and.DeclaringTypethrows. The compiler-generated class visible in the stack trace (<>c__DisplayClass64_0) is precisely this closure.Fix: Capture the type before entering the lambda so the closure holds a reliable reference:
GetType()onthisalways resolves correctly regardless of JIT inlining. The remaining uses ofMethodBase.GetCurrentMethod()in this file are in ordinary (non-lambda) methods and are unaffected.Validation Steps Performed
MethodBase.GetCurrentMethod()inUWPApplication.csis the one fixed here; all other call sites are in regular methods.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
o3svsblobprodcus318.vsblob.vsassets.io/usr/bin/dotnet dotnet build(dns block)If you need me to access, download, or install something from one of these locations, you can either: