Skip to content

Stop pinning the gc freeze count in test_external_freeze_is_left_alone - #18237

Merged
danielhanchen merged 1 commit into
mainfrom
ci/gc-freeze-count-refcount
Nov 18, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
ci/gc-freeze-count-refcount

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Stop pinning the gc freeze count in test_external_freeze_is_left_alone

tests/test_gc_train_scope.py::test_external_freeze_is_left_alone failed on a Repo tests (CPU, rest) run whose base already has every recent fix (#18220, a frontend-only PR):

E   assert 1364742 == 1364744
E    +  where 1364742 = <built-in function get_freeze_count>()

Cause

The test freezes the heap itself, runs train(), and asserts gc.get_freeze_count() is exactly what it was before. That count is not stable. An object in the permanent generation is still freed by refcounting, and CPython drops it from the count when it goes. So any frozen object released while train() runs lowers the count, and the assert fails with no change in behaviour. Here, two objects went.

gc.collect(); x = [[] for _ in range(5)]; gc.freeze(); b = gc.get_freeze_count(); del x
# b = 12084, gc.get_freeze_count() = 12078

The scope code in unsloth/models/_gc_train_scope.py is correct: it leaves a freeze it does not own alone. Only the test's way of checking that is wrong.

Fix

The test now checks the claim directly instead of comparing a counter that can move on its own:

  • It spies on gc.collect, gc.freeze and gc.unfreeze through the scope module, the same way test_wrapper_captured_after_install_freezes_once already does.
  • It asserts that train() called none of them.
  • It asserts that the heap is still frozen both inside train() and after it.

A new test, test_freeze_count_falls_when_a_frozen_object_is_freed, pins the CPython behaviour above. It documents why the count is not compared.

Verification

  • tests/test_gc_train_scope.py: 17 passed.
  • Sabotage checks:
    • Letting the scope freeze on top of an external freeze (dropping the gc.get_freeze_count() > 0 early return) fails this test and test_after_step_needs_an_owned_freeze.
    • Making the scope always call gc.unfreeze() on exit fails this test.
  • tests/python/test_accelerator_skip_guards.py and tests/studio/test_backend_ci_*.py pass, and ruff is clean.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T02:30:26.802395Z 4c1a165 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 Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 4c1a16547b

ℹ️ 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 5e5dd64 into main Nov 18, 2026
@danielhanchen
danielhanchen deleted the ci/gc-freeze-count-refcount branch November 18, 2026 02:31
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