ADR 0011: A per-task exception slot: pygo statuses as POST exceptions, task failure, channel iteration¶
Status: Final Type: Feature Created: 2026-07-28 Amends: 0007 (the pending-exception slot, process-wide there, becomes per-task), 0010 (the restrictions that fact forced fall away) Related-ADRs: 0014 (generalises the ABI check built here). Provenance: arbitrations A21, A26, A27, A30, A31, A34; upstream questions U1, U17.
Context¶
ADR 0007's pending-exception slot was process-wide, which was sound while the compiled world was sequential and became unsound the moment tasks existed. The over-restrictions ADR 0010 shipped (a task cannot raise, a status aborts where it should raise, TaskGroup(timeout=) refused) were all that one fact wearing a different hat. This ADR makes the slot per task and follows the consequence out to everything that was blocked on it.
Decision¶
-
The pending-exception slot is thread-local. The exception runtime declares it thread-local (C11's
_Thread_local, with a fallback and a warning where the compiler lacks it), applied to the slot and to thefinallysave stack, which is the slot's history. Under Scheduler A a pygo task is an OS thread, so thread-local is task-local; Scheduler B will make a task smaller than a thread; the declaration then moves with it. The pygo ABI is untouched: the slot belongs to the language runtime. Nothing else about the error-return lowering changed: a sequential program emits exactly the IR it emitted before, because a program with one thread has one slot. -
A
pygo_statusis the POST exception it names.PYGO_CLOSEDisChanClosed,PYGO_CANCELLEDisCancelled,PYGO_SCOPE_CLOSEDisScopeClosed: the names the dev-mode kernel raises, with the kernel's message text, sostr(e)in a compiledexcept ChanClosed as e:is the string the interpreted program prints. The wrapper raises into the per-task slot and generated code checks the slot after the call, exactly as it does after any call to a function that can raise; a helper that cannot fail (creating a channel, entering a scope, asking whether its deadline fired) gets no check. A status with no POST spelling (PYGO_TIMEOUT,PYGO_DEADLOCK) still reports and aborts, because a wrong exception is worse than a loud stop.
The three names are importable (from pygo import ChanClosed) and are resolvable only through the import: they are classes in the pygo package and absent from builtins, so a program that writes except ChanClosed: without importing it raises NameError interpreted and is a diagnostic compiled. Cancelled gets a code with no parent, because the kernel derives it from BaseException on purpose: an except Exception: written for program errors must not swallow a scope shutdown. That is why the runtime's parent table maps each exception code to its parent, where it used to assume one root.
-
A task that raises propagates to its scope; only the trampoline can say so. The generated trampoline of each spawned function calls the body and then one task-exit site, which takes whatever the body left in the per-task slot, records it against the scope, and calls
pygo_task_failedexactly once. A task ending inCancelledis expected shutdown and does not report, so the exception's code is tested, with the pending bit only as state. The trampoline's argument is the scope, passed aspygo_spawn's payload: a task body needs to name its scope. The scope is the one pointer whose lifetime provably contains the task's (the exit joins; a drop may only follow the exit), so it costs no allocation and nothing has to free it. -
The scope's exit is
TaskGroup.__exit__. The kernel'sTaskGroupis normative and the wrapper follows it rule for rule: a body exception that is notCancelledcancels the subtree first; the join runs unconditionally; a child failure wins over the body's exception; and aCancelledbelonging to this scope is swallowed while one from anywhere else propagates. That last rule needs to know which scope cancelled. The status does not say, so the answer comes from the one fact that is reachable. Withtg.cancel()not lowered, the only cancellation this compiler can produce is a deadline, andpygo_scope_timed_out(scope)names the scope whose deadline fired; an inner scope cancelled by an outer scope's deadline correctly reports false and propagates.tg.cancel()will need a companion accessor and stays open (A26). -
TaskGroup(timeout=…)andtg.timed_outare lowered, since (2) and (4) are all they ever needed.timeout=is a keyword or the one positional argument (the kernel's signature isTaskGroup(timeout=None)), lowered topygo_scope_enter'stimeout_s;tg.timed_outis the one lowered attribute on a task group, read after the block, which is meaningful only because of (7). -
for msg in ch:is the kernel'sChan.__iter__with the generator removed. Receive; if the call left aChanClosedpending, take it and leave the loop; anything else (aCancelledfrom a scope deadline) stays pending and goes to the innermost handler.break,continueandelseget the array loop's block shape, andelseruns when the channel closed, which is what exhaustion means here. The loop had no lowering at all before this ADR, for exactly one reason: there was noChanClosedto end it with. -
A scope is leaked, like everything else this compiler allocates. ADR 0010 emitted
pygo_scope_exitfollowed bypygo_scope_drop, which released the scope at the end of the block and gave theastarget a lifetime Python's does not have: a latertg.spawn(f)was a use-after-free where the interpreter raisesScopeClosed, so it was refused by name. The drop is gone.pygo_spawnon an exited scope returnsPYGO_SCOPE_CLOSED, which (2) turns into aScopeClosed; the compiled program then says what the interpreted one says.pygo_scope_dropis now the one ABI entry point the compiler declares and does not call. -
The pygo ABI is single-sourced, and versioned. Inside the compiler, the wrapper unit's C source is the one definition and everything else is derived from it: the prototypes the C backend's preamble contains are parsed out of the definitions, and so is the signature table every emitted call is checked against. The class of bug this catches (an element crossing into a helper at the wrong width) passes every unit test, cross-backend parity and the stdout differential, because the three backends are not disagreeing: they are all wrong in the same direction, against a C definition that is none of their business. The check runs on every compile that lowers a pygo call and raises, under ADR 0014's rule that no POST program can cause it. Across the two repositories,
pygo_runtime.hdefinesPYGO_ABI_VERSIONand the wrapper unit#if-checks it, so a header that has moved is a build error naming the version; and a unit test parses the header and checks that each pygo entry point the compiler calls still has the signature it is called with. The C compiler checks that too, but only on a machine that has a pygo checkout and builds a concurrent program, which is not the machine a signature change is made on.
The conflict this slice could not avoid: exception groups¶
Spec §5.1 permits "scalar exceptions only; no exception groups" and §5.2's PP033 refuses except*. pygo's structured-concurrency error model needs the thing the base spec excludes. TaskGroup.__exit__ re-raises all remaining child exceptions together as a BaseExceptionGroup, even for a single child. So except ValueError: around a task group does not catch a task's ValueError in the interpreted program, and except Exception: does. (This is pygo upstream question U1, floor or ceiling, arriving for real; logged with U17.)
The stand-in is a scalar exception. A join that has child failures raises a scalar exception with the group's type name (ExceptionGroup) and the group's message text ("unhandled errors in pygo TaskGroup (2 sub-exceptions)", CPython's formatting to the character). It is not a group type: it holds no list, has no except*, and cannot be raised by a POST program: raise ExceptionGroup(...) is refused, because CPython's constructor takes a sequence of exceptions and POST has none to give it.
The reason for the stand-in is the project's rule. First-failure propagation is one line simpler and silently different: a compiled except ValueError: would catch what the interpreted one does not, on stdout, with no diagnostic. With the stand-in, every property a POST program can observe is equal: except Exception: catches, except ValueError: does not, str(e) is the same string, the uncaught report's last line is the group header CPython's traceback prints, and the exit status matches.
The children's identities are lost, since a scalar cannot hold them, so they are announced on stderr as the group's contents: one line per failing task, plus the body's exception when it was folded in. stderr already differs, since a compiled program has no traceback.
The task-exit site's failure ledger is a fixed table of 64 entries, and overflow announces and drops the excess. It is bounded because it holds only failures not yet joined; a scope joins every task it spawned, so a program with more than 64 concurrently-unjoined failures gets a loud, truthful stderr line and one propagated group.
What this ADR left deferred¶
Everything ADR 0010 had deferred when this ADR was written stayed deferred except the surfaces above. Some of it has landed since: select, closures and task arguments in 0010's consolidation, pygo.net in 0016. tg.cancel() and the rest of A26 stay open, as (4) says; pygo/notes/roadmap.md tracks the rest, actors and the M:N scheduler among them.
Consequences¶
The compiled surface stops being smaller than the interpreted one on the paths that matter. messages.close() then messages.recv() raises ChanClosed where it aborted. A task that raises cancels its siblings and propagates at the join. A scope deadline cancels and is swallowed at its own exit and not at an inner one. And tg outlives its with exactly as far as Python's does.
The exception vocabulary grew by four, and only one of them is POST's. ChanClosed, Cancelled and ScopeClosed are the concurrency profile's and are declared apart from the base vocabulary, so a POST implementation without the profile still has ADR 0007's seven. ExceptionGroup is a builtin and is the one name in the vocabulary that can be caught but never raised. Cancelled is the first name with no parent other than the root, which is why matching an exception indexes a parent table.