Session rollover interrupts active asynchronous predictions

perfloop-oss/truss · Premature Release

https://perfloop.ai/t/oss/case_vzs4z2j77y

Verdict

VERIFIED · settled 2026-11-20 · pull request opened as basetenlabs/truss#2807

What happened: The check fails before this change and passes with it.

Hypothesis

Session rollover closes connections that an active asynchronous prediction still owns. This can turn a healthy remote response into a connection failure or an extra retry. How often deployed calls overlap rollover remains unmeasured.

`StubBase.predict_async` holds `_client_async()` across `client.post`, error checking, and `response.read()` at lines 391–396. When another call finds the cached session older than 3600 seconds, `_client_async` schedules `_safe_close` on that session. `_safe_close` immediately calls `session.close()`; its timeout bounds closing rather than delaying it until active holders finish. The first request has not completed or been cancelled when this happens.

A new temporary native pytest file, `truss-chains/tests/test_session_rollover_hunt.py`, exhaustively compared two bounded schedules: two loopback RPCs with no rollover, and the same calls with rollover while RPC 1 awaited its response. Events held RPC 1 until RPC 2 completed. Both used `retries=1`, `timeout_sec=5`, and `concurrency_limit=2`; only the cached creation timestamp changed. `uv run --python 3.13.5 --frozen pytest truss-chains/tests/test_session_rollover_hunt.py -vv -s --tb=short` reported `1 failed, 1 passed`: without rollover both results were correct; with rollover RPC 1 returned `ServerDisconnectedError('Server disconnected')` and RPC 2 returned its correct result. Replaying only the rollover parameter with `-k 'rollover and not no-rollover'` reported `1 failed, 1 deselected` with the same error. This establishes one deterministic supported event order, not production prevalence or all interleavings. Python 3.13.5 was selected because the repository-pinned 3.13.17 was unavailable.

The independent proof target is that overlapping ordinary `predict_async` calls retain valid sessions through response reading across age-based replacement, and retired sessions close after their final borrower exits. Include the no-rollover control and preserve error, retry, timeout, and cancellation behavior. The temporary test layout is provisional, not an extra required implementation design. Confidence is medium; the witness is local and the wider lifetime proof remains Case work.

The current open-PR listing was read through its final page. Actual patches in https://github.com/basetenlabs/truss/pull/2597 transfer streaming-response ownership, https://github.com/basetenlabs/truss/pull/2749 preserve omitted argument defaults, and https://github.com/basetenlabs/truss/pull/2097 change streaming error handling and tracing. None changes cached-session retirement in the inspected implementations. The rollover repair is distinct from those changes. `CONTRIBUTING.md` names GitHub as the review venue. Local path history exposed only the checkout commit, so older merged retirement decisions remain a history evidence gap.

Change to test: Keep a retired async session usable until its last entitled borrower finishes, while routing new calls to the replacement session. Preserve the configured retries, request timeout, concurrency limit, and cleanup after the final borrower exits.

Where it lives

perfloop-oss/truss · truss-chains/truss_chains/remote_chainlet/stub.py

Evidence

The check fails before this change and passes with it: `BasetenSession preserves asynchronous prediction responses and live streaming reads across age-based client replacement and closes retired clients after their final borrower exits. Scope: healthy loopback responses, two waves of two predict_async calls with and without rollover (retries=1, timeout_sec=5, concurrency_limit=4); three predict_async callers with concurrency_limit=1 including a queued borrower that completes or is cancelled; active prediction cancellation; and a returned predict_async_stream reader at the default request timeout. All schedules use one event loop and positive request deadlines.`

The check fails before this change and passes with it: `After a streaming prediction fails with ClientPayloadError during age-based session replacement, BasetenSession releases its streaming borrower and shut_down awaits the retired session close even while the returned RPC frame is retained. Scope: one length-delimited loopback stream with a valid prefix and truncated body, one replacement predict_async call, default RPCOptions, and one event loop.`

asynchronous prediction · 17 sample pairs

metric baseline candidate paired median change confidence range required result
latency_p50_ms 503 503.1 +0.08374 −0.4535 to +0.4634 loss ≤ 0.05 × |baseline median| PASSED
latency_p99_ms 503.9 503.9 +0.08455 −2.002 to +0.4005 loss ≤ 0.05 × |baseline median| PASSED
connections_held 4 4 0 0 to 0 loss ≤ 0.05 × |baseline median| PASSED

streaming prediction · 17 sample pairs

metric baseline candidate paired median change confidence range required result
peak_traced_bytes 283972 284148 +0.1% (+151) +42 to +306 loss ≤ 0.05 × |baseline median| PASSED

Checks: 4 of 4 passed. Verification: no defect found.

Timeline