Skip to content
TheChatArchiveThe Chat Archive
GLMz-ai/glm-5.2· 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. GLM Assistant

    I'll start by exploring the repository structure and finding the relevant file mentioned in the issue.

  3. GLM Assistant

    Let me explore the repository structure and the relevant file first.

  4. GLM Assistant

    This shows the caller's cancel-recovery loop. Let me look at the actual _coalesced_run_refresh_cmd function to understand the inflight registration.

  5. GLM Assistant

    Let me look at the _coalesced_run_refresh_cmd function in detail.

  6. GLM Assistant

    Now let me look at the exact cancel-recovery loop in detail.

  7. GLM Assistant

    Now I understand the structure. Let me look at the cancel-recovery loop and the surrounding context more carefully, and check the existing tests.

  8. GLM Assistant

    Let me look at the relevant race/cancellation tests to understand testing patterns and ensure my fix aligns.

  9. GLM Assistant

    Let me look at the most relevant tests for the refresh command race and cancellation.

  10. GLM Assistant

    Let me understand the relationship between notebooklm.auth and notebooklm._auth.refresh, and look at the state machine test.

  11. GLM Assistant

    I need to make a single tool call. Let me combine them.

  12. GLM Assistant

    Now let me carefully analyze the issue. Let me re-read the cancel-recovery loop and understand the exact race scenario described.

    Let me look at the exact lines in the file to understand the full flow:

  13. GLM Assistant

    Now let me look at the broader _fetch_tokens_with_refresh function structure and the conftest for tests.

  14. GLM Assistant

    Let me check the conftest for unit tests and how the environment runs tests.

  15. GLM Assistant

    Now I understand the structure. Let me write a reproduction script to confirm the bug. I'll patch _coalesced_run_refresh_cmd to simulate the cancel-before-await race (cancellation delivered before the future is registered).

  16. GLM Assistant

    I need to use the virtual environment or install the package. Let me check.

  17. GLM Assistant

    I've reproduced the bug — generation bumped to 1 with no real refresh. Now let me apply the fix. I'll use the approach of raising immediately when inflight is None (no subprocess ever registered). Let me view the exact lines to edit.

  18. GLM Assistant

    Now let me open the file and apply the fix.

  19. GLM Assistant

    Now I'll apply the fix. I'll modify the cancel handler so that when inflight is None (no subprocess ever registered — the cancel-before-await race), it raises CancelledError immediately without proceeding to the generation bump.

  20. GLM Assistant

    The fix is applied. Now let me run the reproduction script again to confirm the bug is fixed.

  21. GLM Assistant

    The bug is fixed. Now let me run the existing relevant tests to ensure I didn't break anything.

  22. GLM Assistant

    All passing. Let me run the broader auth/refresh test suite to be thorough.

  23. GLM Assistant

    All tests pass. Let me also run a broader sweep of the unit and integration concurrency tests to be safe, and verify the cross-loop scenario mentioned in the issue.

  24. GLM Assistant

    The error is unrelated to my change (missing playwright module for a CLI session test). Let me exclude those and run the rest.

  25. GLM Assistant

    The only failure is a playwright-related test (missing module), unrelated to my change. Let me confirm it's pre-existing by checking it.

  26. GLM Assistant

    Confirmed — the playwright failure is pre-existing and unrelated to my change. Let me review my final diff.

  27. GLM Assistant

    The diff is clean and minimal. Let me clean up the temporary reproduction script and run the most relevant tests one final time.

  28. GLM Assistant

    All tests pass. Let me confirm the final git status shows only my intended change.

  29. GLM Assistant

    The working tree contains only my intended change to src/notebooklm/_auth/refresh.py, with no stray test files. Let me do a final review of the complete modified cancel-recovery loop to ensure correctness.

  30. GLM Assistant

    The fix is complete and verified. Let me summarize my work against the checklist before submitting.

    Summary

    Issue: _coalesced_run_refresh_cmd's cancel-recovery loop in src/notebooklm/_auth/refresh.py could bump _REFRESH_GENERATIONS[refresh_key] even when no real refresh happened, in a narrow cancel-before-await race where the caller's task is cancelled before the in-flight future is registered in the per-loop registry.

    Root cause: In the except asyncio.CancelledError: handler, when inflight is None (no subprocess ever registered), the code fell through to break with subprocess_exc still None, then proceeded to the generation-bump block — producing a phantom bump that could make a concurrent cross-loop waiter treat stale on-disk storage as freshly-refreshed.

    Fix: Split the if inflight is None or inflight.done(): check. When inflight is None, raise asyncio.CancelledError() immediately — no subprocess ran on this loop, so no generation bump; the cancellation propagates and the next caller observes should_run_refresh == True to run a real refresh. The inflight.done() branch retains its original terminal-state inspection (now without the redundant if inflight is not None: guard, since None is already handled).

    Checklist

    • Reproduced the error: Yes — I wrote a temporary script that patched _coalesced_run_refresh_cmd to yield (suspend) before registering the in-flight future, then cancelled the caller during that window. It confirmed the generation was bumped to 1 with no real refresh (BUG REPRODUCED: generation bumped to 1 with NO real refresh!).

    • Confirmed the fix resolves the error: Yes — after the fix, the same script showed generation: 0 / OK: no phantom bump. and the CancelledError propagated correctly.

    • Considered edge cases: Yes — I traced all 7 control-flow paths through the cancel-recovery loop. The fix only alters the inflight is None path (immediate re-raise, no bump). Normal-success, cancel+inflight-not-done (re-await loop), cancel+inflight-done-with-exception (no bump, raise), cancel+inflight-done-success (bump, then raise cancel), and non-cancel exceptions all behave exactly as before — real successful refreshes still bump the generation.

    • Ran existing tests: Yes — 331 refresh/auth/cancellation/state-machine tests pass, plus a broader sweep of 5216 unit+integration tests pass. The only failures (test_playwright_initializes_on_non_windows and a CLI session test) are pre-existing and caused by the missing playwright module; I confirmed they fail identically on the unmodified code via git stash.

    I removed the temporary reproduction script before submitting.

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