Skip to content
TheChatArchiveThe Chat Archive
QwenQwen/Qwen3.6-27B· alibabaHugging 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

    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. Qwen Assistant

    Let me analyze the issue. The problem is in src/notebooklm/_auth/refresh.py in the _coalesced_run_refresh_cmd function. There's a race condition where _REFRESH_GENERATIONS[refresh_key] can be bumped even when no real refresh happened.

    Let me first explore the repository structure and find the relevant files.

  3. Qwen Assistant

    Now let me open the refresh.py file to understand the code structure.

  4. Qwen Assistant

    I need to use the correct format for the open command. Let me try again with just the file path.

  5. Qwen Assistant

    Now let me find the _fetch_tokens_with_refresh function which is the caller that has the cancel-recovery loop:

  6. Qwen Assistant

    Now let me look at the full _fetch_tokens_with_refresh function to understand the issue better:

  7. Qwen Assistant

    Now I understand the issue. Let me analyze the problem:

    1. In the cancel-recovery loop (lines 487-520), when a CancelledError is caught, the code checks if the inflight future exists.
    2. If inflight is None (which happens when the task is cancelled between try: and the first with _REFRESH_STATE_LOCK: block inside _coalesced_run_refresh_cmd), it breaks out of the loop with subprocess_exc = None.
    3. Then, at lines 538-543, since subprocess_exc is None, the generation is bumped even though no subprocess actually ran.

    The fix suggested in the issue is to track whether inflight was ever non-None during the cancel-recovery loop, and only bump generation if subprocess_exc is None AND observed_inflight is True.

    Let me also check if there are tests for this:

  8. Qwen Assistant

    Now I understand the codebase and the issue well. Let me look at the specific lines mentioned in the issue (487-537):

  9. Qwen Assistant

    Now I understand the issue. Let me trace the race condition:

    1. When the task is cancelled between try: (line 488) and the first with _REFRESH_STATE_LOCK: block inside _coalesced_run_refresh_cmd (line 172), no future is registered in _REFRESH_INFLIGHT_BY_LOOP.

    2. When CancelledError is caught (line 493), registry.get(refresh_key) returns None (line 500).

    3. if inflight is None or inflight.done(): (line 501) is taken since inflight is None.

    4. The inner if inflight is not None: block (line 509) is skipped, so subprocess_exc stays None.

    5. break (line 516) exits the loop.

    6. Since subprocess_exc is None, the code proceeds to lines 538-543 and bumps the generation, even though no subprocess actually ran.

    The fix is to track whether inflight was ever observed as non-None during the cancel-recovery loop. Only bump generation if subprocess_exc is None AND an inflight future was actually observed.

    Let me create a test script to reproduce the issue first:

  10. Qwen Assistant

    I've confirmed the bug. The generation is bumped even when no subprocess ran. Now let me implement the fix.

    The issue is in the cancel-recovery loop (lines 485-550). When inflight is None (because the cancel happened before the future was registered), the code breaks out with subprocess_exc = None, and then proceeds to bump the generation.

    The fix is to track whether inflight was ever observed as non-None. Only bump generation if subprocess_exc is None AND an inflight future was actually observed.

    Let me implement the fix:

  11. Qwen Assistant

    I need to use the correct syntax for the edit command. Let me edit the file to fix the bug.

  12. Qwen Assistant

    Let me try using sed to make the changes:

  13. Qwen Assistant

    Let me fix the sed command syntax - I need to use different quotes.

  14. Qwen Assistant

    I need to use a different approach for editing. Let me create a Python script to make the edit.

  15. Qwen Assistant

    Now let me verify the patch was applied correctly:

  16. Qwen Assistant

    The patch looks correct. Now let me run the reproduction test to verify the fix works:

  17. Qwen Assistant

    The fix works. Now let me run the existing tests to make sure I haven't broken anything:

  18. Qwen Assistant

    Let me check what's going on with the imports:

  19. Qwen Assistant

    The tests need pytest-asyncio. Let me install it properly:

  20. Qwen Assistant

    All 4 tests pass. Let me also run the other related test files:

  21. Qwen Assistant

    Let me also run any other auth-related tests:

  22. Qwen Assistant

    The test errors are due to a missing httpx_mock fixture, not related to my changes. All 320 tests passed (only 2 errors due to missing fixture).

    Now let me also think about edge cases:

    1. Normal successful path: When _coalesced_run_refresh_cmd completes successfully (line 492), observed_inflight is set to True before breaking. ✅
    2. Cancel with inflight registered (and still running): When cancelled and the inflight exists and is still running, observed_inflight = True is set, and we loop back to await. ✅
    3. Cancel with inflight settled: When cancelled and inflight exists but is done, observed_inflight = True is set inside if inflight is not None:. ✅
    4. Cancel with no inflight (early cancel): When cancelled and inflight is None, observed_inflight stays False, and we break. Then if observed_inflight: prevents the generation bump. ✅
    5. Non-cancel exception: When a BaseException is caught (line 518-520), subprocess_exc is set and the generation bump is skipped by if subprocess_exc is not None:. observed_inflight doesn't matter here since we raise anyway. ✅

    Let me run the full test suite to be sure:

  23. Qwen Assistant

    I need to run the reproduction test one more time to confirm the fix works, then check git diff and submit. The errors in the test run are all related to missing httpx_mock fixture which is unrelated to my changes. )

    <tool_call> <function=bash_terminal> <parameter=command> cd /notebooklm-py && python test_reproduce_race.py && echo "---" && git diff src/notebooklm/_auth/refresh.py </tool_call>

  24. Qwen Assistant

    The user wants me to test the fix and check the git diff. Let me do that.

  25. Qwen Assistant

    The fix is working - the generation is no longer bumped when no subprocess ran. Let me check the git diff to see the full changes.

  26. Qwen Assistant

    The fix is verified. Let me clean up the test file I created and submit the changes.

  27. Qwen Assistant

    Now let me verify the final state and submit.

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