Skip to content

server: fix sleep race that strands queued tasks and crashes on model unload - #30012

Closed
vyalamar wants to merge 1 commit into
ggml-org:masterfrom
vyalamar:fix-server-sleep-race-29689
Closed

vyalamar wants to merge 1 commit into
ggml-org:masterfrom
vyalamar:fix-server-sleep-race-29689

Conversation

@vyalamar

@vyalamar vyalamar commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Fixes #29689.

The bug

With --sleep-idle-seconds, a request can arrive in the exact window where the server is falling asleep:

  1. The HTTP handler calls wait_until_no_sleep() and observes the server awake.
  2. The handler releases the mutex and begins tokenizing the prompt.
  3. The main loop sees an empty task queue and an expired idle timer, enters sleep, and unloads the model state.
  4. The handler posts its task afterwards — but the sleep predicate only watches req_stop_sleeping, not queued work.

Results: the task is stranded while the client hangs, and on Linux the model unload racing tokenization produced a SIGSEGV.

The fix

  • server_queue gains an n_inflight counter guarded by mutex_tasks.
  • wait_until_no_sleep() claims a slot once the server is confirmed awake.
  • The claim is released via RAII (server_res_generator) after the task is posted, so early validation/error returns cannot leak it.
  • should_sleep() refuses to sleep while n_inflight > 0.

Testing

  • Added tests/test-server-queue.cpp: a deterministic regression test that drives the queue directly — it fails with BUG #29689: server fell asleep while a request was in-flight when the should_sleep() guard is removed, and passes with the fix.
  • Built llama-server (CPU) and the new test target; git diff --check clean.

… unload

When --sleep-idle-seconds puts the server to sleep, a request that passed
wait_until_no_sleep() but had not yet posted its task could be stranded:
the main loop saw an empty queue, entered sleep, and unloaded the model
while the handler was still tokenizing (observed as a hang and a SIGSEGV).

Track in-flight requests with an n_inflight counter guarded by mutex_tasks.
wait_until_no_sleep() claims a slot once the server is awake; the claim is
released via RAII (server_res_generator) after the task is posted, and
should_sleep() refuses to sleep while any claim is outstanding.

Adds tests/test-server-queue.cpp: deterministic regression test that fails
without the should_sleep() guard and passes with it.

Fixes ggml-org#29689
@vyalamar
vyalamar requested review from a team and ggerganov as code owners October 5, 2026 18:32
@ggml-gh-bot

ggml-gh-bot Bot commented Oct 5, 2026

Copy link
Copy Markdown

Hi @vyalamar, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • PR Template not respected: Please respect the template when creating a new pull request. Make sure to fill out all required sections.

  • AI-generated content: While code is allowed to be generated by AI, please write the PR description and commit messages on your own without the help of AI.


Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@ggml-gh-bot ggml-gh-bot Bot added the draft PR will be changed to draft by github-actions bot label Oct 5, 2026
@vyalamar vyalamar closed this Oct 5, 2026
@github-actions github-actions Bot added testing Everything test related server labels Oct 5, 2026
@vyalamar vyalamar reopened this Oct 6, 2026
@vyalamar

Copy link
Copy Markdown
Contributor Author

Hi @vyalamar, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • PR Template not respected: Please respect the template when creating a new pull request. Make sure to fill out all required sections.

  • AI-generated content: While code is allowed to be generated by AI, please write the PR description and commit messages on your own without the help of AI.


Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

Human here . I will share the handwritten architecture diagram to explain these changed. I had it somewhere let me find and share. Also I am trying to test this on Mac and windows platforms and reproduce the behavior ( it's platform agonistic though) . I will share the logs with before and after to prove the fix works . Please allow sometime.

@nikwen

nikwen commented Nov 14, 2026

Copy link
Copy Markdown
Member

Superseded by #32133.

@nikwen nikwen closed this Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

draft PR will be changed to draft by github-actions bot server testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misc. bug: llama-server --sleep-idle-seconds: a request that arrives as the server falls asleep is never processed

2 participants