Skip to content

Lease results to the session in place with typed-lease - #464

Draft
johnsabath wants to merge 4 commits into
feat/typed-codec-linkfrom
feat/typed-codec-lease
Draft

johnsabath wants to merge 4 commits into
feat/typed-codec-linkfrom
feat/typed-codec-lease

Conversation

@johnsabath

@johnsabath johnsabath commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Why

With REACTOR_MODEL_SERVER_CODEC=typed, the session copies each array of a
result out of the model server's shared memory. For large results, that copy
is most of the step's round trip. This PR adds an opt-in mode that skips it.

What Changed

REACTOR_MODEL_SERVER_CODEC=typed-lease, set on the model server and on every
session runtime, works as typed, except for results. The model server
writes each array of a result into its own span of its output block. The
session gets read-only arrays over those spans. When the last view of an
array is gone, the session sends a release, and only then can the model
server write that span again. Requests are still copied.

A result that fits the block, but finds no free span within 2 seconds, fails
with BlockFull. That request fails alone, and the link goes on. A release
the session cannot make, or a leased array in a request, closes that link.

ModelClient's reader thread no longer keeps the last message while it waits
for the next one, so a result its caller dropped is freed at once.

Surface Before After
REACTOR_MODEL_SERVER_CODEC pickle or typed adds typed-lease
reactor_runtime.model_server.BlockFull none raised by ModelClient.step() with typed-lease
ModelClient.step() result arrays the caller owns with typed-lease, non-empty arrays are read-only
Link records rr.ShmHello adds rr.ShmLeaseHello and rr.Release, sent only in typed-lease mode
Typed frame, array place inline or in block adds leased, with a hold id; refused unless the link agreed on typed-lease
reactor_runtime.ipc.lease none new: Spans, leased_array, LeaseReader

An end in typed-lease against an end in any other mode, or against an older
runtime, fails at connect with the variable named.

Side effects

An application that keeps results keeps the model server's memory. Size
model.resources.sessions.shared_memory.output for what it keeps, or keep
np.array(x) copies. Echo keeps up to 29 frames for its burst. An array kept
past the link's close keeps that block in the slot's volume. An in-place op
on a tensor made with torch.from_numpy ends the session process.

split_echo now runs under any codec, and with typed-lease it requires
fewer held spans than one burst after each cycle. CI still runs it with the
default codec.

How it was proved

Each invariant has a test, and each guard was checked by mutation: 17
mutations, each failed a test. The reader-thread test fails without its fix.
A 15-cycle split_echo run with typed-lease passes, and fails when the
model server ignores releases. An older runtime is simulated by unregistering
the new hello, not run.

Round trip in one CPU-only Linux container, 1,000 steps, median p50 of 3 runs (µs):

Payload typed typed-lease
0.46 MB fp16 223.1 230.6
2.76 MB uint8 442.3 393.8
25.2 MB uint8 2370.1 1829.7

Authored lines: 646 in src (code and docstrings), 777 in tests and the endurance suite.

The contract for typed-lease: the setting's new value, what a session gets as a result's arrays, BlockFull, and how kept arrays count against the slot's volume. The link does not implement it yet.

Signed-off-by: John Sabath <john@reactor.inc>
With REACTOR_MODEL_SERVER_CODEC=typed-lease, the model server writes each
array of a result into its own span of the output block. The session gets
read-only arrays over those spans and copies nothing. When the last view
of an array is gone, the session sends a release, and the model server can
write that span again.

A result that finds no free span within 2 seconds fails with BlockFull.
The request fails alone, and the link goes on. A release the session
cannot make, or a leased array in a request, closes that link.

The session's reader thread no longer keeps the last message while it
waits for the next one, so a result its caller dropped is freed at once.

Signed-off-by: John Sabath <john@reactor.inc>
- A leased array still alive at interpreter exit is not released, so an
  exit handler can still read it.
- The session's lease reader takes a reentrant lock, so a collection
  that runs inside it cannot deadlock on a release.
- The model server applies a release frame all or none.
- A release frame stays inside the link's frame limit.
- A release that cannot be sent closes the session's link.
- A refused lease leaves no live array counted.

split_echo now runs under any REACTOR_MODEL_SERVER_CODEC: each half
registers the echo's step types. The model server probe reports the
result spans it holds. With typed-lease, they must stay below one burst
after every cycle, because echo keeps a partial burst of frames.

Signed-off-by: John Sabath <john@reactor.inc>
…tests rely on

Signed-off-by: John Sabath <john@reactor.inc>

Copy link
Copy Markdown
Contributor Author

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@johnsabath johnsabath changed the title Document leased results on the model server link Lease results to the session in place with typed-lease Dec 5, 2026

This branch has not been deployed

No deployments
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