Fix NullReferenceException crash when launching UWP apps from PowerToys Run#47376
Fix NullReferenceException crash when launching UWP apps from PowerToys Run#47376Copilot wants to merge 3 commits into
Conversation
…odBase.GetCurrentMethod().DeclaringType with GetType() and adding null guard for UserModelId Agent-Logs-Url: https://github.com/microsoft/PowerToys/sessions/30311995-2a99-4433-8115-d805ec66b678 Co-authored-by: MuyuanMS <116717757+MuyuanMS@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Fixes a PowerToys Run UWP launch crash by making UWP launch failure logging avoid MethodBase.GetCurrentMethod() in affected async/error paths and by handling missing app user model IDs gracefully.
Changes:
- Adds a null/empty
UserModelIdguard before UWP activation. - Replaces vulnerable
MethodBase.GetCurrentMethod().DeclaringTypelogging calls withGetType(). - Makes launch failure message display null-safe via
api?.ShowMsg().
| if (string.IsNullOrEmpty(UserModelId)) | ||
| { | ||
| ProgramLogger.Exception($"UserModelId is null or empty for {DisplayName}", new InvalidOperationException(), GetType(), queryArguments); | ||
| var name = "Plugin: " + Properties.Resources.wox_plugin_program_plugin_name; | ||
| var message = $"{Properties.Resources.powertoys_run_plugin_program_uwp_failed}: {DisplayName}"; | ||
| api?.ShowMsg(name, message, string.Empty); | ||
| return; |
There was a problem hiding this comment.
Added a regression test in src/modules/launcher/Plugins/Microsoft.Plugin.Program.UnitTests/Programs/UWPTests.cs that exercises launching a UWPApplication with an empty UserModelId.
The test verifies the result action returns successfully and reports the launch failure through ShowMsg instead of throwing. I also rebuilt Microsoft.Plugin.Program and Microsoft.Plugin.Program.UnitTests, then ran the full Microsoft.Plugin.Program.UnitTests assembly locally (96 passing).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
@copilot please re-review the latest changes. |
The changes look good. The regression test in commit
The fix in |
Summary of the Pull Request
Fixes a crash in PowerToys Run's UWP launcher where a
NullReferenceExceptionescapes an error-handlingcatchblock inside anasync voidmethod, causing an unhandled exception on the WPF dispatcher. The crash was reported when launching Camera via PT Run but affects any UWP app whose launch fails.PR Checklist
Detailed Description of the Pull Request / Additional comments
Root cause — two-stage failure:
ActivateApplication(UserModelId, ...)fails (e.g.UserModelIdis null/empty for certain apps).catchblock callsProgramLogger.Exception(..., MethodBase.GetCurrentMethod().DeclaringType, ...). Inside aTask.Runlambda in .NET 9,MethodBase.GetCurrentMethod()can returnnull, so.DeclaringTypethrows a secondaryNullReferenceExceptionthat escapes thecatchblock entirely.Because
Launchisasync void, the uncaught exception propagates to the WPF dispatcher and crashes the process. The same.NET 9 / MethodBase.GetCurrentMethod()issue was already identified and fixed inPluginManager.cs(lines 242–245) with an explicit comment.Changes in
UWPApplication.cs:MethodBase.GetCurrentMethod().DeclaringType→GetType()in theLaunchlambdacatchblock.GetType()is always non-null on any instance and is the correct fix, consistent withPluginManager.cs.IfApplicationCanRunElevated()'scatchblock for consistency.string.IsNullOrEmpty(UserModelId)guard before callingActivateApplication— logs the anomaly and shows a graceful user-facing error instead of propagating into the COM call.api?.ShowMsg()(null-conditional) for additional defensive robustness.Regression test added in
UWPTests.cs:UwpResultActionShouldHandleEmptyUserModelIdGracefullyconstructs aUWPApplicationwithUserModelId = string.Empty, invokes the result action, and asserts thatShowMsgis called with the display name in the message — without throwing or crashing the async void path. AManualResetEventSlimis used to properly synchronize with the async launch operation.Validation Steps Performed
MethodBase.GetCurrentMethod()/ .NET 9 regression is the same root cause documented inPluginManager.cs.NullReferenceExceptionoriginating at the top of the lambda (b__0) rather than inside a nested callee, consistent with the.DeclaringTypeaccess on a nullMethodBasebeing the throw site.UwpResultActionShouldHandleEmptyUserModelIdGracefullyadded and verified passing locally (96 tests passing inMicrosoft.Plugin.Program.UnitTests).