Skip to content
TheChatArchiveThe Chat Archive
MiniMaxminimax/minimax-m3· unknownHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION auth: spurious REFRESHGENERATIONS bump on early-cancel in…

  1. User

    ISSUE DESCRIPTION

    auth: spurious _REFRESH_GENERATIONS bump on early-cancel in _coalesced_run_refresh_cmd

    Summary

    _coalesced_run_refresh_cmd in src/notebooklm/_auth/refresh.py (moved from src/notebooklm/auth.py in tier-10 PR-B-high #814, but the code is byte-identical to origin/main since tier-9 E #803) can bump _REFRESH_GENERATIONS[refresh_key] even when no real refresh happened, in a narrow cancel-before-await race.

    Reproducer (race condition)

    1. Caller invokes await _coalesced_run_refresh_cmd(refresh_key, path, profile) from the cancel-recovery loop in _fetch_tokens_with_refresh.
    2. Task is cancelled in the window between try: and the first with _REFRESH_STATE_LOCK: block inside _coalesced_run_refresh_cmd — so NO future is registered in _REFRESH_INFLIGHT_BY_LOOP[loop][refresh_key].
    3. Control jumps to except asyncio.CancelledError: in the caller.
    4. registry.get(refresh_key) returns None.
    5. The inner if inflight is None or inflight.done(): is taken; the if inflight is not None: block is skipped; subprocess_exc stays None; break.
    6. Outer if subprocess_exc is not None: is False, so no raise.
    7. Generation is bumped at the with _REFRESH_STATE_LOCK: ... max(existing, refresh_generation + 1) line, even though no subprocess ran.
    8. Then if caller_cancelled: raise asyncio.CancelledError().

    Impact

    A concurrent waiter on a different event loop sharing the same refresh_key would observe the phantom generation bump on its lock-re-acquire path, treat the storage as freshly-refreshed, and skip its own _run_refresh_cmd — proceeding to build_httpx_cookies_from_storage against STALE on-disk state.

    Bounded blast radius: the stale-storage caller will fail again on the next token fetch and trigger its own real refresh. No data corruption. But it's a wasted round-trip.

    Suggested fix (from CodeRabbit on PR #814)

    Track whether inflight was ever non-None during the cancel-recovery loop. Only bump generation if subprocess_exc is None AND observed_inflight is True. Or per the alternative suggestion: raise immediately when inflight is None at the top of the cancel handler (the subprocess never registered, so we can't be the leader-cancel-before-await case for an actually-running refresh).

    Why not fixed in PR #814

    That PR's scope was "extract verbatim from auth.py into _auth/refresh.py + facade-forward". Changing async semantics was explicitly out of scope (must_not_do). Filing here for a dedicated follow-up.

    References

    Interface

    No new interfaces are introduced.

    ADDITIONAL ADVICE

    Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use git checkout <file> to restore the file to its original state. You can also reset all changes in the repository using git reset --hard command. Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to get the changes that need to be submitted.

    CHECKLIST

    Before submitting your solution, please go over the following checklist and make sure you've done everything:

    • If an error was reported in the issue description, I have successfully reproduced it.
    • If an error was reported in the issue description, I have confirmed that my fix resolves the error.
    • I have thought about edge cases and made sure my fix handles them as well.
    • I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.

    Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!

    (Current directory: /notebooklm-py, current file: none) bash-$

  2. MiniMax Assistant

    Let me start by exploring the repository structure to understand the codebase

  3. MiniMax Assistant

    Let me see the actual _coalesced_run_refresh_cmd function and the surrounding code

  4. MiniMax Assistant

    Now let me look at the _fetch_tokens_with_refresh function more carefully to understand the cancel-recovery loop

  5. MiniMax Assistant

    me explore the tests for refresh to understand how to verify the fix

  6. MiniMax Assistant

    Let me check how auth.py imports things from _auth.refresh

  7. MiniMax Assistant

    Let me check the imports and module-level state of refresh.py

  8. MiniMax Assistant

    Now let me first create a reproducer script to confirm the bug

  9. MiniMax Assistant

    let me write a reproducer script to confirm the bug exists

  10. MiniMax Assistant

    Let me check the actual function being called in auth.py

  11. MiniMax Assistant

    The auth module re-exports the symbols from _auth_refresh. The issue is that when I do auth_mod._coalesced_run_refresh_cmd = fake_coalesced, I'm setting it on the auth module, but the actual function used inside _fetch_tokens_with_refresh (which lives in _auth_refresh) is the local reference.

    Let me check if there's a facade write-through mechanism:

  12. MiniMax Assistant

    Let me update my reproducer to use the facade properly

  13. MiniMax Assistant

    The bug is confirmed. Now let me apply the fix using the observed_inflight flag approach as the primary suggestion from the issue.

  14. MiniMax Assistant

    Let me try using the replace command instead

  15. MiniMax Assistant

    me verify the change was made correctly

  16. MiniMax Assistant

    The fix is in place. Now let me run the reproducer to confirm it fixes the bug:

  17. MiniMax Assistant

    The fix works. Now let me verify the existing tests pass:

  18. MiniMax Assistant

    existing tests pass. Let me also run a broader test suite to ensure nothing is broken:

  19. MiniMax Assistant

    The test failures are unrelated to our fix (they're about playwright/CLI session tests). Let me verify these failures are pre-existing by checking against the original code:

  20. MiniMax Assistant

    found a bug in my fix. The success path (normal await) doesn't set observed_inflight = True, so a normal successful refresh would also skip the generation bump. Let me fix this:

  21. MiniMax Assistant

    Let me verify the fix and then re-run the tests:

  22. MiniMax Assistant

    let me write a comprehensive reproducer to test all the edge cases:

  23. MiniMax Assistant

    Let me simplify the test by using the real _coalesced_run_refresh_cmd where possible:

  24. MiniMax Assistant

    5 scenarios pass. Let me verify the test would have failed with the original code by temporarily reverting the fix:

  25. MiniMax Assistant

    The original code fails scenario 5 with a phantom bump. The fix is correctly applied. Let me re-run the test suite to make sure nothing else is broken:

  26. MiniMax Assistant

    338 tests pass. Let me do a final review of the change:

  27. MiniMax Assistant

    The diff is clean and minimal. Let me run all the affected tests one more time to confirm everything works:

  28. MiniMax Assistant

    All 64 relevant tests pass. Let me also verify the wider test suite (excluding pre-existing playwright failures):

  29. MiniMax Assistant

    The 10 failures and 21 errors are all in tests/unit/cli/test_session.py (which I should not be ignoring — but it's a CLI test directory). Wait, I did --ignore=tests/cli but there are still test_session.py failures. Let me check what's going on.

    Actually wait, I did --ignore=tests/cli. But the errors are from tests/unit/cli/test_session.py. The --ignore=tests/cli ignores the tests/cli/ directory, but tests/unit/cli/ is a different directory.

    Let me check the structure:

  30. MiniMax Assistant

    4211 tests pass (excluding pre-existing CLI/playwright failures). Let me clean up my reproducer files and verify the final state:

  31. MiniMax Assistant

    Summary

    The fix is complete. Here's a summary of the changes:

    Issue

    _coalesced_run_refresh_cmd could bump _REFRESH_GENERATIONS[refresh_key] even when no subprocess ran, in the narrow cancel-before-register race described in the issue.

    Fix

    Following the primary suggestion from the issue (the observed_inflight flag approach), I added a boolean flag that tracks whether the in-flight future was ever observed during the cancel-recovery loop:

    1. Set to True after normal completion (the subprocess definitely ran if await _coalesced_run_refresh_cmd(...) returns normally — the shielded future was resolved)
    2. Set to True inside the except asyncio.CancelledError: handler when inflight is not None (the in-flight future was registered, so a subprocess ran or is running)
    3. The generation bump is now wrapped in if observed_inflight: — only happens when a subprocess was actually driven

    Edge cases handled

    • Normal success: gen=1 (bumped) ✓
    • Refresh fails: gen=0 (no bump) ✓
    • Cancel before register (the bug): gen=0 (no phantom bump) ✓
    • Cancel mid-flight, subprocess succeeds: gen=1 (bumped for other callers) ✓
    • Cancel mid-flight, subprocess fails: gen=0 (no bump) ✓

    Checklist

    • ✅ Reproduced the bug with a script that simulates the cancel-before-register race (gen went from 0 to 1)
    • ✅ Confirmed the fix prevents the phantom bump (gen stays at 0)
    • ✅ Tested all edge cases for the cancel-recovery loop
    • ✅ All 4211 existing tests in the relevant test suite pass (excluding pre-existing CLI/playwright failures unrelated to this change)
    • ✅ Confirmed the original code FAILS scenario 5 (phantom bump) and the fix PASSES

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.