Skip to content

Fix NullReferenceException crash in PowerToys Run when launching UWP apps#47412

Closed
MuyuanMS with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-powertoys-run-crash
Closed

Fix NullReferenceException crash in PowerToys Run when launching UWP apps#47412
MuyuanMS with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-powertoys-run-crash

Conversation

Copilot AI commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Summary of the Pull Request

MethodBase.GetCurrentMethod() returns null inside a Task.Run() lambda under JIT optimization, causing .DeclaringType to throw NullReferenceException in the catch block of UWPApplication.Launch. Since that exception escapes an async void method, it crashes PowerToys Run entirely.

Fix: Replace MethodBase.GetCurrentMethod().DeclaringType with typeof(UWPApplication) — a compile-time constant, always safe:

// Before — crashes when JIT inlines the lambda
ProgramLogger.Exception($"Unable to launch UWP {DisplayName}", ex,
    MethodBase.GetCurrentMethod().DeclaringType, queryArguments);

// After
ProgramLogger.Exception($"Unable to launch UWP {DisplayName}", ex,
    typeof(UWPApplication), queryArguments);

Applied in two locations in UWPApplication.cs:

  • Launch() — the direct crash site (inside Task.Run() lambda catch block)
  • IfApplicationCanRunElevated() — same unsafe pattern in a regular catch block

PR Checklist

  • Closes: #xxx
  • Communication: I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected
  • Tests: Added/updated and all pass
  • Localization: All end-user-facing strings can be localized
  • Dev docs: Added/updated
  • New binaries: Added on the required places
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

GetCurrentMethod() is documented to be unreliable in inlined or compiler-transformed contexts (lambdas, async state machines). When ActivateApplication throws and the catch block calls GetCurrentMethod() on the inlined lambda, it returns null. The resulting NullReferenceException is not caught by the surrounding try/catch (it originates in the catch itself), surfaces as a faulted Task, and re-throws on the SynchronizationContext via the async void frame — 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

  • Code review and CodeQL security scan passed with no findings.
  • Verified both changed call sites use the correct enclosing type.

…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
Copilot AI requested a review from MuyuanMS April 29, 2026 11:07
@niels9001 niels9001 added the Product-PowerToys Run Improved app launch PT Run (Win+R) Window label Apr 29, 2026
@MuyuanMS

Copy link
Copy Markdown
Contributor

Needing more information from author as log is missing. Closing this generated PR for now

@MuyuanMS MuyuanMS closed this May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Product-PowerToys Run Improved app launch PT Run (Win+R) Window

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PowerToys Run crashes when launching app (NullReferenceException in UWPApplication.Launch)

3 participants