Skip to content

Ariadne: ws-open and ws-recv don't stall the frame loop in single-threaded wasm - #925

Merged
transfix merged 19 commits into
masterfrom
feat/ariadne-ws-nonblocking
Nov 13, 2026
Merged

transfix merged 19 commits into
masterfrom
feat/ariadne-ws-nonblocking

Conversation

@transfix

@transfix transfix commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

#914 (L4) and #923 (WebSocket verbs) have both merged, and master is merged in here: the diff against master is only this PR's change (the non-blocking ws path, ed4191d4's content, plus the curl-backend race fix e85e1fc).

In the single-threaded wasm pages, ws-open and ws-recv froze the frame loop, as http-get did before #914:

This moves both onto #914's path. The split was agreed with the session that owns #923 ("Cypkg project setup"): it stays out of WebWsConnection's queue and callbacks meanwhile and keeps #923 as is.

What changes

  • cvc::net:
    • ws_start(url, opts, done) and WsConnection::recv_async(timeout, done), plus ws_starts_async();
    • a WsConnector interface for a transport in place of the compiled backend. The verbs take one in NetAccess::ws (NetHostConfig::ws for enable_net), so the started path is testable natively. There is no process-wide connector;
    • the browser backend in a build without threads answers from the WebSocket's own callbacks and emscripten_set_timeout timers;
    • the returned stop function abandons a connect (closing the socket, a still-connecting one too) or drops a wait;
    • elsewhere both wait in place, exactly as before.
  • Verbs: ws-open and ws-recv return a state_exec async_work (Ariadne L4: requests in single-threaded wasm don't stall the frame loop #914) when ws_starts_async().
    • The program parks, frames go on, and the callbacks post the reply.
    • A killed or timed-out call abandons its connect or drops its wait (on_cancel).
    • ws-open's checks, credentials and handle table are shared by the kernel and the started path (build_ws_open / prepare_ws_open / finish_ws_open).
    • ws-poll, ws-send, ws-close, ws-open? never waited and are unchanged.
  • A browser fix to Ariadne: WebSocket client verbs (ws-open, ws-send, ws-poll, ws-recv, ws-close) #923: WebWsConnection::close sends 1000 for a code a page may not send. WebSocket.close throws on anything but 1000 and 3000–4999, and the file used 1001 (destructor) and 1013 (too many sockets).

Docs: ARIADNE.md §10.5.

Tests

  • Native AriadneNetL4, with a deferred connector and connection that the test answers between frames:
    • WebSocketOpenAndRecvAreStartedWhileFramesGoOn: ≥20 on_tick frames during the connect and again during the wait; then ws-send and the message.
    • ATornDownDocumentAbandonsItsConnectAndItsWait.
    • Mutation check: forcing ws-open back to its kernel fails both.
    • Ariadne: WebSocket client verbs (ws-open, ws-send, ws-poll, ws-recv, ws-close) #923's real-server tests (WsVerbsRoundTripThroughAPolicy, WsOpenRefusesAnOriginThePolicyDoesNotAllow) and net_ws_client_test still pass: the native path is unchanged.
  • wasm (single-threaded, emsdk 5.0.7, under node 22's WebSocket): new tests/l4_ws.ari against tests/ws_probe_server.py (stdlib only; a 1 s handshake, then one message 1 s later).
    • With this change: 354 frames while connecting, 374 while waiting, message received, clean exit.
    • With the blocking path forced: 0 and 0.
    • CI's ari terminal backend (wasm, node) job gets this as a step and fails under 20.
    • The HTTP checks still pass on this branch: net_probe.ari unchanged; l4_frames.ari counted 186 frames while the request was out.

Brought up to master (2026-11-10)

Master moved ws-open and ws-recv on meanwhile (#926, #928): subprotocols and an auth_protocol credential, credentials in ws-send, and tokens blanked in what ws-recv/ws-poll return. The split is put back on top of that:

  • build_ws_open / prepare_ws_open / finish_ws_open carry the subprotocols and the auth_protocol token and file a WsEntry with the credentials it sent.
  • WsPrepared wipes its tokens when it goes, so an abandoned started connect leaves none behind.
  • The started ws-recv scrubs its event as the kernel does. The browser's ws_start checks and offers the subprotocols as ws_connect does.

The macOS red: libcvc linked the SDK's libcurl there, which has no WebSocket support. So have_ws_backend() was false, the ws-* verbs were never registered, and the two AriadneNetL4 WebSocket tests never reached their connector. The verbs are now registered when the access has a transport or the build has a backend. #1145 (merged) also links cvcpkg's libcurl on macOS, so the real-server tests run there too.

No singleton (2026-11-12): an earlier revision kept the connector process-wide (set_ws_connector) and put its cell on the writable-data scan's allowlist. It now travels in NetAccess, and the allowlist is master's.

Locally (Windows, static Release): AriadneNetL4, AriadneNetHost, AriadneNetIntrinsics and NetWsClient pass, and so does every other built case under ctest.

nicotina-ci added 7 commits October 30, 2026 16:46
In a browser build without threads the background pool's jobs run on the page's main thread,
so an http verb's kernel ran inside the frame pump and spun on emscripten_sleep(0) until the
reply came: nothing drew or took input for the whole request (100-300 ms, up to the 30 s
timeout).

- cvc::net: start(req[, policy], done) -> HttpTransfer (abort()), and http_starts_async().
  The browser fetch backend issues an emscripten_fetch and returns; onsuccess/onerror deliver
  the reply from the event loop. It reports a timeout as one, enforces the 64 MiB cap as the
  body arrives, and aborts cleanly (no callback after abort). Elsewhere start() sends in place.
  The policy check follows redirects hop by hop, as send(req, policy) does (shared next_hop).
- state_exec: async_work {kernel, start} and a register_async_fn overload. Where the call can
  park, start() begins the work and the call is an external one with the registration's
  timeout and charging; the work settles and posts it. async_call::on_cancel(stop) is how a
  kill, a timeout or the host's shutdown stops it. Elsewhere the kernel runs as before.
- The net verbs (http-get, http-request) return an async_work when the transport starts
  requests without blocking: credentials go in and captures come out in C++ around the
  started request (perform split into prepare/complete). fetch of an http(s) URI uses the
  http handler's new non-blocking form (register_uri_async_handler / resolve_async); a host's
  own handler for the scheme drops it.
- AppRuntime::idle() counts the scheduler's calls in flight (started ones included), and
  settle() yields to the browser's event loop in wasm, so ari snapshot waits for the reply.

Tests: AsyncCalls started-call cases (park, timed await slices, errors, kill, timeout,
shutdown, kernel fallback), NetStart (in place, later, policy hop by hop, abort),
AriadneNetL4 (frames go on while a program is parked on a request, await slices, teardown
aborts, credentials and capture on the started path, policy refusal, fetch). CI: the wasm
ari's node job runs tests/l4_frames.ari against a 1 s endpoint and fails unless frames ran
while the request was out (218 here; 0 with the blocking path).

Docs: ARIADNE.md 10.2, STATE_EXEC_EXCEPTIONS.md 3.12.
…ws-close)

A document can follow a server that pushes changes instead of polling it.
cvc::net gets a WebSocket client (ws_client.h) with two backends: libcurl's
WebSocket API natively, so wss:// has the same TLS and proxy handling as
https://, and the browser's WebSocket under Emscripten. Messages arrive whole
in a queue; ws-poll takes what is there without waiting, for on_tick loops,
and ws-recv waits on the background pool. The host's policy checks a ws(s)
URL as its http(s) twin, and auth/headers/credential values work as for the
HTTP verbs (natively; a browser WebSocket sends no headers).

Documented in ARIADNE.md 10.5, with client tests against a Beast server and
verb tests in ariadne_runtime_test.
…eaded wasm

#923's browser WebSocket waited in loops on emscripten_sleep(10): WebWsConnection::wait_open
inside ws_connect (up to the 15 s connect timeout) and recv (up to ws-recv's seconds). Without
-pthread those kernels run on the pump thread, so ws-open and ws-recv froze the page, as http-get
did before #914. This moves them onto #914's path.

- cvc::net: ws_start(url, opts, done) and WsConnection::recv_async(timeout, done), plus
  ws_starts_async() and a set_ws_connector seam (WsConnector), as set_http_client is for HTTP.
  The browser backend in a build without threads answers from the WebSocket's own callbacks
  (on_open / on_error / on_close / on_message) and emscripten_set_timeout timers; the returned
  stop function abandons a connect (closing the socket, a connecting one too) or drops a wait.
  Elsewhere both wait in place, as before.
- ws-open and ws-recv return a state_exec async_work (#914) when ws_starts_async(): the program
  parks, frames go on, and the callbacks post the reply; a killed or timed-out call abandons its
  connect or drops its wait (on_cancel). ws-open's checks, credentials and handle table are
  shared by the kernel and the started path (build / prepare / finish_ws_open).
- WebWsConnection::close sends 1000 for a code a page may not send (WebSocket.close throws on
  anything but 1000 and 3000-4999, the 1001 and 1013 this file uses included).

Tests: AriadneNetL4.WebSocketOpenAndRecvAreStartedWhileFramesGoOn and
ATornDownDocumentAbandonsItsConnectAndItsWait (a deferred connector and connection the test
answers between frames; forcing ws-open back to its kernel fails both). CI: the wasm ari's node
job runs tests/l4_ws.ari against tests/ws_probe_server.py (stdlib; a 1 s handshake, then a
message 1 s later) and fails unless 20 frames ran during each wait: 354 and 374 here, 0 and 0
with the blocking path.

Docs: ARIADNE.md 10.5.
@transfix

Copy link
Copy Markdown
Owner Author

Local native ctest on this branch (Linux GCC Release; filter Ariadne|ari|Net|net|Ws|ws|AsyncCalls|async): 957/958 passed.

The one failure, AsyncCalls.StepCostSetsTheDefaultRate, is a step-rate measurement and not related to this change. It checks master's kDefaultAsyncStepsPerCpuMs (8000, the same on #914 and #923) against 4× the fastest measured rate. This machine was at load average ~40, so it measured 1200–1700 steps per CPU ms, and 4× the fastest program was 6871 < 8000. It passed in #914's run here at lower load. Everything else passed, including the new AriadneNetL4 WebSocket tests, #923's real-server ws tests and net_ws_client_test.

nicotina-ci added 2 commits October 30, 2026 20:08
@transfix transfix changed the title Ariadne: ws-open and ws-recv don't stall the frame loop in single-threaded wasm (stacked on #914, #923) Ariadne: ws-open and ws-recv don't stall the frame loop in single-threaded wasm (stacked on #914) Oct 31, 2026
…r is queued

The curl backend's reader pushed the Error event and then called close(1009), which clears
open(). A program (or NetWsClient.AnOversizedMessageIsAnError) that took the error at once could
still see open() true: the Windows recipe lane failed on it. Close first, as the peer-close paths
already do, so open() is false from the last event on (docs/ARIADNE.md 10.5).
@transfix

Copy link
Copy Markdown
Owner Author

The recipe / windows-2022 / Release failure was NetWsClient.AnOversizedMessageIsAnError (#923's test, now on master): c->open() was still true after the oversize error event arrived. It was a race in the curl backend's reader: it queued the error, then called close(1009), which is what clears open(). Fixed in e85e1fc by closing first, as the peer-close paths already do. Locally: that test 300/300 with --gtest_repeat, net_ws_client_test 8/8.

…blocking

# Conflicts:
#	.github/workflows/ci.yml
#	src/ari/CMakeLists.txt
#	src/cvc/tests/ariadne_runtime_test.cpp
@transfix transfix changed the title Ariadne: ws-open and ws-recv don't stall the frame loop in single-threaded wasm (stacked on #914) Ariadne: ws-open and ws-recv don't stall the frame loop in single-threaded wasm Oct 31, 2026
transfix pushed a commit that referenced this pull request Nov 6, 2026
…ter, without #925)

This rebuilds the branch on master, which now has #923, #914 and #926, and drops
the #925 merge: #925 (feat/ariadne-ws-nonblocking) is still open and fails its
own AriadneNetL4.WebSocket* tests on macOS, which also failed this PR. This
commit's tree is master plus the feature alone; its first parent keeps the
branch's earlier history, so nothing is rewritten.

A browser's WebSocket can send no request headers, but it can offer
subprotocols, so a credential there is the one handshake auth a document in the
wasm page can use. ws-open takes two new options:

- "protocols": a list of subprotocols to offer (Sec-WebSocket-Protocol), each an
  HTTP token.
- "auth_protocol": (dict "credential" NAME ["prefix" P] ["encoding" "raw"|
  "base64url"]) offers the host's token for NAME, fetched for the URL's origin as
  for auth, as one more subprotocol: P then the token, or its unpadded base64url
  (a token68's '/' or '=' can't be in a subprotocol), as Kubernetes does with
  "base64url.bearer.authorization.k8s.io.".

cvc::net: WsOptions::protocols is sent natively as a Sec-WebSocket-Protocol line
and in the browser through createAttributes.protocols. Both fail the connect
unless the server picks one of the offered subprotocols, as browsers do
(RFC 6455 4.1; natively from the 101's headers, which libcurl files under
CURLH_1XX). An invalid or repeated subprotocol is refused, with no error
quoting one, and the joined strings are wiped. ws_valid_protocol is public for
the option checks.

The token is wiped after the connect, and errors are redacted of it and of its
base64url spelling. The connection records the name, so ws-recv and ws-poll
blank either spelling if the server echoes it.

Tests: NetWsClient covers offering, a server that picks none or another
subprotocol, and invalid subprotocols. AriadneNetHost.
WsOpenOffersTheHostsTokenAsASubprotocol checks the handshake line, the
redacted echo, a raw token with '/' that is refused without connecting, and that
no state node holds the token. LocalWsServer can pick a subprotocol. Spec:
docs/ARIADNE.md section 10.5.
Master moved ws-open and ws-recv on since this branch began: subprotocols and an
auth_protocol credential (#928), credentials in ws-send and blanked in what
ws-recv and ws-poll return (#926), connections kept as WsEntry with their origin.
The non-blocking split is put back on top of that:

- build_ws_open takes master's protocols and auth_protocol; prepare_ws_open puts
  the auth_protocol token in as a subprotocol; finish_ws_open files a WsEntry
  with the names of the credentials it sent. WsPrepared wipes its tokens when it
  goes, so a started connect that is abandoned leaves none behind either.
- ws-recv's started wait scrubs the event as the kernel does. Its callback keeps
  the origin and the sent names, not the connection that holds the callback.
- The browser's started connect (ws_start) checks and offers the subprotocols as
  master's ws_connect does.

And the macOS failure: libcurl there has no WebSocket support, so
have_ws_backend() was false, the ws-* verbs were never registered, and
AriadneNetL4's two WebSocket tests never reached their connector. A connector
set_ws_connector puts in place now counts as a backend; the libcurl connect
still checks only what libcurl was built with.
set_ws_connector's process-wide connector (connector_cell, connector_mutex)
is the WebSocket twin of set_http_client's client_cell/client_mutex, which
the allowlist already carries: a test or host swaps the transport for the
whole process, as it does for HTTP.
…blocking-925

# Conflicts:
#	tools/singleton_scan/allowlist.txt
set_ws_connector kept one WebSocket connector for the whole process (a
function-static cell and its mutex), the kind of state P0a removes; this branch
had put the cell on the writable-data scan's allowlist. It is gone:

- NetAccess::ws is the transport the ws-* verbs connect through (a test's that
  it answers between frames, or a host's own); null is the compiled backend.
  NetHostConfig::ws hands one to enable_net.
- cvc::net::WsConnector stays, as the interface such a transport implements;
  ws_connect, ws_start and ws_starts_async are the compiled backend only, and
  have_ws_backend() is again what the build has.
- The verbs are registered when the access has a transport or the build has a
  backend, so the AriadneNetL4 WebSocket tests run where libcurl has none.
- The allowlist is master's again (cvc 451).
@transfix
transfix merged commit 8b1ddfb into master Nov 13, 2026
33 checks passed
@transfix
transfix deleted the feat/ariadne-ws-nonblocking branch November 13, 2026 20:55
transfix added a commit that referenced this pull request Nov 13, 2026
…erf/threaded-s1b

#925 starts ws-open's connect and ws-recv's wait where the transport does not block (the browser
without threads), and their callbacks settle the call. Its start paths kept the async_host by
reference and posted through it; here they take the host's finisher (host.make_finisher()) and
settle_started through it, as http_work and uri_fetch_work do: the callbacks answer later, and an
isolated document's group's host may be gone by then.

- net_intrinsics.cpp: the conflict kept this branch's providers by kind; have_ws takes master's
  NetAccess::ws. The any provider's comment says a host's own WebSocket transport must be as
  thread-safe as the compiled backend.
- ariadne_runtime_test: both sides' new L4 cases are kept, plus
  AStartedWebSocketCallKeepsTheHostItStartedOn, the WebSocket twin of
  AStartedRequestKeepsTheHostItStartedOn.
transfix added a commit that referenced this pull request Nov 13, 2026
Conflicts with #925 (ws-open and ws-recv started, not blocking, in
single-threaded wasm):
- net_intrinsics.cpp: master's ws_open_work, prepare and finish steps,
  with the app's WsTable passed down (finish_ws_open and ws_open_work
  take it; the kernel, the start path and the connect callback hold it)
  and ws-recv looking up in it. register_net_intrinsics is this branch's,
  with master's have_ws (a NetAccess::ws connector counts).
- net_host.cpp: disable_net(app), and NetAccess carries master's
  config.ws.
- ariadne_runtime_test.cpp: master's DeferredWsConnection,
  DeferredWsConnector and ws_allow; L4Guard stays gone, as each app's
  net host and HTTP client go with it, so the two new WebSocket tests
  drop it.
transfix added a commit that referenced this pull request Nov 13, 2026
…rkers

No conflicts. Master's new net code (#925's started ws-open and ws-recv,
settle_started, the app's WsTable) registers async work through the same
async_work marshals and start lambdas, which take no context; nothing it
calls changed in this branch, so it merges as it is.
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