Skip to content

[PR] fix: detach inline abort listeners after synchronous completion - #6575

Merged
ranxianglei merged 14 commits into
ranxianglei:masterfrom
Afloat16:2026-11-13_inline-signal-cleanup
Nov 14, 2026
Merged

ranxianglei merged 14 commits into
ranxianglei:masterfrom
Afloat16:2026-11-13_inline-signal-cleanup

Conversation

@Afloat16

Copy link
Copy Markdown
Contributor

Fixes #6573

Problem

The inline dispatcher installs finish/close cleanup after awaiting the response commit. Fast management and error responses can finish synchronously before that await resumes, so their terminal events are already gone. Each completed request leaves its onAbort closure on the caller signal, retaining the finished response until a later abort or signal collection.

Changes

Detach the abort listener immediately when the response is already ended or destroyed. For active streaming responses, retain the existing finish/close cleanup and mid-body cancellation behavior. Caller-owned listeners are untouched.

Validation

  • Five real-engine regressions: completed health response, repeated shared-signal requests with a caller-owned listener, completed 404 response, a live response which finishes normally, and mid-body caller abort.
  • Unchanged master e1f31ad: 3 failures and 2 passing streaming controls. Five completed health requests leave five engine listeners.
  • Fixed branch: all five pass, including exact listener counts and body error behavior. With existing pi-inline and inline-streaming tests, 13 tests pass.
  • Root typecheck, complete build, unused-export gate, test-import-graph gate and git diff --check pass. Hosted CI and real-model E2E remain separate merge gates.

Related scope

Fresh all-state searches and prior Afloat16 PRs were checked. #6504 handles signal inheritance and #6571 connects body cancellation to response destruction. This patch handles completion before cleanup registration; it introduces no retries, configuration, persistence or kernel changes.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷

(ai: vllm-qwen/qwen3.8-27b)

