Asupersync — Cancel-Correct Async for Rust

Asupersync

13 min read Original article ↗
v0.6.2 on crates.io · newer commits on main

The
Cancel-Correct
Async Runtime.

An async runtime for Rust. Every task belongs to a region, and a region doesn't close until everything in it has finished. Cancelling a task asks it to stop instead of dropping it mid-poll. Effects go through an explicit capability context, and a lab runtime replays any schedule from its seed.

What the runtime
keeps track of

Most executors leave task ownership, cancellation, and cleanup to convention. Asupersync makes them part of the runtime: who owns each task, what it still has to finish, and what it's allowed to do. These guarantees cost time per task, and they're enforced by the runtime rather than proven by the type system.

No orphan tasks

Every task you spawn through a CxCxThe capability context every async function receivesCx is how a task spawns children, checks for cancellation, reads its budget and the current time, draws randomness, and records traces. It belongs to a region, so anything spawned through it is owned by that region. A test can hand in a lab Cx and the same code runs on virtual time. belongs to that context's regionRegionThe scope that owns tasksEvery task belongs to a region, and regions nest into a tree. Closing a region cancels whatever is still running in it, waits for those tasks to finish, runs finalizers, and resolves obligations before it reports done. Tasks spawned through a Cx belong to that Cx's region; tasks spawned through a RuntimeHandle belong to the root region., and regions nest into a tree. Closing a region cancels whatever is still running in it, waits for those tasks, runs finalizers, and resolves outstanding obligations before it reports done.

There's no detached spawn. Tasks started from a runtime handle belong to the root region and are drained at shutdown. Click a region in the demo to watch a close cascade down the tree.

Cancellation is a request

Dropping a future stops it at its last await, whatever it was in the middle of. Here, cancelling a task asks it to stop. It keeps running until it reaches a cancellation pointCancellation PointWhere a task notices it has been cancelledcx.checkpoint() returns an error once cancellation has been requested, and cancel-aware awaits such as channel receives and lock acquisitions return early the same way. A task that never reaches one is never forcibly stopped, and it holds up its region's close. Loops that do real work should call cx.checkpoint()., drains its own work (it can still await, and it can still return a value), and then its finalizers run. The outcome records Cancelled(reason) with the cause chain.

The flip side: a task that never checks in is never forcibly stopped. It holds up its region's close. Cleanup budgetsBudgetDeadline, poll quota, cost quota, and priority for a piece of workA small Copy struct: an optional absolute deadline, a poll quota, an optional abstract cost quota, and a priority from 0 to 255. Scopes and tasks carry one, and nested budgets combine with meet, so the tighter constraint always wins. On the production runtime, cleanup budgets are advisory: a task that runs past its budget is not killed. are advisory on the production runtime, and Runtime::shutdown_timeout bounds how long you wait.

Reserve, then commit

If a task is cancelled halfway through sending a message, the message shouldn't be lost or half-sent. Channel sends are split in two: tx.reserve(&cx).await? claims capacity and commits nothing, and permit.send(v) publishes. Cancelled while reserving? Nothing happened. Dropped the permit? The slot is released.

The demo uses a bank transfer as the analogy. In the library, the pattern covers channels, network sends, and obligation tokens. It isn't a transaction system: partial I/O like write_all documents its own weaker contract.

Permits the runtime can count

Reserving a channel slot, taking a semaphore permit, or holding a lease records an obligationObligation SystemThe runtime's table of permits, acks, and leasesWhen a task reserves a channel slot, takes a semaphore permit, or holds a lease through a runtime-built Cx, the runtime records an obligation. Sending, releasing, or aborting resolves it. A region can't close with unresolved registered obligations, and the obligation_leak oracle names any that escape, by kind and holder.. Sending, releasing, or aborting resolves it. A region can't close cleanly while one is outstanding.

Rust can't enforce “use exactly once” at compile time (its types are affine, not linear), so this is runtime bookkeeping: a permit that escapes through mem::forget is reported by the lab's leak oracle, by kind and holder.

