51 scheduled runs died with 0xC000013A. Task Scheduler was innocent.
51 scheduled Python tasks exiting with 0xC000013A. Bare ^C in every log. Task Scheduler showing success. If that's what you searched for, this is what happened.
What Task Scheduler's log actually said (and why it was not the killer)
Every failed run produced the same pair of events in the Operational log: event id 201 — "successfully completed ... with return code 3221225786" — followed immediately by event id 102 — "successfully finished." No termination event. No kill signal from the scheduler. Task Scheduler thought everything went fine. The process exited on its own, carrying STATUS_CONTROL_C_EXIT out the door.
That exit code, 0xC000013A, is what Windows assigns when a process is torn down by a CTRL_CLOSE_EVENT. It is not a crash. It is not a timeout. It is a signal — and Task Scheduler never sent it.
The time-of-day fingerprint
51 inbox-watch runs died this way. Four of the eight Build Log cycles up to 2026-11-03 produced nothing at all because of it. That's not noise.
The pattern that broke it open: 0 kills in 83 runs between 00:00 and 08:00. 47 kills in 165 runs between 11:00 and 23:00. Overnight, it ran clean every time. During the day, it died constantly. Nobody changed the code between midnight and noon. The only thing that changed was whether a human was at the keyboard opening and closing console windows.
What was really killing it
The tasks were running with an interactive token. That means they shared the interactive desktop's console session. When any console window on that desktop closes, every process attached to it receives CTRL_CLOSE_EVENT — and gets torn down with exactly 0xC000013A. The process always died at the same point: the IMAP session, the longest blocking call in the run. Whatever console closed at that moment took the Python process with it.
Everything tried that didn't work (and why each one couldn't)
Socket timeouts made no difference. The process died at 3 seconds, not the 45-second timeout — it was killed mid-wait, not hung. StopOnIdleEnd=False changed nothing because the scheduler wasn't the one stopping it. SetConsoleCtrlHandler(NULL, TRUE) installs cleanly but cannot help: unlike CTRL_C_EVENT, CTRL_CLOSE_EVENT is unignorable by design. You cannot mask it.
The textbook fix is session 0 isolation — register the task with LogonType S4U so it runs in a session with no interactive desktop attached. That would have worked. It failed with "Access is denied" because registering a task that way requires elevation that wasn't available in this setup.
The fix
Switch the interpreter from python.exe to pythonw.exe. A GUI-subsystem process is never allocated a console. If there is no console, CTRL_CLOSE_EVENT has no path to arrive through. cmd.exe can still be torn down — and in one test run it was — but the Python process it launched survived and completed the IMAP fetch regardless.
One side effect: pythonw.exe may have no stdout handle at all. Both scripts were updated to open their own log file when the inherited handle is missing, so nothing is lost silently.
What it cost, and what the fix proved
10 consecutive scheduled runs under active console churn. 10 out of 10 completed the IMAP fetch. One lost its cmd.exe wrapper mid-run and finished anyway. Before the fix: 51 dead runs and four silent Build Log cycles. After: clean.
The cost was real — 51 wasted runs, four empty build cycles, and the wrong assumption held for longer than it should have because the scheduler's log looked like success. The fix itself was a one-word change to the interpreter path. The expensive part was not believing the time-of-day data sooner.
Receipts
What this post rests on.
- 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