查重 (duplicate check): 不重复. No other open [PR] targets this thread (#6573). The related inline-transport PRs are distinct mechanisms: #6504 (signal inheritance / honoring Request cancellation) and #6571 (body cancellation → response destruction). This PR handles a third, non-overlapping facet — completion-before-cleanup-registration — so it stands on its own.

Review verdict — the fix is correct, minimal, and well-tested.

What I verified (live, on this checkout):

  • Bug reproduced on unpatched master e1f31ad9: running the PR's 5-test suite against master gives exactly 3 failures (completed health, shared-signal reuse, completed 404) and 2 passing streaming controls — matching the issue's claim precisely.
  • On the PR branch 253f1201: all 5 pass; combined with the existing inline-streaming + pi-inline suites, 13/13 pass.
  • Mechanism confirmed in source: InlineRes.end()/destroy() (src/inline-engine.ts:198-223) set writableEnded/destroyed synchronously and emit finish/close in the same call. The dispatcher's await committed only resumes in a later microtask, so by the time the finally block runs, a synchronous response's terminal events have already fired — the old res.once("finish"/"close", detach) registered onto events that already happened, and detach never ran. The added if (res.writableEnded || res.destroyed) detach(); else {...} is exhaustive because those two flags are the synchronous markers of a terminated response.
  • Edge cases hold: an active stream (neither flag set) keeps the original finish/close cleanup; a destroyed/aborted response detaches as a no-op (the once:true listener already auto-removed, or was never registered for a pre-aborted signal); caller-owned listeners are untouched (removeEventListener targets only the engine's onAbort).
  • Scope is right — this is the only instance of the leak pattern. makeHttpDispatch (WS bridge) takes a real req/res, no signal-based onAbort; external-summary.ts:201/220 removes unconditionally in finally (no completion-before-cleanup race); native-ws-intercept.ts is a separate subsystem with explicit removal at its own lifecycle points.
  • No wire / config / persistence / kernel changes. The else branch is byte-identical to the old code, so streaming behavior is preserved exactly.

Labels added to the PR (judged from its own diff): bug, p2, scope/wiring — a listener-lifecycle leak in the inline transport wiring.

CI: still in flight on the mirror — several jobs from an earlier run show cancelled and the gate/test jobs are queued; the full-suite verdict is pending. The local targeted verification above is the decisive evidence for this change and is green.

8问 (结构化审核):

  1. 是什么问题: 内联引擎 dispatcher 在响应同步完成后,把调用方 AbortSignal 上的 abort 监听器泄漏了——已完成的响应(health/404 等管理/错误响应)的 onAbort 闭包一直挂在调用方 signal 上,直到调用方 abort 或 signal 被回收。
  2. 什么原因导致的: InlineRes.end()/destroy() 在发出 finish/close 前同步置位 writableEnded/destroyed,而 dispatcher 的 await committed 在微任务里才恢复——此时终态事件早已触发,finally 块里 res.once("finish"/"close", detach) 注册到的是已经发生过的事件,detach 永远不会被调用。
  3. 影响范围与严重程度: 仅当调用方跨多个已完成请求复用同一个长生命周期 AbortSignal 时触发(pi-inline 车道会传入宿主 signal)。每个已完成响应保留一个 onAbort 闭包(含 InlineRes 对象图),缓慢内存泄漏。p2——能用但有明显问题,需特定条件(共享长生命周期 signal)且累积缓慢,非可用性缺陷。
  4. 怎么修复的: 在 finally 块加一个判断——若 res.writableEnded || res.destroyed(响应已终结)则立即 detach();否则保留原有 finish/close 清理。因 InlineRes 同步置位这两个标志,该检查完备:已完成响应立即摘除、活跃流保留清理、已销毁(已 abort)响应 detach 为幂等 no-op。
  5. 是否有回归问题: 无。else 分支与原代码逐字节一致(流式行为不变);新分支只在响应已终结时触发,此时 detach 幂等。不改 wire/配置/持久化/kernel。高危模式(语义漂移、fail-fast 变吞错、阈值/时序/输出格式漂移)逐项核查均无。
  6. 是否破坏缓存: 不涉及。不触碰缓存、重试、增量游标、去重键。
  7. 是否改了设计方向: 不涉及。内联引擎 dispatcher 内部的监听器生命周期局部修复,不动架构/边界/数据流。
  8. 是否引入了新的配置: 不涉及。无新 env/config/默认值。

判定: 审核通过——修复正确、最小、测试有牙(3 个复现原 bug + 2 个流式对照,均断言精确监听器计数)、范围恰当、无回归、diff 干净。符合 auto-merge 标准(单模块 fix:、有回归测试、不触碰 wire/config/kernel/承重基建),CI 转绿后可合并;合并权在 owner。

ework-agent and others added 13 commits November 14, 2026 04:35
The suite baseline preload (preload-storage-baseline.mjs) pins
BILI_STORAGE_UNIFIED="legacy" for the broad corpus. After ranxianglei#6480 flipped the
storage default to pure-sqlite, the notice-registry suite's visibleNotices
assertions expect the pure-sqlite default notice, which is gated on
unifiedSelectionIsSqlite (unified "sqlite" or absent) — the preload's
"legacy" made the notice silent, so the suite was born-red on master. Pin
the selection explicitly (ranxianglei#4011 lesson: a test that depends on the production
default passes locally but fails in CI).
…test

The dsh plugin lane (ci-e2e-dsh.yml) runs node --import tsx --test
WITHOUT the preload-storage-baseline.mjs preload, so BILI_STORAGE_UNIFIED
is unset and the storage mode defaults to 'full'. Under the pure-sqlite
default flip (5a80e2b, ranxianglei#6480), 'full' mode turns the JSON envelope
writer OFF — so the .content-store.json sidecar is never written, and
the test's readdirSync(namespace) hits ENOENT.

Pin BILI_STORAGE_JSON_WRITER=on in the test so the content-store
sidecar is written regardless of the storage mode. This keeps the
corrupted-store detection path exercised in both full and legacy modes.

Closes: dsh plugin lane red on master (run 43532980164, job 132255459726)
The pure-sqlite default flip (5a80e2b, ranxianglei#6480) turns the JSON envelope
writer OFF, so the .json session file this lane asserts on is never
written and 'proxy must persist the session' / 'no persisted session
carried compressed blocks' fail after the 20s wait. Pin the writer ON in
the spawned client's env (the bootstrapped proxy inherits it) so the
envelope this test was written for is still produced.

Closes: native opencode lane red on master (run 43547593803, job 132302438610)
Same pure-sqlite default flip (5a80e2b, ranxianglei#6480) as the opencode lane:
the JSON envelope writer is OFF, so 'proxy must persist the session
within 20000ms' fails — the on-disk .json session file is never written.
Pin the writer ON in the spawned dsh client's env (the bootstrapped
proxy inherits it).

Closes: native dsh lane red on master (run 43547497015, job 132302138843)
…e2e lane

Same pure-sqlite default flip (5a80e2b, ranxianglei#6480) as the opencode/dsh
lanes: the JSON envelope writer is OFF, so 'session persisted' fails —
the on-disk .json session file is never written. Pin the writer ON in
the spawned pi client's env (the bootstrapped proxy inherits it).

Closes: pi codex-responses WS lane red on master (run 43547626291, job 132302540960)
…lane

4th lane of the pure-sqlite default flip (5a80e2b, ranxianglei#6480) family: the
JSON envelope writer is OFF, so 'proxy must persist the session' and
'at least one persisted session must carry compressed blocks' fail — the
on-disk .json session file is never written. Pin the writer ON in the
spawned pi client's env (the bootstrapped proxy inherits it).

Closes: pi package-native lane red on ci-fixes (run 43558179054, job 132336647955)
Same pure-sqlite default flip (5a80e2b, ranxianglei#6480) family: the JSON writer
is OFF, so the per-session CCR content-store sidecar (.content-store.json)
that 'child persists its own companion and survives a proxy restart'
asserts on is never written (0 !== 1). The earlier b1bc17b pin only
covered tests/plugin-fork.test.ts, not this file. Pin the writer ON at
module load (before any SessionStore is constructed).

Closes: dsh plugin lane red on ci-fixes (run 43558179102, job 132336519539)
…lane

Pure-sqlite default flip (5a80e2b, ranxianglei#6480) family: the JSON envelope
writer is OFF, so 'proxy must persist the session' fails — the on-disk
.json session file is never written. Pin the writer ON in the spawned
opencode client's env (the bootstrapped proxy inherits it).
Pure-sqlite default flip (5a80e2b, ranxianglei#6480) family: the JSON envelope
writer is OFF, so the on-disk .json session envelope this lane asserts on
(exactly ONE persisted session under the raw key) is never written. Pin
the writer ON in the spawned dsh client's env.
Pure-sqlite default flip (5a80e2b, ranxianglei#6480) family: the JSON envelope
writer is OFF, so the on-disk .json session file this lane asserts on
(state survival across the OLD→NEW update) is never written. Pin the
writer ON in the spawned proxy's env (both update phases share envBase).
Resolve CI baseline conflicts using the current upstream test setup.
@ranxianglei
ranxianglei merged commit 2f8c5b9 into ranxianglei:master Nov 14, 2026
50 checks passed
@ranxianglei

Copy link
Copy Markdown
Owner

Superseded by #6677 (merged as 3858b6956, 2026-11-14).

Landed into master via the real merge commit 2f8c5b94201 (Merge Afloat16/2026-11-13_inline-signal-cleanup (PR #6575)), so your original commits are preserved verbatim in master history — only hashes of the batch wrapper changed, your authorship and commit messages are intact.

The fix for #6573 is now in master. Closing this PR as superseded — thanks for the careful, well-tested fix.

1 similar comment
@ranxianglei

Copy link
Copy Markdown
Owner

Superseded by #6677 (merged as 3858b6956, 2026-11-14).

Landed into master via the real merge commit 2f8c5b94201 (Merge Afloat16/2026-11-13_inline-signal-cleanup (PR #6575)), so your original commits are preserved verbatim in master history — only hashes of the batch wrapper changed, your authorship and commit messages are intact.

The fix for #6573 is now in master. Closing this PR as superseded — thanks for the careful, well-tested fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working p2 scope/wiring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Completed inline responses retain caller AbortSignal listeners

2 participants