Effects go through Cx

Async functions take &Cx. Spawning, reading time, randomness, tracing, and runtime-managed I/O go through it, so a function's signature says what it can do, and handing it a lab Cx runs the same code on virtual time.

A context can be narrowed (Cx::restrict) but not widened again, and plain I/O entry points refuse when the calling task lacks the IO capability. Some host-boundary code, like OS entropy for temp-file names, is documented as outside the boundary.

The lab runtimeLab RuntimeThe deterministic test runtimeRuns async code on virtual time with a seeded scheduler, so the same seed reproduces the same schedule. It captures traces, injects cancellation and chaos deterministically, detects futurelocks, writes crashpacks for failing runs, and checks oracles. Concurrency bugs become reproducible test failures instead of flakes.

Run your async code on virtual time with a seeded scheduler and the same seedSeedThe number that fixes a lab scheduleThe lab scheduler's choices come from a seeded deterministic RNG, so the same seed reproduces the same interleaving, timer order, and chaos injections. A failing seed is a reproducible bug report. reproduces the same interleaving. Sleeps complete instantly, traces are captured, and oracles check every run for leaked tasks, leaked obligations, undrained race losers, and protocol violations.

A schedule explorer derives new seeds from the races it detects and skips schedules equivalent to ones it has seen. It's a bug finder, not a proof of exhaustive coverage, but a failing seed is a reproducible test instead of a flake.

OTP-style actors that can't be orphaned

SporkSporkAsupersync's OTP-style layerSupervision, a name registry, and actors on top of regions, obligations, and explicit cancellation: GenServers, supervisors, links, monitors, process groups, and AppSpec for declarative topologies. Processes always belong to a region and can't be detached, and restart and DOWN-message order is deterministic under the lab runtime. brings Erlang's vocabulary to regions: GenServersGenServerAn OTP-style stateful serverA task that owns state and handles call, cast, and info messages from a bounded mailbox, spawned with cx.spawn_gen_server. Each call hands the server a Reply that wraps a tracked obligation: the server must send or abort it. Dropping it unanswered panics outside of cancellation and unwinding, and the lab's reply_linearity oracle checks the same rule. This is enforced at runtime, not by the compiler., actors with bounded mailboxes, monitors, links, and supervisorsSupervisorRestarts failed children by policyA Spork supervisor compiles a restart topology over regions: boot order, dependencies, and shutdown budgets. Run live with CompiledSupervisor::bind_managed, it cancels and drains a failed child, then restarts it one-for-one, one-for-all, or rest-for-one, within shared intensity and backoff limits. that restart children one-for-one, one-for-all, or rest-for-one. Every process belongs to a region.

Each call hands the server a reply obligation. Dropping it unanswered fails loudly instead of leaving the caller waiting, and under the lab runtime restarts and DOWN messages arrive in a fixed order.

Fountain codes, and a transfer protocol on top

The runtime includes an RFC 6330 RaptorQRaptorQThe RFC 6330 fountain codeA systematic fountain code: the first symbols are the source data itself, and repair symbols can be generated without limit. With K source symbols, receiving K usually suffices and K + 2 almost always does. Asupersync's implementation is deterministic, with a policy-driven decode planner and optional SIMD GF(256) kernels. codec: the sender produces as many encoded symbols as it likes, and any sufficient set rebuilds the data, so packet loss costs bandwidth instead of round trips.

Its main user is ATP, a file-transfer protocol with Merkle-verified commits, resumable journals, and multi-donor pulls. See the ATP page for measurements, including where it loses to rsync.

More in the demos

The testing machinery, the scheduler internals, and the math, each with an interactive visualization and a plain account of what's on by default.

Re-run a test once per await point, cancelling at each one, with oracles checking every run.

Lab oracles and e-processes

Leak, quiescence, and protocol checks, summarized across seeds with an anytime-valid test.

Recognize two schedules as the same behavior up to reordering of independent events.

Derive new seeds from detected races and skip schedules the explorer has already seen.

Nested deadlines and quotas combine with meet: the tighter limit always wins.

