Skip to content

[Fix] scheduler: Keep PD decode non-idle while deferred KV releases are held - #44386

Merged
hnyls2002 merged 3 commits into
sgl-project:mainfrom
alphabetc1:fix/flush-waits-deferred-decode-release
Oct 16, 2026
Merged

hnyls2002 merged 3 commits into
sgl-project:mainfrom
alphabetc1:fix/flush-waits-deferred-decode-release

Conversation

@alphabetc1

@alphabetc1 alphabetc1 commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

On PD decode, a failed or aborted transfer is held for deferred release (#35049, on by default). Its KV pages, request row, tree lock and metadata slot stay with it until every prefill rank acks that its writes drained, or until the timeout (30 s by default).

Scheduler.is_fully_idle never looks at these holds. Its decode branch checks the prealloc, retracted and transfer queues, but a held request has already left the transfer queue. So flush_cache runs while a hold is outstanding:

  1. The flush clears the tree, the request pool and the allocator. The held pages go back to the free list.
  2. When the hold resolves, _do_release → release_kv_cache frees the same pages again.
  3. The allocator now has every one of those pages twice. Two later requests get the same pages and overwrite each other's KV.
  4. With the decode radix cache on, the held req.lock points at a node of the old tree.
  5. If the prefill write was still landing, which is always the case for a room [PD] Poison the deferred abort ack when a Mooncake send fails #43719 poisons, it lands on pages that already have a new owner.

RL rollouts hit this without any unusual timing. The flow is abort, pause_generation, then update_weights(flush_cache=True). While paused, the decode loop skips process_decode_queue, so nothing resolves the holds before the flush.

The idle leak check already knew about these holds and skipped itself while they were pending. Only the flush gate was missing.

Modifications

  • In is_fully_idle, the decode branch now requires has_pending_deferred_releases() to be False.

The same gate also covers attach / detach of a storage backend and release_memory_occupation. The last of these currently drops held entries and pauses KV memory while a prefill write may still be landing.

Tests

  • No new unit test. The existing retracted-queue idle test stubs the transfer queue with a SimpleNamespace, which now also needs has_pending_deferred_releases.
  • test_decode_queue_cleanup.py passes (14 tests).
  • Allocator-level repro on an H200, with the real is_fully_idle, the real DecodeTransferQueue defer / resolve and a real allocator. On main, a flush followed by the deferred release raised available pages above the pool total (272 of 256), and one alloc returned 16 pages twice. With this change the flush is refused, and the pool returns to 256 with no duplicates.

Related: #36869 rewrites is_fully_idle as not_idle_reasons(). When it rebases, this condition becomes one more require_empty entry.


CI States

Latest PR Test (Base): ❌ Run #38836202980
Latest PR Test (Extra): ❌ Run #38836203143
Latest PR Test (AMD ROCm 10): ❌ Run #38836203142

@alphabetc1

Copy link
Copy Markdown
Collaborator Author

/rerun-test -c

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

🚀 ubuntu-latest (1 test): ✅ View workflow run

cd test/ && python3 registered/unit/disaggregation/test_decode_queue_cleanup.py

@hnyls2002
hnyls2002 merged commit f9df334 into sgl-project:main Oct 16, 2026
106 of 122 checks passed
@alphabetc1
alphabetc1 deleted the fix/flush-waits-deferred-decode-release branch October 16, 2026 02:18
LinyuanLi0046 added a commit to LinyuanLi0046/sglang that referenced this pull request Oct 19, 2026
Port existing community lifecycle code onto b85c644.
Fixed upstream source: 0dc6de2.

Keep generation-tagged ABORT/ACK state across sender cleanup, count dequeued
and in-flight transfers, drain running transfer futures, poison uncertain
native failures, and retain decode KV plus metadata until ACK or the
community device-destination timeout. Resolve held releases before decode
scheduling gates and account for them in scheduler idle checks. Complete
the existing cached-prefix event handoff through the worker. Use shared
terminal-status helpers and socket locks for failure fanout and to prevent
Success after cancellation.

Migrated upstream implementations and provenance:
- 97dedd1 (sgl-project#35049): device deferred
  release, ACK tracking, future draining and worker drain integration.
- adca19c (sgl-project#35360): common ACK helpers,
  unique prefill rank and dequeue-before-status accounting.
- 8ef6d31 (sgl-project#40645): preserve pending
  drain ACK targets across sender cleanup.
- cd11037 (sgl-project#41023): default-on setting
  and explicit backend capability gate.
- 14ab74e (sgl-project#41402): ACK endpoint fanout.
- 2a1c477 (sgl-project#41404): defer all eligible
  failure paths and force-arm partial metadata publication.
- 8293f9a (sgl-project#37077): shared ABORT/ACK
  types, generation filtering and common target/poison state.
- 4195980 (sgl-project#43719): suppress drained
  ACK after uncertain native KV/state/aux failure.
- f9df334 (sgl-project#44386): pending decode
  releases keep the scheduler non-idle.
- c683190 (sgl-project#41395): cached-prefix
  ready-event forwarding and worker-side synchronization.
- 2a46cf2 (sgl-project#36612): terminal/cleared
  room handling, first failure cause and shared failure/status fanout.
- abddb1c (sgl-project#32541): state-send return
  propagation and per-cached-socket send locking.
- 05c7ebf (sgl-project#30545): staging_counted
  and outstanding accounting across worker requeue/completion.
- 8ad7641 (sgl-project#36160): common wait_event
  job field used by the early-send handoff.
- 5b5d721 (sgl-project#40703): worker cleanup
  precedes outstanding-entry removal.
- 348689d (sgl-project#38959): sender poll wrapper
  holds Success while counted transfers remain.

- 1a0250c (sgl-project#41607): check Failed
  after state transfer before continuing to aux.
- 8bf780d (sgl-project#41128): retain deferred
  release start_time/deadline tuple shape without its new metrics API.
- 4dc9cda (sgl-project#37454): decode-side
  backend capability check before deferring a failed request.

Baseline compatibility adaptations:
- Insert selected snippets into existing server_args/configuration and
  worker/listener layout; retain local request timing and transfer batching.
- Omit absent deferred-allocation/bootstrap ownership, role-switch teardown,
  host-receive and PP-consensus branches; import no placeholder subsystems.
- Keep baseline 30-second socket timeout, optional sender-table guards and
  three-frame Mooncake status wire format; serialize existing manager sends
  with the imported endpoint locks.
- Use release_kv_cache(req, tree_cache, is_insert=False), the old failed
  request release API, and the existing scheduler invariant-check entrypoint.
- Omit unavailable new deferred-release metrics without importing the full
  metrics refactor; retain existing local metrics and diagnostics.

No WELM/NPU layerwise additions are included. Device timeout behavior and
backend capability defaults match the pinned community source.

Validation: 48 external CPU protocol/worker/decode cases; source AST
comparison for unadapted helpers; Python syntax, isort and configured Ruff
checks; git diff --check. CPU fixtures remain outside this repository.
No NPU/RDMA hardware execution was available for this commit.
@sohom-cs

Copy link
Copy Markdown
Contributor

Thanks for fixing this. I reproduced the same race on CPU with a script that drives the real DecodeTransferQueue, allocators and flush_cache (one aborted transfer leaves a deferred release holding two pages), and it confirms the fix:

before #44386  is_fully_idle()=True   flush_cache() -> True   the held pages go to the next request and end up freed twice (10 pages free in an 8-page pool)
after #44386   is_fully_idle()=False  flush_cache() -> False  nothing is freed twice, and the PD role switch is refused as well

The role switch goes through the same check (role_switch.py:73). If a regression test would help, I can send one that aborts a transfer through the real DecodeTransferQueue and checks that flush_cache and the role switch are refused until the hold resolves.

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.

3 participants