Skip to content

fix(scheduler): stop promising a memory requeue that step() cannot carry out - #5322

Open
masterfasa wants to merge 2 commits into
jundot:mainfrom
masterfasa:fix/memory-enforcer-requeue-livelock
Open

masterfasa wants to merge 2 commits into
jundot:mainfrom
masterfasa:fix/memory-enforcer-requeue-livelock

Conversation

@masterfasa

Copy link
Copy Markdown
Contributor

Problem

At hard memory pressure, ProcessMemoryEnforcer asks the scheduler to store and requeue live requests instead of aborting them, and a positive return from Scheduler.request_memory_requeue() suppresses abort_all_requests. That positive answer could be returned indefinitely while zero memory was freed:

  1. _process_pending_memory_requeue() clears _pending_memory_requeue and returns silently when a live request has no generation-batch row yet (still in BatchGenerator's prefill batch), while the request-side blocker check accepts any request with a valid uid — so the next 1 s enforcer poll is told "yes" again.
  2. While the inference thread is inside a long step(), _pending_memory_requeue stays set and every 1 s poll returns len(running) + len(prefilling) unbounded.

Observed on an Apple M5 Pro (64 GB) with max_concurrent_requests=4: Hard memory pressure: storing and requeueing 4 request(s) repeating every ~1 s at 57.3 GB physical footprint with no token progress.

Fix

Track unfulfilled requeue asks per request (_memory_requeue_stalls). When step() skips a requested requeue (now logged with the reason) or a poll arrives while _pending_memory_requeue is still undrained, increment the stall count. After _MAX_MEMORY_REQUEUE_STALLS (3), _memory_requeue_blocker reports "requeue stalled", request_memory_requeue() clears the pending flag and returns 0, and ProcessMemoryEnforcer falls through to clean abort rather than livelocking. Stall entries are cleared on successful requeue, when a request exits, and on reset().

Tests

Added two regression tests in tests/test_scheduler_boundary_completion.py covering both failure modes and recovery for subsequent requests (904 passed across test_process_memory_enforcer.py and scheduler suites; 5/5 mutations of the stall tracking and cleanup paths fail the new tests).

@masterfasa
masterfasa force-pushed the fix/memory-enforcer-requeue-livelock branch from b679218 to 6e7887d Compare November 23, 2026 00:39
@masterfasa

Copy link
Copy Markdown
Contributor Author

Pushed a second commit (rebased cleanly onto main 1cd5aaa9) that fixes what the enforcer hears when it polls while step() is carrying a requeue out, and verified clean stacking with #5542:

  • Defect fixed in Commit 2: While testing the stall cap we found that a requeue whose store outlasts a few 1 s polls got aborted halfway: _process_pending_memory_requeue() clears the pending flag before it stores, so a poll during the store set the flag again and the following polls counted as stalls until request_memory_requeue() answered 0. A second race was worse: once requests were moved to waiting, running and prefilling were empty, so a poll during detach or _clear_cache() answered 0 and abort_all_requests() killed the requests that had just been stored.
  • Fix: The scheduler now tracks _memory_requeue_running_count — set to len(live) once all blocker checks pass and reset in a finally after _clear_cache() returns. request_memory_requeue() answers with it before reading the queues and without setting _pending_memory_requeue. step() itself is unchanged, so polls during an ordinary step are still recorded.
  • Tests & fix(scheduler): bound the memory-pressure requeue store and let aborts stop it #5542 compatibility: Three new tests cover polls during the store, polls during _clear_cache() after detach, and a poll during a normal step with nothing pending (each verified via mutation).
    • Standalone pytest -q tests/test_scheduler_boundary_completion.py tests/test_process_memory_enforcer.py: 221 passed.
    • Combined with fix(scheduler): bound the memory-pressure requeue store and let aborts stop it #5542 cherry-picked on top (zero conflicts — the store body is kept un-indented on purpose so the two PRs do not collide) + tests/test_memory_requeue_store_budget.py: 227 passed (request_memory_requeue() during a slow store answers [2, 2, 2, 2, 2, 2] and leaves _pending_memory_requeue = False).

masterfasa and others added 2 commits December 3, 2026 20:07
…rry out

At hard memory pressure the enforcer asks the scheduler to store and
requeue the live requests instead of aborting them, and a positive answer
means it does not abort. request_memory_requeue() could answer yes
indefinitely while nothing was released:

- _process_pending_memory_requeue() cleared the flag and returned without
  a trace when a request was still in the batch generator's prefill batch
  (no generation row yet), so the next poll asked and was told yes again.
- while the inference thread sat in a long step, the flag stayed pending
  and every poll was answered with the live count.

Track unfulfilled asks per request and, after _MAX_MEMORY_REQUEUE_STALLS,
report the request as not requeueable. request_memory_requeue() then
returns 0 and the enforcer aborts as it did before requeueing existed.
Entries are dropped with the request, and a skipped requeue now logs why.
… count

The enforcer polls once a second. _process_pending_memory_requeue() clears
the pending flag before it stores, so a poll during the store set the flag
again and the following polls counted as stalls: after
_MAX_MEMORY_REQUEUE_STALLS of them request_memory_requeue() answered 0 and
the enforcer aborted requests whose requeue was under way. Any store longer
than about four polls was affected.

Once the requeue has moved the requests to waiting, running and prefilling
are empty, so a poll during the detach or the cache clear read no live
request and answered 0 as well, and the enforcer aborted the requests that
had just been stored.

Keep the number of requests being requeued from the point the requeue is
committed until the cache clear returns, and answer polls in that window
with it, before reading the queues and without setting the flag. A step()
that has no requeue to carry out never sets it, so its polls are kept.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@masterfasa
masterfasa force-pushed the fix/memory-enforcer-requeue-livelock branch from 6e7887d to df30dd3 Compare December 4, 2026 02:07
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