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¶
-
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, pluspygo_listenerandpygo_conn, plusPYGO_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. -
The wrapper unit's three rules, unchanged. A listener and a connection cross as
void *, so no emitted translation unit learns what apygo_connis; 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_CLOSEDfromacceptisChanClosed, with the kernel's own message. -
for conn in listener:is the channel loop. The kernel'sListener.__iter__isChan.__iter__withacceptwhererecvis, and so is the lowering: one function emits the block shape, theexcept ChanClosed:that ends the loop, and theelseon 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 bothfor msg in ch:andfor conn in listener:is the strongest way to keep it true. ACancelledfrom a scope deadline is not swallowed by either loop. -
net.Connandnet.Listenerare 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, soapp.spawn(echo, conn), the accept loop's whole point, works. -
The annotations are dotted only.
net.Connandnet.Listenerare recognised asast.Attributeover the namenet, never as a bareConn. UnlikeTaskGroupandChan, these names are not exported from thepygopackage root, so a bare one is a name the interpreted program could not resolve either.Connis also a common word a program might bind itself. -
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 is0; 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 beclose(NULL->fd), a crash the author gets no more use from than from silent wrongness. -
A socket failure stops the program. The interpreted kernel raises whatever CPython's
socketmodule raises, anOSErrorsubclass chosen by theerrno, with a message naming it.PYGO_IO_ERRORnames noerrno, so the compiled program cannot know which subclass or which message CPython would give; raisingOSErroritself would be a near miss, since a handler for the subclass CPython raises would not catch it.PYGO_IO_ERRORtherefore reaches__pp_pygo_fail(reported on stderr, thenabort()). Both sides exit non-zero on a refused connection; only one of them can be caught. -
A C default that is not the interpreted semantic is suppressed.
sendto a departed peer raisesSIGPIPE; CPython sets that signal toSIG_IGNat start-up, so the reference kernel seesEPIPEand raises, while a compiled program with the default disposition is killed, leaving no output for any differential to compare. Scheduler A setsSO_NOSIGPIPEper socket (and passesMSG_NOSIGNALwhere that is the spelling), so the failure arrives asPYGO_IO_ERRORand takes rule 7's path. The suppression is per socket: setting the process's disposition would be a library overreaching. -
Ownership v0, as everywhere else. A listener and a connection are allocated by the runtime and never freed;
closecloses the descriptor and, for a listener, makes every parked and futureacceptanswerPYGO_CLOSED, while the handle itself stays readable (listener.portafter close is not an error). There is nodrop: it would inheritpygo_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.