Skip to content

Ask the spoof-survival question in a subprocess, not in the worker - #10866

Merged
danielhanchen merged 1 commit into
mainfrom
fix-spoof-leak-in-guard-test
Sep 13, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
fix-spoof-leak-in-guard-test

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Why

#10864 added tests/python/test_accelerator_skip_guards.py to stop tests gating hardware on a probe that tests/_zoo_aggressive_cuda_spoof.py patches process-wide. One of its own tests then did exactly the thing the file exists to prevent. My fault, and this is the fix.

test_the_recorded_answer_survives_the_spoof called spoof.apply() in-process. That sets torch.cuda.is_available to return True and never restores it, so the rest of the xdist worker runs believing there is a CUDA card. peft.infer_device() opens with:

if torch.cuda.is_available():
    return "cuda"

So tests/test_save_lora_without_vllm.py::test_the_saved_adapter_still_loads_back, scheduled onto the same worker, asked safetensors to load an adapter onto CUDA on a CPU-only box:

peft/utils/save_and_load.py -> safe_load_file(filename, device=device)
NotImplementedError: Could not run 'aten::empty_strided' with arguments from the 'CUDA' backend

Nothing in that names the cause, and the guard test itself passed. Which victim gets hit depends on how xdist distributes work, so it presented as a flake in an unrelated file.

Reproduction

Deterministic in two files, in this order:

pytest tests/python/test_accelerator_skip_guards.py tests/test_save_lora_without_vllm.py

before:  1 failed, 18 passed
after:  20 passed

The fix

The question goes to a subprocess, the same pattern tests/vllm_compat/test_unsloth_zoo_imports.py has used since #10855, and for the same reason: a question about global state has to be asked somewhere the answer cannot leak back. Coverage is unchanged. It still asserts that the spoof really does patch is_available (so the test cannot quietly become vacuous) and that the recorded answer does not move.

Also adds test_this_file_never_applies_the_spoof_in_process, an ast check that this file never regains an in-process spoof.apply(). Worth pinning structurally rather than trusting: the damage is invisible at its own site and lands on whichever test the scheduler runs next, which is precisely why it took two wrong hypotheses to find.

How I got it wrong, since it is relevant to reviewing this

Two bad calls worth recording:

  1. I ran a control that added only the new files to the base tree, saw the victim pass, and read that as "not caused by the change". It was not a valid control: that tree contained the offending test too, so it also applied the spoof, and only the scheduling differed. "Did not reproduce" meant "scheduling-dependent", not "not mine".
  2. I then blamed Hub contention, because the victim downloads hf-internal-testing/tiny-random-LlamaForCausalLM and I had run two suites concurrently. Plausible, untested, and wrong.

Both would have been avoided by capturing the traceback first. The comparison runs used --tb=no, so the first real diagnostic arrived several rounds late. The runs behind this PR use --tb=short.

Checks

  • Two-file repro: 1 failed, 18 passed to 20 passed.
  • The new guard is not vacuous: re-inserting an in-process spoof.apply() makes it fail and name the line number.
  • Full Repo tests (CPU) on this branch: 1 failed, 18523 passed, 497 skipped. The single failure is test_lint_exec_literals::test_the_baseline_matches_the_tree_it_was_recorded_against, which fails identically on clean d7e5c6baa with no changes applied, so it is pre-existing and separate.
  • ruff check --select E9,F63,F7,F82 clean; the pinned ruff-format-with-kwargs hook is a fixed point.

#10864 added tests/python/test_accelerator_skip_guards.py to stop tests gating
hardware on a probe the aggressive CUDA spoof patches process-wide. One of its
own tests then did exactly the thing the file exists to prevent.

test_the_recorded_answer_survives_the_spoof called spoof.apply() in-process.
That sets torch.cuda.is_available to return True and never restores it, so the
rest of the xdist worker runs believing there is a CUDA card. peft's
infer_device() opens with `if torch.cuda.is_available(): return "cuda"`, so
tests/test_save_lora_without_vllm.py::test_the_saved_adapter_still_loads_back,
scheduled onto the same worker, asked safetensors to load an adapter onto CUDA
and died with

    NotImplementedError: Could not run 'aten::empty_strided' with arguments from
    the 'CUDA' backend

Nothing about that names the cause, and the guard test itself passed. Which
victim gets hit depends on how xdist distributes work, so it looked like a flake
in an unrelated file for two rounds of investigation.

Reproduces deterministically in two files, in this order:

    pytest tests/python/test_accelerator_skip_guards.py \
           tests/test_save_lora_without_vllm.py
    before: 1 failed, 18 passed
    after:  20 passed

The question now goes to a subprocess, the same pattern
tests/vllm_compat/test_unsloth_zoo_imports.py uses since #10855, and for the
same reason: a question about global state has to be asked somewhere the answer
cannot leak back. Coverage is unchanged, it still asserts the spoof really does
patch is_available and that the recorded answer does not move.

Also adds test_this_file_never_applies_the_spoof_in_process, an ast check that
this file never regains an in-process spoof.apply(). Worth pinning structurally
because the damage is invisible at its own site and lands on whichever test the
scheduler happens to run next.

Full Repo tests (CPU) on this branch: 1 failed, 18523 passed, 497 skipped. The
one failure is test_lint_exec_literals::test_the_baseline_matches_the_tree_it_
was_recorded_against, which fails identically on clean d7e5c6b and is a
separate matter.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 13, 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-13T13:14:41.464183Z 431613c PR opened
🔒 Security Review ✅ Completed 2026-09-13T13:16:47.702454Z 431613c PR opened
ℹ️ 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 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: 431613c787

ℹ️ 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".

offenders = [
f"line {node.lineno}"
for node in ast.walk(tree)
if isinstance(node, ast.Call) and _dotted(node.func)[-2:] == ("spoof", "apply")

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 Detect aliases of the spoof module

This structural guard only rejects calls whose receiver is literally named spoof. If the in-process call is restored using _spoof.apply()—the alias already used by the version/vLLM compatibility tests—or any other import alias, offenders remains empty and the worker-wide CUDA patch can leak again. Resolve the alias imported from _zoo_aggressive_cuda_spoof, or otherwise detect calls to that module independently of the local variable name.

Useful? React with 👍 / 👎.

@danielhanchen
danielhanchen merged commit 1a6c33b into main Sep 13, 2026
14 of 15 checks passed
@danielhanchen
danielhanchen deleted the fix-spoof-leak-in-guard-test branch September 13, 2026 13:19
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