HMAC-chained tokens that anyone can restrict and nobody can widen.

Cancel, timed, and ready lanes, with cancel preemption bounded at 16 in a row.

Compensate completed steps in reverse; batch monotone steps without coordination.

Spectral wait-graph health

An on-demand early-warning diagnostic built on the wait graph's Fiedler value.

The formal rules behind the runtime, and what Lean has actually checked.

Simultaneous failures arrive in a fixed order, so a replay is a replay.

The opt-in discounted-UCB1 selector, and why it's off by default.

What maps to what

The APIs are deliberately different: Asupersync trades implicit convenience for explicit cancellation. The concepts still line up.

Every async operation takes &Cx

tokio reads runtime state from thread-locals. Asupersync passes a capability context, so cancellation and budgets flow through the call graph and a function's signature shows what it can do.

No fire-and-forget

tokio::spawn detaches. Here every task lives in a region, and leaving a scope waits for its children. A task that never reaches a cancellation point holds up that close, which is the point: you find out.

Outcome has four variants

Ok, Err, Cancelled(reason), and Panicked(payload), ordered by severity. Combinators aggregate with that order, so a cancellation or panic can't be masked by a sibling's success.

The same producer and consumer. The Asupersync version is longer for three reasons: the send reserves capacity first so a cancelled producer never loses a message, &cx is threaded through every wait, and the producer is owned by the caller's region and joined instead of detached.

What it costs

Every task carries a region membership, a cancellation state machine, and a terminal-result channel, and permits are tracked as obligations. That bookkeeping isn't free.

Spawning a task, a current-thread yield, and a channel round trip are still several times slower than tokio. A yield on four workers is within 2×. Loopback TCP is 1.1–1.7× slower, and the HTTP/1.1 server handles about half of hyper's requests per second.

For most servers, a few microseconds per task disappear next to network and disk latency. If you spawn millions of tiny tasks per second or exchange messages in a tight loop, the difference matters. For fan-out inside one task, fiber::scope costs about what a tokio task does.

How these were measured: both runtimes in one process, release build with default features, p50 per operation at n = 1,000, on 2026-10-05. Ranges span three hosts (16, 10, and 64 CPUs), two of them shared with other builds, so host noise is part of the range. The HTTP rows come from one 64-CPU host and compare against hyper 1.x.

Reproduce with benches/runtime_vs_tokio.rs. The numbers already include this month's cuts: an O(1) region task set, an obligation-free one-call mpsc::send, a LIFO wake slot, and no global lock per dispatch.

What ships today

Asupersync brings its own stack instead of wrapping tokio's. Each card says where that piece stands, because support varies by surface.

Runtime and scheduler

A multi-thread work-stealing scheduler with three lanes (cancel, timed, ready), a current-thread flavor, and an on-demand blocking pool. Linux epoll is the main reactor, with optional io_uring; the BSD and Windows reactors accept fewer interest flags.

Channels and sync

mpsc, oneshot, broadcast, watch, and session channels with reserve-then-send. Mutex, RwLock, Semaphore, Barrier, Notify, OnceCell, and an object pool. Every wait takes &Cx and can be cancelled.

Combinators

join, race, and timeout drain their losers instead of dropping them. Beyond those: quorum, hedge, first_ok, pipeline, map_reduce, circuit_breaker, bulkhead, rate_limit, bracket, and retry.

Fibers

cx::fiber::scope runs futures that borrow from the task's stack concurrently, each with its own cancellation. A fiber costs about what a tokio task does. Fibers share one thread, so reach for tasks when you want parallelism.

Networking

TCP, UDP, and Unix sockets, DNS, TLS 1.2/1.3 through rustls, and WebSocket with an RFC 6455 conformance suite. Under the lab runtime, a virtual TCP stack replaces kernel sockets.

Built in · TLS behind a feature

HTTP and gRPC

HTTP/1.1 and HTTP/2 servers, a pooled HTTP/1.1 client, gRPC, and a small router with extractors, middleware, and SSE. It isn't axum: handlers use Asupersync's own Request and Response types.

