Skip to content

Fix NullReferenceException when launching UWP apps (e.g. Codex) via PowerToys Run#47408

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-nil-reference-in-codex-app
Draft

Fix NullReferenceException when launching UWP apps (e.g. Codex) via PowerToys Run#47408
Copilot wants to merge 2 commits intomainfrom
copilot/fix-nil-reference-in-codex-app

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 29, 2026

Summary of the Pull Request

MethodBase.GetCurrentMethod() returns null inside compiler-generated closures under .NET 9+ JIT inlining. In UWPApplication.Launch, the catch block called .DeclaringType on that potentially-null return value, producing a NullReferenceException whenever a UWP app failed to activate — masking the real activation error and preventing the error dialog from appearing.

PR Checklist

  • 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

Root cause: Inside Task.Run(() => { ... }), the JIT can inline the closure method. When that happens, MethodBase.GetCurrentMethod() returns null, and .DeclaringType throws. 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:

// Before
await Task.Run(() =>
{
    catch (Exception ex)
    {
        ProgramLogger.Exception(..., MethodBase.GetCurrentMethod().DeclaringType, ...);
    }
});

// After
var type = GetType();
await Task.Run(() =>
{
    catch (Exception ex)
    {
        ProgramLogger.Exception(..., type, ...);
    }
});

GetType() on this always resolves correctly regardless of JIT inlining. The remaining uses of MethodBase.GetCurrentMethod() in this file are in ordinary (non-lambda) methods and are unaffected.

Validation Steps Performed

  • Code review: no issues flagged.
  • Confirmed the only lambda-context use of MethodBase.GetCurrentMethod() in UWPApplication.cs is 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
    • Triggering command: /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:

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

NullReferenceException when trying to start Codex APP using PowerToys

3 participants