Skip to content

net: the connection engine in pycvc and arisrvx (ARIADNE_NET_ENGINE N2) - #1434

Merged
transfix merged 1 commit into
masterfrom
feat/net-engine-n2
Nov 22, 2026
Merged

transfix merged 1 commit into
masterfrom
feat/net-engine-n2

Conversation

@transfix

Copy link
Copy Markdown
Owner

Part N2 of the connection engine (docs/roadmap/ARIADNE_NET_ENGINE.md §2.7, §3, §4): N1's cvc::net::net_engine (#1392) in pycvc, and host.net in arisrvx.

What Python gets

Joe asked for "the full C++ api in python", not a wrapper over it. So bindings/pycvc/pycvc_net.i SWIG-wraps inc/cvc/net/net_engine.h itself, under the C++ names:

  • the types: net_engine, engine_options, listener_options, framing, inbound_limits, conn_info, net_event, net_batch, net_counters, net_stats (listener_stats, thread_stats), and the enums as SWIG spells them (close_reason_slow_reader, net_event.type_frames, framing.mode_delimiter);
  • every method: listen, port, stop_listening, send, send_many, broadcast, close, join, leave, set_framing, resume, want_writable, wait, poll, set_notify, stats and stop.

pycvc.ariadne.net holds the same objects, and host.net is a net_engine.

Python-side additions, only where the language needs them:

  • arguments take bytes-like objects or a str (sent as UTF-8), and framing.delimited(b"|") works as well as an int;
  • durations are ints of milliseconds, and the option structs take their members as keywords;
  • listen() raises OSError with the engine's reason instead of returning 0;
  • net_counters.closed is a dict keyed by reason name;
  • wait, poll, stop and destruction release the GIL;
  • members of class type read as copies and vectors as lists. SWIG's own getters return pointers into the parent object, so e.stats().threads read freed memory. There is a regression test for this.

Frames and the loop

  • Frames. net_batch.events gives each event's frames as memoryviews into the batch's buffers, with no copy. Each memoryview holds a reference to those buffers, so a frame kept after the callback stays valid instead of dangling. The exporter type is created once, at module init, and kept in the module dict.
  • asyncio. listen() returns a net_listener: the listener id, an int, with decorator methods:
    • on_accept(conn, info), which may return a reason to refuse the connection (sync or async);
    • on_frames(batch), with batch as [(conn, [memoryview, ...]), ...]: one call per batch per listener, each connection once;
    • on_close(conn, reason[, message]), on_writable and on_error.
  • The waker. A thread waits in wait() without the GIL and hands each batch to the loop with call_soon_threadsafe. It takes the next batch only after the loop has taken this one, so input that arrives while the loop is busy comes as one larger batch. A batch is delivered as accepts, then frames, then writables, then closes, then errors. That keeps each connection's events in order.
  • Engine ownership.
    • In arisrvx, HostHandles::net makes the relay's engine the first time the program uses host.net, through a "cvc.net.net_engine" capsule from _arihost.net(). It has --net-threads N I/O threads (2 by default) and outlives the program.
    • Under plain python, Host(net_threads=2) starts its own engine and stops it at shutdown, after its closes have reached on_close.

Measured (prettyhatemachine, Xeon E5-2650 v2)

The setup is the same as N1's: 6000 loopback receivers, a 113-byte line, 2 I/O threads, 21 rounds. Other sessions' builds were running (load about 10 on 32 cores), so N1's C++ benchmark and the Python one were run back to back, twice:

