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 what we're working with.

  3. MiniMax Assistant

    Now let me look at _coalesced_run_refresh_cmd function:

  4. MiniMax Assistant

    Now I have a clear picture. Let me also look at the existing tests for this code path:

  5. MiniMax Assistant

    Now I have a thorough understanding. Let me look at the exact area around lines 487-537 of the file:

  6. MiniMax Assistant

    Now I fully understand the issue. Let me also look at git history for context:

  7. MiniMax Assistant

    Let me first create a reproducer to confirm the bug exists:

  8. MiniMax Assistant

    The reproducer confirms the bug. Generation went from 0 to 1 even though no subprocess ever ran.

    Now let me apply the fix. I'll go with the simpler "raise immediately when inflight is None" approach:

  9. MiniMax Assistant

    Now I'll apply the fix:

  10. MiniMax Assistant

    me use the edit command directly instead:

  11. MiniMax Assistant

    Now let me verify the fix works by running the reproducer:

  12. MiniMax Assistant

    The fix works. Now let me think about edge cases and add more comprehensive tests.

    Let me think through all the scenarios:

    1. Normal happy path: subprocess runs successfully → no cancellation → no exception → generation bumped ✓
    2. Subprocess fails: subprocess raises → except BaseException → subprocess_exc set → generation not bumped ✓
    3. Caller cancelled before inflight registered (the bug scenario): inflight is None → raise immediately → generation not bumped ✓ (new behavior)
    4. Caller cancelled while subprocess in flight (not yet settled): re-await shielded future, subprocess settles, success → break, bump generation, then raise CancelledError ✓
    5. Caller cancelled after subprocess settles with failure: inflight.done() → subprocess_exc set → generation not bumped ✓
    6. Caller cancelled after subprocess settles with success: inflight.done() → subprocess_exc is None → break → bump generation → raise CancelledError ✓
    7. Cancel/settle race: subprocess settles right when caller is cancelled → inflight.done() True → subprocess_exc set appropriately → generation not bumped on failure ✓ (already covered)

    Let me also make sure the existing tests still pass. Let me also add a test scenario for case 4 specifically — caller cancelled while subprocess is in-flight.

    Let me first run the existing test suite for the refresh tests:

  13. MiniMax Assistant

    All existing tests pass. Let me also run the broader auth tests:

  14. MiniMax Assistant

    All auth and refresh related tests pass. Let me also run the broader unit tests:

  15. MiniMax Assistant

    The failures look like they're related to playwright/browser testing, not to my changes. Let me verify by checking against the unmodified state:

  16. MiniMax Assistant

    : the playwright tests fail without my changes due to missing playwright module. These are pre-existing environmental failures, not regressions from my fix.

    Let me run the rest of the test suite excluding playwright tests:

  17. MiniMax Assistant

    All 4534 unit tests pass. Let me also run the integration tests, excluding the playwright-dependent ones:

  18. MiniMax Assistant

    All 670 integration tests pass. Now let me extend my reproducer to cover additional edge cases:

  19. MiniMax Assistant

    All edge cases pass. Now let me also verify the fix works for the case where the future IS in the registry (cancel during in-flight subprocess) — the existing tests already cover this, but let me also write a reproducer for case 4 and case 5 to be thorough:

  20. MiniMax Assistant

    All edge cases pass correctly. Let me also verify the fix works through the auth.py facade (since the issue mentions the facade write-through):

  21. MiniMax Assistant

    The fix works correctly through the facade as well. Let me also do a thorough re-review of the diff to make sure it's correct:

  22. MiniMax Assistant

    The diff is minimal and focused. Let me also clean up the unused if inflight is not None check inside the if inflight.done(): block (since we now know inflight is non-None at that point):

  23. MiniMax Assistant

    Let me also verify the inner if inflight is not None: block is still correct (now unreachable but harmless due to the early raise). The defensive check helps with mypy narrowing. I'll leave it as-is to minimize the change.

    Let me run the relevant tests one more time:

  24. MiniMax Assistant

    All tests pass. Let me also run mypy to make sure the type annotations are still correct:

  25. MiniMax Assistant

    No mypy errors. Let me also run ruff to make sure formatting/linting is clean:

  26. MiniMax Assistant

    All checks pass. Let me also look at the final state of the fix:

  27. MiniMax Assistant

    Let me verify the bug is present without the fix by temporarily reverting and running the reproducer:

  28. MiniMax Assistant

    The fix is verified:

    • Without the fix: phantom generation bump (gen=1) — bug present
    • With the fix: no phantom generation bump (gen=0) — bug fixed

    Let me clean up my reproducer scripts and ensure the fix is final:

  29. MiniMax Assistant

    The fix is complete. Let me run one more comprehensive test to make sure nothing is broken:

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