QUIC and HTTP/3

Native QUIC and HTTP/3 with no tokio underneath, including a multi-peer listener over real UDP with streaming request bodies. Multi-connection deployment, 0-RTT, migration, and interop with other stacks are still unproven.

Databases

PostgreSQL (binary protocol, SCRAM-SHA-256) and MySQL clients that speak the wire protocol directly over TcpStream, plus SQLite on the blocking pool. Prepared statements, transactions, and connection reuse.

Actors and supervision

Spork, the OTP-style layer: GenServers, actors, monitors, and links that run as tasks in the caller's region. Supervision trees restart failed children one-for-one, one-for-all, or rest-for-one with intensity and backoff limits.

Remote tasks

Region-owned remote spawn over a native TCP + mutual-TLS protocol, with leases that count as obligations, an idempotency store for retries, and saga compensation. Route persistence and WAN reliability are open work.

RaptorQ and ATP

An RFC 6330 fountain codec with a deterministic decode planner. Its main consumer is ATP, the file-transfer protocol: QUIC or TCP, Merkle-verified commits, resumable journals, and multi-donor bonded pulls.

Browser Edition

A wasm32 build with JS/TS packages for the main thread and dedicated workers. In the browser it's a ledger of regions, scopes, and task handles over the host's promises, with fetch, WebSocket, and WebTransport behind capabilities.

Observability

Structured logs carrying task and region IDs, counters, gauges, and histograms with an optional OpenTelemetry exporter, a live task inspector, and diagnostics that explain why a task is blocked or why it was cancelled.

Lab runtime

Virtual time, seeded scheduling, trace capture and replay, deterministic chaos injection, futurelock detection, crashpacks, and race-guided schedule exploration that skips runs equivalent to ones it has already seen.

Runtime comparison

Where Asupersync is stronger, where it's weaker, and where the honest answer is a caveat.

A scope that cleans up after itself

A budgeted scope, a JoinSet that owns its fan-out, and a join that can't return while any child is still running.

Where things stand

Seven phases, from a single-thread deterministic kernel to ongoing hardening. Partial means partial.

Phase 0 · Complete

Deterministic single-thread kernel

  • Regions, tasks, and the cancellation state machine

  • A four-valued Outcome ordered by severity: Ok < Err < Cancelled < Panicked

  • The lab runtime: virtual time, seeded scheduling, trace capture

Phase 1 · Complete

Parallel scheduler and region heap

  • Three-lane work-stealing scheduler: cancel, timed (EDF), and ready

  • Region heap with generation-checked handles, reclaimed when the region closes

  • Optional sharded runtime state with a fixed lock order

Phase 2 · Partial

I/O and protocols

  • epoll reactor with optional io_uring; narrower BSD and Windows reactors

  • TCP, HTTP/1.1, HTTP/2, TLS, WebSocket, gRPC, and database clients

  • Native QUIC and HTTP/3; deployment and interop evidence still open

Phase 3 · Complete

Actors and supervision

  • GenServers, actors, monitors, and links, on the native runtime and in the lab

  • Live supervision trees: one-for-one, one-for-all, rest-for-one

  • Restart intensity and backoff limits

Phase 4 · Core complete

Distributed structured concurrency

  • Region-owned remote spawn over TCP with mutual TLS

  • Leases as obligations, an idempotency store, saga compensation

  • RaptorQ snapshot distribution with quorum recovery

Phase 5 · Partial

Schedule exploration and formal tooling

  • Race-guided seed exploration that skips equivalent traces

  • TLA+ export of lab traces, checked by TLC

  • Lean checks six model invariants; there is no Rust refinement proof yet

Phase 6 · Ongoing

Hardening

  • Benchmark, golden-output, flamegraph, and proof-note gates before each commit to main

  • Browser Edition packages, currently a release candidate

  • Cutting per-task overhead, measured against tokio in the same process

Read the architecture guide

Try it

The first program needs one import and an attribute. Default features build on stable Rust 1.95 or newer.