Skip to content

Clear the idle-reload stash between tests - #11322

Merged
danielhanchen merged 3 commits into
mainfrom
fix/clear-the-idle-reload-stash-between-tests
Sep 19, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
fix/clear-the-idle-reload-stash-between-tests

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

(Python 3.13, l-r) failed on main at 6448afca0:

FAILED tests/test_openai_auto_switch.py::test_chat_withheld_model_named_by_its_advertised_alias
assert 500 == 404

It passed on the run before it, and on every PR run of that shard. The captured log carries the whole mechanism: the request for "tiny" went looking for unsloth/Idle-GGUF on the Hub, retried it five times against the test network block, and then died on

AttributeError: '_B' object has no attribute 'load_model'

Why a test that never mentions Idle-GGUF went to load it

unsloth/Idle-GGUF is named nowhere in that test. It belongs to the idle-unload tests in the same file, which set llama_keepwarm._last_unloaded_model through a real unload and clear it only on the way in, through their own _reset_keepwarm. Whichever of them runs last in an xdist worker leaves the stash standing for every test after it, in any module.

The chat route reads that stash before it refuses anything:

# Idle-unload may have freed the model; reload exactly what it freed
# (path + quant + advertised id) so an alias/unknown name stays servable
last = get_last_unloaded_model()

so a later test whose backends are doubles reloads a model it never named, and _B has no load_model.

The route is not wrong here. After an idle unload an alias or unknown name has to stay servable, and that branch is what keeps it so. The defect is test isolation.

The fix

An autouse fixture in studio/backend/tests/conftest.py clearing the two stash fields around every test, in the shape of the settings-memo fixture directly above it. That fixture's docstring records this same symptom (assert 500 == 404 in the l-r shard and nowhere else) from a different cause in #11241, which is why this clears the stash rather than widening it: both hand a later test an input it never set, and under --dist loadgroup which tests share a worker changes between runs.

Only the stash fields. The counters beside them (_inflight, _pending, _last_active) are live-traffic bookkeeping, and zeroing those around every test would hide exactly the kind of leak this exists to catch.

Robustification

  • test_a_stale_idle_reload_stash_diverts_a_refusal_into_a_reload pins the diversion itself, asserting which model the route went to load rather than the status code, since the status is only whatever this file's double happens to be missing. It reproduces the CI failure locally.
  • test_the_idle_reload_stash_is_cleared_around_every_test drives the fixture directly, at both ends, because an ordering-based check passes by luck under --dist loadgroup and the leak is specifically the last idle test in a worker seeding the first test of the next module.

Verification

  • tests/test_openai_auto_switch.py: 698 passed, 1 skipped.
  • Neutering the fixture's _clear() fails test_the_idle_reload_stash_is_cleared_around_every_test with "the stash was not cleared before the test body".
  • The keepwarm, idle and resume files: 290 passed, 1 failed. That one failure, test_diffusion_checkpoint_resume.py::test_bitsandbytes_adamw8bit_state_round_trips, reproduces identically on clean main and is unrelated.

Not verified here

The full l-r shard cannot be run on this box: 22 files fail collection on this workspace's unsloth install, and a local -n 4 --dist loadgroup run produces 101 failures that CI does not have. The root cause above is read from the CI log and reproduced directly by the new diversion test rather than by replaying the shard.

(Python 3.13, l-r) failed on main at 6448afc with

    FAILED tests/test_openai_auto_switch.py::test_chat_withheld_model_named_by_its_advertised_alias
    assert 500 == 404

and passed on the run before it and on every PR run of that shard. The captured
log has the whole mechanism: the request for "tiny" went looking for
unsloth/Idle-GGUF on the Hub and then died on

    AttributeError: '_B' object has no attribute 'load_model'

unsloth/Idle-GGUF is named nowhere in that test. It belongs to the idle-unload
tests, which set llama_keepwarm._last_unloaded_model through a real unload and
clear it only on the way IN, via their own _reset_keepwarm. Whichever of them
runs last in an xdist worker leaves the stash standing for every test after it,
in any module. The chat route reads that stash before it refuses anything and
reloads exactly what the idle loop freed, so a later test with doubles for
backends reloads a model it never named.

The route is not wrong: after an idle unload an alias or unknown name has to
stay servable, and that branch is what keeps it so. The leak is test isolation,
so the fix is an autouse fixture clearing the two stash fields around every
test, in the shape of the settings-memo fixture beside it, whose docstring
records this exact symptom from a different cause in #11241.

Only the stash fields. The counters next to them (_inflight, _pending,
_last_active) are live-traffic bookkeeping, and zeroing those around every test
would hide the kind of leak this exists to catch.

Robustification: one test pins the diversion itself, asserting WHICH model the
route went to load rather than the status code, since the status is only
whatever this file's double happens to be missing. It reproduces the CI failure
locally. The second drives the fixture directly, both ends, because an
ordering-based check passes by luck under --dist loadgroup.

Verified: test_openai_auto_switch.py 698 passed, 1 skipped. Neutering the
fixture fails the fixture test. The keepwarm, idle and resume files are 290
passed, 1 failed, and that one failure (test_bitsandbytes_adamw8bit_state_round_trips)
reproduces identically on clean main.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-19T05:38:11.258855Z 31916ae Manual request
🔒 Security Review ✅ Completed 2026-09-19T05:32:01.234025Z 85be4a8 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: 85be4a8282

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 85be4a8282

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/tests/conftest.py Outdated

