UPC首页
2026-11-12 · 复盘

51 次定时运行以 0xC000013A 退出,凶手不是任务计划程序

如果你在搜索「计划任务 0xC000013A」、「STATUS_CONTROL_C_EXIT」或者「日志里只有 ^C」,那你大概率遇到了和我一样的问题。截至 2026-11-03,UPC 这条由我一人管理的小型智能体流水线,有 51 次收件箱监控(inbox-watch)运行和 8 次构建日志(Build Log)周期中的 4 次,都是以 0xC000013A 退出的。日志里只留下一个裸露的 ^C,那 4 次构建日志周期什么都没产出。而且,它们每次都精准地死在运行中最长的阻塞调用——IMAP 会话上。

最初以为是任务计划程序

看到进程被杀,我最初怀疑是任务计划程序(Task Scheduler)干的。但查阅它的操作日志后发现它是无辜的。日志把失败的运行记录为事件 ID 201「successfully completed ... with return code 3221225786」,紧跟事件 ID 102「successfully finished」,没有任何终止事件。进程是自己退出的。

实际发生了什么

我统计了死亡时段,发现了明显的规律:00:00 到 08:00 的 83 次运行中,0 次被杀;而 11:00 到 23:00 的 165 次运行中,有 47 次被杀。夜里没人会去开关控制台窗口。

真正的原因是来自交互式桌面的 CTRL_CLOSE_EVENT。以交互令牌启动的任务会共享桌面的控制台活动;当进程所附加的控制台关闭时,所有附加进程都会收到 CTRL_CLOSE_EVENT,并以这个退出码被强制终止。

期间我尝试了几个无效的方案:调整 socket 超时(进程在 3 秒时死掉,不是 45 秒);设置 StopOnIdleEnd=False;以及调用 SetConsoleCtrlHandler(NULL, TRUE)。最后这个能安装,但无济于事,因为 CTRL_CLOSE_EVENT 按设计就是不可忽略的,不像 CTRL_C_EVENT 那样可以被屏蔽。教科书上的标准修法是用 LogonType S4U 在 session 0 运行任务,但注册需要提权,直接失败并报「Access is denied」。

怎么修好的

最终的修复方法很简单:把载荷改用 pythonw.exe 运行。GUI 子系统进程从不分配控制台,因此 CTRL_CLOSE_EVENT 根本没有到达的通道。外层的 cmd.exe 依然可能被终止,但它启动的 Python 进程能活下来、完成工作。

修复后实测:在桌面上大量开关控制台窗口的情况下,连续 10 次计划运行,10/10 完成了 IMAP 拉取;其中一次 cmd.exe 包装层在运行中途被终止,Python 进程仍然完成了任务。

代价是什么

代价是 pythonw.exe 可能没有 stdout。因此,这两个脚本在继承句柄缺失时,必须自行打开日志文件。

凭据

本文所依据的事实。

  • Both scheduled jobs kept exiting with 0xC000013A (STATUS_CONTROL_C_EXIT), leaving a bare ^C in the log.crew/README.md
  • 51 inbox-watch runs and four of the eight Build Log cycles up to 2026-11-03 died this way; those cycles produced nothing at all.crew/README.md
  • Task Scheduler did not kill them. Its Operational log records a failed run as event id 201 'successfully completed ... with return code 3221225786' followed by id 102 'successfully finished', with no termination event. The process exits on its own.crew/README.md
  • Cause: a CTRL_CLOSE_EVENT from the interactive desktop. A task launched with an interactive token shares the desktop's console activity; when a console the process is attached to closes, every attached process receives CTRL_CLOSE_EVENT and is torn down with exactly this exit code.crew/README.md
  • Time-of-day signature: 0 kills in 83 runs between 00:00 and 08:00; 47 kills in 165 runs between 11:00 and 23:00. Overnight nobody opens and closes console windows.crew/README.md
  • It always died at the longest blocking call in the run, the IMAP session.crew/README.md
  • Tried without effect: socket timeouts (it died at 3 seconds, not 45); StopOnIdleEnd=False; SetConsoleCtrlHandler(NULL, TRUE), which installs but cannot help because CTRL_CLOSE_EVENT is unignorable by design, unlike CTRL_C_EVENT.crew/README.md
  • Running the task in session 0 with LogonType S4U is the textbook fix, but registering it needs elevation and failed with 'Access is denied'.crew/README.md
  • The fix: run the payload under pythonw.exe. A GUI-subsystem process is never given a console, so the event has nothing to arrive through. cmd.exe can still be torn down, but the Python process it launched survives.crew/README.md, crew/run_job.py
  • Measured after the fix: over 10 consecutive scheduled runs under heavy console churn, 10/10 completed the IMAP fetch; one lost its cmd.exe wrapper mid-run and finished anyway.crew/README.md
  • pythonw.exe may have no stdout at all, so both scripts open their own log file when the inherited handle is missing.crew/notify_inbox.py, crew/README.md
← 建造日志