Repository navigation
net: the connection engine's C++ core (ARIADNE_NET_ENGINE N1) - #1392
Merged
Merged
Conversation
cvc::net::net_engine: a caller-owned engine of Boost.Asio I/O threads that accepts TCP connections, splits their input into delimiter, length-prefixed or raw frames, and queues their output. send, send_many and broadcast copy the bytes once into a refcounted buffer that each receiver's queue shares; commands apply in issue order, so a connection's bytes keep the order of its sends, broadcasts and closes. Groups (join, leave; a close leaves all of them), max_queued backpressure (slow_reader), want_writable at a low mark, flushing closes, inbound limits (bytes and frames per window, the first frame's deadline), and per-thread counters. A framing's pause_after and raw(n) make a framing switch land exactly after a frame, since the I/O threads split input ahead of the program. net_engine_test covers the framings and their switches, ordering, groups, backpressure, closes, limits and stop, and benchmarks one message to 6000 loopback connections. The sanitizer lanes run it. The design doc records what building N1 changed, and the measured fan-out cost, whose floor is the loopback write itself.
transfix
enabled auto-merge (squash)
November 20, 2026 05:42
macOS accepts an RLIMIT_NOFILE above OPEN_MAX and still refuses descriptors past it (and past kern.maxfilesperproc): recipe / macos-latest / Debug failed the 5115th connect with EMFILE. Clamp the limit the receiver count is fitted to on Apple.
This was referenced Nov 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part N1 of the connection engine in docs/roadmap/ARIADNE_NET_ENGINE.md (#1294): the engine's C++ core. Plain TCP only. TLS is N3, pycvc and arisrvx are N2, client connections and UDP are N4.
What it is
cvc::net::net_engine(inc/cvc/net/net_engine.h,src/cvc/net/net_engine.cpp):io_threadsAsio io_contexts, 2 by default and at most one per core. A connection lives on one of them for its whole life (its id says which), so nothing inside a connection takes a lock.max_frame,max_queued(4 MiB),no_delay,flush_timeout, and inbound limits: bytes and frames per window, and the first frame's deadline.delimited(byte)(the frame includes the delimiter),length_prefixed(1|2|4)(the frame is the payload) andraw(one read, at mostmax_frame). A frame pastmax_framecloses withframe_too_longas soon as that is known.set_framingapplies from the next byte not yet split.accepted,frames,closed,writable,error) go to one queue.wait(batch, timeout)andpoll(batch)take everything queued as anet_batch, whose frames arestring_views into refcounted buffers the batch holds until it is cleared.set_notifyis the hook for N2's waker. A read is copied once into a buffer sized to it, so an idle connection holds no read buffer.send,send_manyandbroadcast(group, bytes, exclude)copy the bytes once, and each receiver's queue holds a reference. Commands are queued under one lock, so every I/O thread sees them in issue order. A broadcast goes to each thread once, and each thread walks its own share of the group. What reaches one connection in one turn of its thread goes out in one gathered write.max_queued, the connection closes withslow_reader.want_writable(conn, low_mark)asks for onewritableevent.close(conn, flush). A flushing close ends our side once the output is out and postsclosedthen. A small lingerer reads and drops input until the peer ends its side (bounded byflush_timeout), so unread input does not turn the close into a reset that loses the output.joinandleave; a close leaves every group.stats()reports per engine, per listener and per thread: open, accepted, closed by reason, bytes and frames in, bytes and messages out, broadcasts and receivers, queued bytes (now and the maximum), and busy time. Each thread writes only its own block;stats()sums them.available()is false andlisten()fails.Seams for later parts:
listener_optionsandconn_infosay where N3's TLS fields go. A connection's socket I/O is three calls (wait for readiness,read_some,write_some) for N3 to wrap. N4's client connections would reuseconnectionwith a connect in place of the accept.What the design got wrong (the doc is fixed in this PR)
Switching framing "between frames" races with read-ahead. The I/O threads split input as it arrives, so a switch the program makes after seeing a frame comes too late for bytes the peer sent right behind it. NMDC's
$ADCSNDand the file's bytes arrive in one write. Two framing rules were added:pause_after: frame prefixes after which the engine stops splitting and reading that connection until the nextset_framing.raw(n):nbytes, then the same pause.NetEngineFraming.APausedConnectionSwitchesFramingExactlyAtTheFramesends$Hello|$ADCSND … 16|plus 16 binary bytes containing|, plus$Next|$Last|, all in one write. The test gets each part exactly.§4's "under 10 ms of I/O time for a message to 6000" is below the floor here. On this machine one
write()to a loopback TCP socket costs 16.5 µs on its own, because the sender's syscall also runs the receiving side's stack. So 6000 receivers cost about 100 ms of CPU, however the engine is written. The engine's own overhead is about 1 µs a receiver. §4 now has the measured numbers and what they mean for N2's soak.A flushing close waited for the peer's FIN before telling the program. Now the program hears at once and the lingering goes on in the background (§2.4).
Fan-out microbenchmark
NetEngineFanOut.OneMessageToManyLoopbackReceivers: 6000 loopback connections, 113-byte line, 21 rounds after a warm-up, medians. Run on prettyhatemachine (Xeon E5-2650 v2 @ 2.6 GHz, 32 threads, Linux 7.0, retpolines) with the machine otherwise idle. The test's two reader threads run on the same box.broadcast()callwrite()s to the same 6000 sockets: 99-103 ms, 16.5 µs a write, the same at N=100.CVC_NET_FANOUT_NandCVC_NET_FANOUT_THREADSset N and the thread count. Under TSan, N is 300; on Windows the default is 1000 (see below).Tests
net_engine_test(23 cases) covers:set_framing;leave, and a close leaving its groups;send_many;writableat the mark;stop();The sanitizer lanes (tsan, asan-ubsan, clang-tsan) run it, and
SAN_REQUIRED_NETmakes 8 of its cases required in each leg.Local runs:
-fsanitize=thread, with no reports.Not done here, follow-ups
async_wait) and then read into the thread's buffer, which is what keeps idle connections free of read buffers. On Windows, Asio implementsasync_waitwith aselect()reactor thread beside IOCP, which is portable but slow with thousands of sockets. A Windows read path withasync_read_someand per-connection buffers is the fix if Windows hubs matter. It builds and should pass there; CI will say.Do not merge without Joe's OK.