Repository navigation
fix(windows): preserve helper exit codes for privilege switching - #266
Merged
Merged
Conversation
WinUI helper 在 OnLaunched 中直接 Environment.Exit 时,CoreMessaging 会在 DLL 卸载阶段 fail-fast,退出码被替换为 0xC0000602,控制面板因此把成功的 --task-admin 提权准备也判为失败。 改为先设置 Environment.ExitCode,再通过 Application.Exit() 正常关闭 WinUI, 所有 helper 模式统一走这一退出路径。
uvwt
approved these changes
Nov 26, 2026
uvwt
left a comment
Owner
There was a problem hiding this comment.
感谢详尽的根因定位、调用栈分析和回归测试!改走 ExitHelper 正常关闭 WinUI 既克制又稳妥,CI 已全部通过,这就合入 main。
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.
问题
Windows 控制面板在「设置 → 权限」开启「以管理员权限运行 AgentDock 核心」并确认 UAC 后,提示「AgentDock 管理程序执行失败,退出码:-1073740286」(
0xC0000602,STATUS_FAIL_FAST_EXCEPTION)。计划任务其实已经准备好了(新的最高权限任务处于禁用状态),但运行清单仍是standard。原因在 helper 的退出方式。
agentdock-tray.exe --task-admin在NativeApp.OnLaunched中直接调用Environment.Exit(...)。此时 WinUI 尚未关闭,ExitProcess卸载 DLL 时,Microsoft.Internal.FrameworkUdk.dll进入CoreMessaging.dll后 fail-fast,TaskAdminService.Run的真实返回值被替换为0xC0000602。控制面板只认退出码0,所以计划任务准备成功了也会按失败处理。不需要 UAC 也能复现:以普通权限运行
agentdock-tray.exe --task-admin bogus-action。Run解析参数失败并返回1,进程退出码却是0xC0000602。附加原生调试器时不复现(PEB.BeingDebugged为 1 时进程按预期返回1),清除该标记后可稳定抓到:RunHelperAndExit承载的其他 helper(--start-core、--start-tunnel、--run-core-task、--run-elevated-agentdock)用的是同一种退出方式。本机探针中它们没有触发,但在OnLaunched中调用Environment.Exit的最小实验里,立即退出、经线程池退出、延时 2 秒退出、GC 后退出,35 次中 34 次 fail-fast。修改
NativeApp.ExitHelper(int exitCode):先写Environment.ExitCode,再调用Application.Exit()正常关闭 WinUI。Application.Start返回后,生成的Main以该退出码结束。第二实例退出路径用的也是Exit()。--task-admin和RunHelperAndExit统一改走ExitHelper,App.xaml.cs不再调用Environment.Exit。TestWindowsHelperModesExitThroughWinUIShutdown,防止重新引入Environment.Exit,并要求先写退出码再关闭 WinUI。另一种做法是自定义
Main,让 helper 在Application.Start之前执行完、完全不初始化 WinUI。那样需要关闭 XAML 生成的入口并接管 Windows App SDK 的引导,改动面明显更大,所以本 PR 保持现有入口,只修正退出方式。验证
环境:Windows 10 22H2 x64(19045)、Windows App Runtime 2.7、.NET 8.0.31。
--task-admin bogus-action(普通权限):v1.1.1 安装版 6/6 次为0xC0000602,main本地构建 3/3 次为0xC0000602;本 PR 构建 10/10 次返回1。--task-admin restore,使用不存在的任务名和Exists=false的备份状态(成功路径,不改动任何计划任务):v1.1.1 为0xC0000602,本 PR 为0。前后根目录计划任务一致。1,行为不变。dotnet publish .\desktop\windows\winui\AgentDock.WinUI.csproj -c Release -r win-x64 --self-contained falsegofmt -l $(git ls-files '*.go')、go vet ./...:通过。go test ./... -count=1 -timeout=3m(Windows):54 个包通过,测试后没有未跟踪文件。另有 7 个用例失败,均与本 PR 无关:2 个符号链接用例在main上同样失败(本机未开启 Windows 开发者模式);5 个是全量并行时超时,在main和本分支上单独重跑都通过。go test ./scripts/test:通过。新测试在main的App.xaml.cs上失败,在本 PR 上通过。限制:没有替换安装后在界面里完整走一遍权限切换;Windows 11、其他 Windows App Runtime 版本、Linux
-race以及test-install-windows.ps1未在本机执行。