The "is it alive?" check that kills your terminal
A one-line process probe sent Ctrl+C to everything on the console. On Windows, it took our agent's whole session down five times before we found it.
Welcome to the first issue of Boternal Field Notes. We run fleets of AI agents that build and operate software with nobody watching. Every issue is one thing that broke in a real system, how we found it, and what we changed so it can't happen again. No summaries of other people's blog posts. Just our own scar tissue.
This one starts with a test suite that killed the terminal it ran in.
The symptom: the test run doesn't fail, it vanishes
One of our services keeps a lock file holding its process ID, so a second copy refuses to start while the first is still running. Ordinary stuff. Its unit tests had been green in CI, on Linux, for weeks.
Then an agent working on the Windows dev box ran the same test file locally, and the terminal died. It wasn't a failure, a hang or a traceback. The process and the shell hosting it disappeared mid-run, and the agent's session went with them.
The agent restarted and tried again. Same result. Five times in one afternoon.
What the isolation told us
We narrowed it down the way you'd expect:
| Probe | Result |
|---|---|
| Whole unit suite | Terminal dies |
| Just this one test file | Terminal dies |
Same file, --collect-only |
Fine: 105 tests collected in 0.17 s |
| A single test from the file | Passes |
| Whole suite except this file | 1,032 passed, exit 0 |
| Output redirected to a file | Still dies, and the file is left empty |
So the module imported cleanly, nothing else in the repo was affected, and it wasn't terminal-output volume. Something in a test body was killing the process, along with its parent.
Our first theory was confident and wrong. The file had eight asyncio.run(...) calls, each
creating and destroying an event loop, and Windows uses a different loop implementation
than Linux. Loop churn on Windows sounded right. We wrote it down as a hypothesis and flagged
it as unverified. That label mattered, because it was the wrong answer.
The real cause: signal 0 isn't zero on Windows
The lock-file code had this, with a comment every Unix programmer would nod along to:
os.kill(pid, 0) # Signal 0 = existence check, no actual signal sent
On Linux and macOS that's true. Signal 0 runs the permission and existence checks and delivers nothing. It's the idiom for "is that process still running?"
On Windows it's a different function wearing the same name. CPython's Windows os.kill
has two branches:
- If the signal is
CTRL_C_EVENTorCTRL_BREAK_EVENT, it callsGenerateConsoleCtrlEvent, which delivers a console control event. - Anything else opens the process and calls
TerminateProcess.
And on Windows, signal.CTRL_C_EVENT == 0.
So the "harmless existence check" is a Ctrl+C. In our runs it reached every process attached to the console: pytest, the shell, and the agent's session. Two tests in the lock-file class fed real PIDs into that check, and each run went down. Excluding just that class let the rest of the file finish in 1.84 seconds, which also killed the event-loop theory for good.
Why it took so long to see
Three things made this bug unusually good at hiding:
- It leaves no evidence. A clean Ctrl+C is not a crash. Windows writes no Application Error event and no resource-exhaustion event, so the event logs looked untouched. We took "nothing in the logs" as a reason to look elsewhere, when it was actually the clue.
- The traceback points at the wrong place. Console control events are delivered
asynchronously. By the time Python raised
KeyboardInterrupt, it was a few milliseconds later, inside unrelated code. Ours surfaced in<frozen codecs>. - A wrong comment protected it. Elsewhere in the codebase, a comment explained that on
Windows
os.kill(pid, 0)"calls TerminateProcess". That's false:TerminateProcessis the branch for non-zero signals. We checked on a throwaway child process: signal 0 left it running, and SIGTERM killed it. But the comment sounded authoritative, so the same call survived, unexamined, in three other files. The wrong explanation was load-bearing.
The fix: a real Windows probe
We replaced every call site with one helper. POSIX keeps signal 0, because there it's correct. Windows asks the kernel directly:
import ctypes, os
from ctypes import wintypes
def pid_is_alive(pid: int) -> bool:
if pid <= 0:
return False # on POSIX, <= 0 addresses a process *group*
if os.name == "posix":
try:
os.kill(pid, 0)
except ProcessLookupError:
return False
except PermissionError:
return True # exists, just not ours
return True
SYNCHRONIZE = 0x00100000
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
WAIT_TIMEOUT = 0x102
ERROR_ACCESS_DENIED = 5
k32 = ctypes.WinDLL("kernel32", use_last_error=True)
k32.OpenProcess.restype = wintypes.HANDLE
h = k32.OpenProcess(SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, False, pid)
if not h:
# Access denied means it exists and isn't ours, the same answer POSIX gives.
return ctypes.get_last_error() == ERROR_ACCESS_DENIED
try:
# Not signalled yet = still running.
return k32.WaitForSingleObject(h, 0) == WAIT_TIMEOUT
finally:
k32.CloseHandle(h)
Two details we learned the hard way:
SYNCHRONIZEis required. With onlyPROCESS_QUERY_LIMITED_INFORMATION, the handle opens fine and then the wait fails. That's an easy way to write a probe that's always wrong in the same direction.- Don't use
GetExitCodeProcess. Its "still running" value,STILL_ACTIVE, is 259, which is also a perfectly valid exit code. A process that exited with 259 looks alive forever.
The part that matters for agents
A human hits this once, swears, and remembers. An agent hits it, loses its session, comes back with no memory of why, and runs the same command again. Each attempt cost a full session restart. The bug wasn't just in the code. It was in what the agent didn't know.
So the fix shipped in three layers:
- The code. One
pid_is_alive()helper, with all call sites pointed at it, and the wrong comments corrected so they stop vouching for the bug. - A test that guards the platform you're not on. Our CI runs on Linux, so it could
never trip this bug. We added a test that fakes a non-POSIX
os.nameand assertsos.killis never called. It runs on Linux CI, and we confirmed it fails when the bug is put back. A guard you haven't seen fail is only a hope. - A standing rule for the agents. Our agents load a short "Windows shell gotchas" file
at the start of every session. This bug is in it now: what the symptom looks like, why
the logs stay empty, and the rule to investigate anything suspicious in a detached
console (
Start-Processwithout-NoNewWindow), so a stray Ctrl+C can't reach the session doing the investigating.
The third layer is the one we'd tell you to copy. Code fixes stay in the repo they were made in. Rules travel with the agent to every repo it works in next.
If you only take one thing
grep -rn "os.kill(.*, *0)" your codebase. If any of it can run on Windows, it isn't
checking anything. It's pressing Ctrl+C.
Next issue: shell commands over about 7 KB that our agents' tools silently dropped, and the 29,000-call measurement that found the exact cutoff.
Reply to this email if you've hit something like this. We read every reply.
Get the next one by email: subscribe to Boternal Field Notes.