Skip to content

ADR 0016: pygo.net, a compiled TCP server

Status: Final Type: Feature Created: 2026-07-28 Amends: 0010: the pygo lowering surface gains its I/O half Related-ADRs: 0011 (statuses become POST exceptions through the per-task slot), 0009 (the null-handle precedent), 0007 (the closed exception vocabulary), 0015 (bytes, the prerequisite). Provenance comes from arbitrations A44, A47, A48.

Context

pygo.net (pygo notes/08 §4, src/pygo/net.py) is the pygo design's I/O surface: listen/dial, a Conn, and a Listener that is a channel of connections. Lowering it did not have to wait for the io-poller. The reason is one sentence: under Scheduler A a task is an OS thread, so a blocking socket call is just a blocking syscall. There is no M:N handoff to arrange and no readiness queue to own.

Cancellation is the one thing to arrange; the contract is to match the reference kernel's ceiling. The interpreted kernel does not block indefinitely: it runs each socket operation in ~0.1 s slices with a cancellation check between them. Scheduler A does exactly the same with poll(2): non-blocking sockets, a 100 ms slice, a task_check_cancel between slices. A TaskGroup(timeout=) around an accept loop has to mean the same thing on both sides, and a future poller removes the ceiling from both without a program changing.

Decision

  1. Eight additive ABI entry points, two opaque handles and one status, at pygo ABI 1.5, with the MINOR checked by the compiler. pygo_listen / pygo_listener_port / pygo_accept / pygo_listener_close / pygo_dial / pygo_conn_send / pygo_conn_recv / pygo_conn_close, plus pygo_listener and pygo_conn, plus PYGO_IO_ERROR. Nothing existing changed meaning, so the MAJOR does not move; the MINOR does. The emitted unit contains #if PYGO_ABI_MINOR < 5, the first MINOR checked this way. Earlier bumps were clarifications an older runtime satisfied without promising to; 1.5 adds symbols this unit calls, so an unchecked MINOR would turn that into eight undefined names at the link.

  2. The wrapper unit's three rules, unchanged. A listener and a connection cross as void *, so no emitted translation unit learns what a pygo_conn is; the ABI's out-parameters are absorbed in the wrapper, because the IR has no address-of; and every status becomes a POST exception raised into the per-task slot (0011). PYGO_CLOSED from accept is ChanClosed, with the kernel's own message.

  3. for conn in listener: is the channel loop. The kernel's Listener.__iter__ is Chan.__iter__ with accept where recv is, and so is the lowering: one function emits the block shape, the except ChanClosed: that ends the loop, and the else on exhaustion, taking the receive as a callback. That is the useful half of "a Listener is a channel of connections": a claim about observable semantics. One lowering reading both for msg in ch: and for conn in listener: is the strongest way to keep it true. A Cancelled from a scope deadline is not swallowed by either loop.

  4. net.Conn and net.Listener are handles that may cross a task boundary. The kernel says so in the strongest available way: both define __deepcopy__ as identity, so the interpreted copy-on-send specifies that they pass through unchanged. They are admitted as spawn arguments and on the send wire, so app.spawn(echo, conn), the accept loop's whole point, works.

  5. The annotations are dotted only. net.Conn and net.Listener are recognised as ast.Attribute over the name net, never as a bare Conn. Unlike TaskGroup and Chan, these names are not exported from the pygo package root, so a bare one is a name the interpreted program could not resolve either. Conn is also a common word a program might bind itself.

  6. The null handle and the guard that makes it defensible. for conn in listener: declares its loop variable in the block that dominates the loop and assigns it per iteration, because the backends give a reassigned local a stack slot. A declaration needs an initialiser of the variable's type; a Str's is "", an Int64's is 0; a connection has none. 0009 answered the same question for a CPython handle with a null handle, defensible because the FFI helpers guard on NULL: a placeholder that reaches an operation cannot be mistaken for data. The same answer is taken here and made defensible the same way: every net entry point in the wrapper checks its handle. A null one is a loud stop naming the only way a program can produce it, a loop variable read after a loop that never ran. Without the check this would be close(NULL->fd), a crash the author gets no more use from than from silent wrongness.

  7. A socket failure stops the program. The interpreted kernel raises whatever CPython's socket module raises, an OSError subclass chosen by the errno, with a message naming it. PYGO_IO_ERROR names no errno, so the compiled program cannot know which subclass or which message CPython would give; raising OSError itself would be a near miss, since a handler for the subclass CPython raises would not catch it. PYGO_IO_ERROR therefore reaches __pp_pygo_fail (reported on stderr, then abort()). Both sides exit non-zero on a refused connection; only one of them can be caught.

  8. A C default that is not the interpreted semantic is suppressed. send to a departed peer raises SIGPIPE; CPython sets that signal to SIG_IGN at start-up, so the reference kernel sees EPIPE and raises, while a compiled program with the default disposition is killed, leaving no output for any differential to compare. Scheduler A sets SO_NOSIGPIPE per socket (and passes MSG_NOSIGNAL where that is the spelling), so the failure arrives as PYGO_IO_ERROR and takes rule 7's path. The suppression is per socket: setting the process's disposition would be a library overreaching.

  9. Ownership v0, as everywhere else. A listener and a connection are allocated by the runtime and never freed; close closes the descriptor and, for a listener, makes every parked and future accept answer PYGO_CLOSED, while the handle itself stays readable (listener.port after close is not an error). There is no drop: it would inherit pygo_chan_drop's proof obligation ("no task is parked on it") with no way to discharge it.

Consequences

A compiled accept loop with a handler task per connection agrees byte-for-byte with the interpreted run over a real socket. The interpreted side is CPython's socket module and the compiled side is poll(2) on the task's own thread, so the agreement covers the scheduler and the I/O alike.

The residue is named. Closing a listening socket while another task is parked in accept is a use-after-close on the descriptor in both implementations. It is also the designed shutdown path in both. The accept loop reads the closed flag before the poll slice and again after it, which makes the outcome correct in every interleaving but does not close the window in which the descriptor number could be reused. for chunk in conn: is not lowered, and TLS is not in the ABI at all.