Repository navigation
feat(runtime-host): add full-duplex connection sessions - #1350
Conversation
Astro-Han
left a comment
There was a problem hiding this comment.
Approving. The connection seam is in good shape: I verified the body's claims against the code and by running the suite (full runtime-host suite green, plus a 21x soak of the two new suites), and the high-risk paths hold up under tracing: pause/resume pairing, the dual inbound budgets, exactly-once writer receipts, pre-Ready handler resolution, and the admission/disconnect races. One P2 that should be fixed or explicitly tracked before this becomes the frozen seam, plus a few items this slice owes the next ones.
P2: clean EOF leaves read() pending forever on a half-open socket.
On end, #ended is set and #drainInbound() flushes the decoder and sets #decoderEnded, but no terminal state is ever established (framed-transport.ts:57, :163). A later read(0) finds an empty queue and no failure, so it installs a waiter with no timeout that nothing will ever wake (framed-transport.ts:68). I reproduced this with a socket stub: after a clean end with the queue drained, read() stays pending with no timer.
Today both sides use Node's default allowHalfOpen: false, so the close event rejects the waiter and masks this. But this PR positions the connection layer as the seam later slices must not renegotiate, and the EOF terminal state is exactly the kind of semantics that should be pinned here: the body already defines EOF ordering for a trailing partial frame, while clean EOF is left undefined. The first slice that enables allowHalfOpen to drain host-side responses will hit it. The fix is small (establish a terminal state once the decoder drains at EOF); whether EOF means immediate teardown or drain-responses-then-close is a product call that belongs to you.
Owed to the next slices (not blocking):
- Teardown is reason-less on both sides. Overload, duplicate request IDs, and protocol violations all surface to the client as an undifferentiated close, with nothing recorded host-side. When the continuity slice implements the
safe/semanticretry policies, it cannot distinguish "evicted for overload" (retryable on a fresh connection) from transport loss. A close frame or structured host-side record would settle this. - Seal-before-flush leaves a response-loss window: command residency releases when the response is computed, not delivered, so shutdown can destroy the transport inside the grace window and drop a response whose effects already settled. That matches the documented "Host-owned and settles" intent, but the continuity slice needs an outcome journal to close it.
- The writer's 2 MiB byte bound has no in-repo test (I probe-verified the mechanism: it trips before the 64-frame bound and settles receipts exactly once). The first slice with large payloads inherits an untested guardrail.
Test evidence gap worth fixing while cheap:
The "non-reading Client overload" test likely closes the connection via the 65th in-flight request rather than the writer's frame/byte budget, since the loop floods requests as fast as it can. It proves isolation, but not that the writer budget is the eviction cause. Pinning the close reason (or throttling ingress below the in-flight cap) would make it prove what the body claims.
Nothing else turned up: build, typecheck, biome, git diff --check, and the behavior-level assertions in the new tests all check out.
简体中文
通过。connection seam 的整体质量不错:我把 PR 描述里的声明逐条对照代码并跑测试验证过(runtime-host 全量套件通过,两个新套件另做 21 轮 soak),高风险路径在逐行追踪下都成立:pause/resume 配对、双 inbound 预算、writer receipt 恰好一次 settle、pre-Ready 的 handler 解析、admission/disconnect 竞态。有一个 P2 应该在这个 seam 冻结前修复或明确跟踪,另有几项是这个 slice 欠后续 slice 的。
P2:干净 EOF 会让 read() 在 half-open socket 上永远悬挂。
收到 end 时,#ended 被置位,#drainInbound() 排空 decoder 并设置 #decoderEnded,但始终没有建立终态(framed-transport.ts:57、:163)。之后的 read(0) 发现队列为空且没有 failure,于是挂上一个无超时的 waiter,再也没有东西会唤醒它(framed-transport.ts:68)。我用 socket stub 复现过:队列排空后收到干净 end,read() 一直 pending。
今天两侧都用 Node 默认的 allowHalfOpen: false,随后的 close 事件会拒绝 waiter,把问题掩盖了。但这个 PR 把 connection 层定位为后续 slice 不得重新协商的 seam,而 EOF 终态恰恰是应该在这里钉死的语义:PR 描述已经定义了尾随 partial frame 的 EOF 顺序,干净 EOF 却是未定义的。第一个为了排空 host 端响应而打开 allowHalfOpen 的 slice 会 directly 踩中。修复很小(EOF 时 decoder 排空后建立明确终态);EOF 意味着立即 teardown 还是先排空响应再关,是产品语义,由你定。
欠后续 slice 的(不阻塞):
- teardown 两侧都没有原因信息。过载、重复 request ID、协议违例对 client 都是无差别的 close,host 侧也没有记录。continuity slice 实现
safe/semantic重试策略时,无法区分"因过载被驱逐"(可在新连接上重试)和"传输层断开"。一个 close frame 或 host 侧结构化记录可以定下这件事。 - seal-before-flush 留下响应丢失窗口:command 的 residency 在响应算出时释放,而不是送达时,因此 shutdown 可以在宽限窗口内销毁 transport,丢掉一个副作用已经 settle 的响应。这与"Host-owned and settles"的既定语义一致,但 continuity slice 需要 outcome journal 来关上这个口。
- writer 的 2 MiB 字节上限在仓内没有测试(我用探针验证过机制:它在 64 帧上限之前触发,receipt 恰好 settle 一次)。第一个带大 payload 的 slice 会继承一条没测过的防线。
值得顺手补的测试证据缺口:
"non-reading Client overload"测试里连接实际很可能是被第 65 个 in-flight request 关掉的,而不是 writer 的 frame/byte 预算,因为循环在全速灌请求。它证明了隔离,但没证明关闭原因是 writer 预算。把关闭原因钉死(或把入站速度限制在 in-flight 上限以下),才能让它证明 PR 描述所声称的内容。
其他没有发现问题:build、typecheck、biome、git diff --check,以及新测试里的行为级断言都成立。
c29a77b to
3686114
Compare
|
@Astro-Han This finding was valid. I fixed it in
There is one transport-level boundary worth stating explicitly: after a peer has already sent FIN, some platforms do not expose a second peer-disappearance event until the Host writes again. The session reacts immediately when Client-visible teardown reasons and the command outcome journal remain deliberately deferred to the continuity/protocol slice, as suggested. The Runtime Host suite is now 60/60, and root build, typecheck, lint, and format checks pass on the rebased branch. 简体中文@Astro-Han 这个 finding 是有效的。我已在
这里有一个需要明确的 transport 边界:peer 已经发送 FIN 后,部分平台不会在 Host 再次写入前暴露第二个“peer 已消失”事件。 按照建议,Client 可见的 teardown reason 与 command outcome journal 仍明确留给 continuity/protocol slice。 当前 Runtime Host 全量为 60/60,rebase 后根级 build、typecheck、lint 与 format check 也均通过。 |
|
LGTM! Merging. |
English
Summary
This PR establishes the complete full-duplex connection transport/session foundation for the Runtime Host:
requestIdand serialized through one bounded FIFO writer, so reverse completion cannot corrupt framing;Boundary and decisions
RuntimeHostConnectionSessionowns the lifetime of one accepted connection. The Kernel continues to own operation admission and residency; the session pumps frames, dispatches admitted work, serializes replies, and performs idempotent connection cleanup.Clean EOF and hard failure intentionally follow different paths.
FramedTransportreportsread_eofonly after its accepted frame queue is exhausted, without disabling writes on the half-open socket. The session then stops accepting input, waits for every already-dispatched admission/handler and writer receipt, and closes after flush. A reset or any other transport/protocol failure retains immediate teardown semantics.The outbound writer separates synchronous queue admission from asynchronous
receipt.flushedcompletion. Its queue is capped independently at 64 frames and 2 MiB; internal typed causes distinguish the frame and byte bounds without adding a wire-level teardown reason.FramedTransportretains complete encoded frames while its decoded queue is full, pauses the socket, and resumes as reads release capacity. An oversized partial frame still fails closed, and EOF with a trailing partial frame remains a protocol error rather than clean EOF.This is the transport/session foundation consumed by later continuity and native-provider slices. Those slices can attach domain behavior without redefining connection flow control or writer semantics.
Scope
This PR does not add Session subscriptions, Interactive domain owners, native-provider frames, surface wiring, or a production cutover. It also deliberately leaves Client-visible teardown reasons and the command outcome journal to the continuity/protocol slice. Existing production entrypoints remain unchanged, and no protocol operation shape changes.
Validation
Real socket and Runtime Host tests cover reverse completion, FIFO flush, the 2 MiB writer cause before the frame limit, clean half-close reply draining, hard reset during pending admission, pre-Ready connections, duplicate/in-flight limits, slow-reader isolation, inbound frame/byte backpressure, and oversized/partial frames. The full Runtime Host suite passes 60 tests; root build, typecheck, lint, and format checks pass.
Tracked by #1167. Architectural context: #853.
简体中文
概要
本 PR 完整建立 Runtime Host 的全双工 connection transport/session 基础:
requestId关联,并经过唯一、有界的 FIFO writer 串行写出,因此 handler 逆序完成不会破坏 framing;边界与决策
RuntimeHostConnectionSession负责一个 accepted connection 的生命周期。Kernel 继续拥有 operation admission 与 residency;session 负责 pump frame、dispatch 已接纳工作、串行回复与幂等 connection cleanup。clean EOF 与 hard failure 有意采用不同路径。
FramedTransport只在已经接纳的 frame queue 排空后报告read_eof,并且不会禁用 half-open socket 的写侧。session 随后停止接收输入,等待 EOF 前已 dispatch 的 admission/handler 与 writer receipt 全部完成,并在 flush 后关闭。reset 或其他 transport/protocol failure 仍立即 teardown。outbound writer 区分同步 queue admission 与异步
receipt.flushed完成。队列分别受 64 frame 与 2 MiB 上限约束;内部 typed cause 会区分 frame bound 与 byte bound,但不会因此增加 wire-level teardown reason。FramedTransport会在 decoded queue 满时保留完整 encoded frame、暂停 socket,并在 read 释放容量后恢复。超大的 partial frame 仍然 fail closed;EOF 末尾存在 partial frame 时仍属于 protocol error,而不是 clean EOF。这是后续 continuity 与 native-provider slice 共用的 transport/session 基础。后续 slice 可以接入各自领域行为,无需重新定义 connection flow control 或 writer 语义。
范围
本 PR 不增加 Session subscription、Interactive domain owner、native-provider frame、surface wiring 或 production cutover。Client 可见的 teardown reason 与 command outcome journal 明确保留给 continuity/protocol slice。现有 production entrypoint 保持不变,protocol operation shape 也没有变化。
验证
真实 socket 与 Runtime Host 测试覆盖 handler 逆序完成、FIFO flush、在 frame limit 前触发 2 MiB writer cause、clean half-close 后回复排空、pending admission 期间的 hard reset、pre-Ready connection、duplicate/in-flight 上限、慢读者隔离、inbound frame/byte backpressure,以及 oversized/partial frame。Runtime Host 全量 60 项通过;根级 build、typecheck、lint 与 format check 通过。
由 #1167 跟踪;架构背景见 #853。