C++ net_engine_test Python bench_pycvc_net.py
I/O CPU per receiver 19.3, 17.9 µs 17.8, 20.1 µs
broadcast() call 19.5, 23.3 µs 30.5, 34.9 µs
burst of 10: I/O CPU per receiver per message 2.0, 2.7 µs
all received 63, 56 ms 69, 67 ms (Python reader threads)
  • Engine cost. From Python the engine costs what it does from C++, at the write floor. N1's reference was 17.6 µs at 2 threads with the box idle, against 16.5 µs for a bare write().
  • Binding overhead. About 1 µs per call: io_threads() costs 0.8 µs, send 2.8 µs and broadcast to an empty group 5.3 µs, with the engine's queueing included. The remaining gap on a single broadcast comes from the benchmark's Python reader threads waking at that moment.
  • The program's side. A Python loop of sock.send() to the same 6000 sockets costs 33.5–34.1 µs of the program's CPU per receiver, 0.20 s per message. sublevels' soak measured about 35 µs with asyncio's transports. Through the engine, the program makes one call of 30–35 µs.
  • Input. 6000 receivers each send a line. All 6000 reach on_frames on an asyncio loop within 116–122 ms, in 2 on_frames calls, at 1.3–1.4 µs of loop CPU per frame.

The same numbers are in the doc's §4 under (N2, measured). A real NIC and sublevels' own load generator are for the sublevels soak (cy-pca/sublevels#8), which can now rerun on the engine.

Tests

  • pycvc_net (test_pycvc_net.py): the C++ API over loopback, covering the option structs, wait/poll batches and memoryview lifetime, every command, set_framing with pause_after, close reasons, stats and set_notify. It also covers the asyncio waker (refusals, one on_frames per listener, callbacks only on the loop's thread) and Host.net under plain python.
  • arisrvx_net: an arisrvx --python program with --net-threads 3 greets a connection, receives its frames, broadcasts to it and sees it close.
  • Run locally, Linux Release: pycvc_net, arisrvx_net, arisrvx_program/serve/check, and the existing pycvc_ariadne* tests pass. The writable-data scan on pycvc and arisrvx finds 0 symbols, and clang-format 18 is clean.
  • Not run locally: Windows and macOS, so CI is their first run.

Not in this PR: TLS (N3).

pycvc wraps inc/cvc/net/net_engine.h with SWIG under its C++ names
(net_engine, engine_options, listener_options, framing, inbound_limits,
conn_info, net_event, net_batch, net_counters, net_stats), so Python has
the engine's whole API: listen, port, stop_listening, send, send_many,
broadcast, close, join, leave, set_framing, resume, want_writable, wait,
poll, set_notify, stats and stop. pycvc.ariadne.net is the same names.

Additions only where Python needs them: bytes-like (or str) arguments,
framing.delimited(b"|"), milliseconds as ints, keyword constructors for
the option structs, OSError from listen, net_counters.closed as a dict,
and wait/poll/stop/destruction without the GIL. Class-typed members read
as copies and vectors as lists: SWIG's getters point into the parent and
dangled once a temporary went (stats().threads read freed memory).

Frames: net_batch.events gives each event's frames as memoryviews into
the batch's buffers. Each memoryview holds those buffers, so a kept frame
stays valid instead of dangling. The exporter type is made at module init
and kept in the module, so the writable-data scan stays at 0.

The asyncio side: listen() returns a net_listener (the id, an int) with
on_accept (which may refuse), on_frames (one call a batch a listener,
[(conn, [memoryview, ...]), ...]), on_close, on_writable and on_error.
A waker thread waits in wait() without the GIL and hands each batch to
the loop, then waits for the loop to take it, so input that arrives while
the loop is busy comes as one larger batch.

arisrvx: host.net is the relay's engine, made on the program's first use
(_arihost.net(), a "cvc.net.net_engine" capsule) with --net-threads I/O
threads (2 by default); it outlives the program. Under plain python,
Host(net_threads=2) starts an engine of its own and stops it at shutdown,
after its closes have reached on_close.

Tests: test_pycvc_net.py (pycvc_net), arisrvx_net. bench_pycvc_net.py is
the Python fan-out check against N1's numbers.
@transfix
transfix merged commit 1290ca1 into master Nov 22, 2026
30 checks passed
@transfix
transfix deleted the feat/net-engine-n2 branch November 22, 2026 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant