See Ash Code logo See Ash Code

An asyncio timeout did not stop my subprocess

Ashlynn Antrobus
• • 3 min read

An asyncio timeout stopped waiting for my command, but left its subprocess running.

A dragon watches a clockwork mouse running in a wheel beside an empty hourglass.

The one-shot Nushell backend had the same cleanup problem as Fish. Each execute() call owned a child process, yet cancelling the coroutine didn’t terminate that child. The July 11 repair made that ownership explicit.

Cancelling the task leaves cleanup to the task

The backend waits for a completion marker and then waits for the process to exit. A caller can put a deadline around that work:

await asyncio.wait_for(shell.execute(command), timeout=0.15)

When the deadline expires, execute() receives cancellation. That doesn’t mean the operating system has killed the subprocess. Without a cleanup handler, Python can stop reading while the child carries on.

For a fresh check, I loaded the archived before-and-after implementations and replaced only their process-spawning function with a fixture that starts a real Python child sleeping for thirty seconds. This isolates the cancellation path; it isn’t an integration test of Nushell parsing or startup.

The harness captured the child and checked the process listing after the timeout:

ImplementationBefore timeoutAfter timeout
Before the repairChild presentChild still sleeping; return code unset
After the repairChild presentChild absent; return code -9

The old implementation had already cleared its tracked-process reference. The harness retained its own reference so it could clean up that surviving child.

Kill, reap, then propagate cancellation

The repaired path is small:

except asyncio.CancelledError:
    if proc.returncode is None:
        try:
            proc.kill()
            await proc.wait()
        except OSError:
            pass
    raise

kill() requests termination; wait() collects the exit. Re-raising preserves cancellation for the caller instead of turning a timeout into an ordinary command result. The OSError guard prevents a process-exit race from replacing the cancellation exception with a cleanup error.

The harness asserts that the repaired child has an exit status and no process-list entry after cancellation. The archived cancellation and reset tests also passed in a separate focused run.

An abort method can’t wait behind the hung command

execute() holds the backend lock for its whole call. Previously, reset() tried to acquire that same lock before killing the current process. Resetting a hung command could therefore wait for the command it was meant to abort.

The repair lets reset reach the in-flight child without taking that lock.

This establishes cleanup of the directly tracked subprocess. The code doesn’t send a signal to a process group, and the fixture doesn’t create grandchildren. Neither is evidence that an arbitrary descendant tree will be terminated.

A timed-out command may never print the marker. The cancellation path now takes care of the child in that case. The normal path still waits for the completion record.

AA

Written by Ashlynn Antrobus

Software developer, author, and builder. Sharing technical lessons learned from building tools like Ebook2Text, IndexedDict, and rag-mcp, and navigating life as a programmer with dyslexia.