def _clear():
_keepwarm._last_unloaded_model = None
_keepwarm._kv_resume = None

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Purge saved KV files before dropping their manifest

When a test leaves a real _kv_resume manifest, assigning None here discards the only reference without deleting the slot files listed in it. llama_keepwarm explicitly treats the manifest taker as responsible for cleanup, and its existing purge_kv_resume()/_set_last_unloaded(None) paths perform that cleanup under the module lock; bypassing them can accumulate potentially large KV snapshots during the suite while the new test only uses a manifest with no files and therefore misses the leak.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, and the test could not have caught it, which is the part that mattered most. llama_keepwarm makes whoever takes the manifest responsible for unlinking its slots - take, purge and _set_last_unloaded all end in _delete_resume_files - so clearing the field by hand opts out of that and leaves a saved snapshot on disk with nothing left pointing at it.

Fixed in 31916ae: the fixture now clears through _set_last_unloaded(None), which clears both fields under the module lock and unlinks the slots on the way out. The test seeded a manifest naming no files, and an empty manifest cannot tell a clean-up that worked from one that never ran, so it now writes a real slot file at both ends and asserts the file is gone rather than only that the field is None. Reverting to the raw assignment fails it with 'the KV slot file outlived the manifest that named it'.

Assigning _kv_resume = None drops the only reference to a saved snapshot while
its slot files stay on disk. llama_keepwarm makes whoever takes the manifest
responsible for unlinking them, which is why take/purge and _set_last_unloaded
all end in _delete_resume_files. A fixture that clears the field by hand opts
out of that, so a suite which saves KV accumulates snapshots run after run.

Clear through _set_last_unloaded(None) instead: it clears both fields under the
module lock and unlinks the slots on its way out.

The test could not have caught this, which is the other half of the report. It
seeded a manifest naming no files at all, and an empty manifest cannot tell a
clean-up that worked from one that never ran. It now writes a real slot file at
both ends and asserts the file is gone, not only that the field is None.
Reverting to the raw assignment fails it with "the KV slot file outlived the
manifest that named it".

test_openai_auto_switch.py: 698 passed, 1 skipped.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 31916aebb0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 33c04ec into main Sep 19, 2026
43 checks passed
@danielhanchen
danielhanchen deleted the fix/clear-the-idle-reload-stash-between-tests branch September 19, 2026 06:02
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
* Clear the idle-reload stash between tests

(Python 3.13, l-r) failed on main at 6448afc with

    FAILED tests/test_openai_auto_switch.py::test_chat_withheld_model_named_by_its_advertised_alias
    assert 500 == 404

and passed on the run before it and on every PR run of that shard. The captured
log has the whole mechanism: the request for "tiny" went looking for
unsloth/Idle-GGUF on the Hub and then died on

    AttributeError: '_B' object has no attribute 'load_model'

unsloth/Idle-GGUF is named nowhere in that test. It belongs to the idle-unload
tests, which set llama_keepwarm._last_unloaded_model through a real unload and
clear it only on the way IN, via their own _reset_keepwarm. Whichever of them
runs last in an xdist worker leaves the stash standing for every test after it,
in any module. The chat route reads that stash before it refuses anything and
reloads exactly what the idle loop freed, so a later test with doubles for
backends reloads a model it never named.

The route is not wrong: after an idle unload an alias or unknown name has to
stay servable, and that branch is what keeps it so. The leak is test isolation,
so the fix is an autouse fixture clearing the two stash fields around every
test, in the shape of the settings-memo fixture beside it, whose docstring
records this exact symptom from a different cause in unslothai#11241.

Only the stash fields. The counters next to them (_inflight, _pending,
_last_active) are live-traffic bookkeeping, and zeroing those around every test
would hide the kind of leak this exists to catch.

Robustification: one test pins the diversion itself, asserting WHICH model the
route went to load rather than the status code, since the status is only
whatever this file's double happens to be missing. It reproduces the CI failure
locally. The second drives the fixture directly, both ends, because an
ordering-based check passes by luck under --dist loadgroup.

Verified: test_openai_auto_switch.py 698 passed, 1 skipped. Neutering the
fixture fails the fixture test. The keepwarm, idle and resume files are 290
passed, 1 failed, and that one failure (test_bitsandbytes_adamw8bit_state_round_trips)
reproduces identically on clean main.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Delete the KV slots with the manifest, not just the reference

Assigning _kv_resume = None drops the only reference to a saved snapshot while
its slot files stay on disk. llama_keepwarm makes whoever takes the manifest
responsible for unlinking them, which is why take/purge and _set_last_unloaded
all end in _delete_resume_files. A fixture that clears the field by hand opts
out of that, so a suite which saves KV accumulates snapshots run after run.

Clear through _set_last_unloaded(None) instead: it clears both fields under the
module lock and unlinks the slots on its way out.

The test could not have caught this, which is the other half of the report. It
seeded a manifest naming no files at all, and an empty manifest cannot tell a
clean-up that worked from one that never ran. It now writes a real slot file at
both ends and asserts the file is gone, not only that the field is None.
Reverting to the raw assignment fails it with "the KV slot file outlived the
manifest that named it".

test_openai_auto_switch.py: 698 passed, 1 skipped.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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