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)
Caller invokes await _coalesced_run_refresh_cmd(refresh_key, path, profile) from the cancel-recovery loop in _fetch_tokens_with_refresh.
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].
Control jumps to except asyncio.CancelledError: in the caller.
registry.get(refresh_key) returns None.
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.
Outer if subprocess_exc is not None: is False, so no raise.
Generation is bumped at the with _REFRESH_STATE_LOCK: ... max(existing, refresh_generation + 1) line, even though no subprocess ran.
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).
Assistant
Now apply the fix.
Assistant
Checklist review:
Reproduced the reported error: Yes. I wrote a temporary script (/tmp/repro.py, since removed) that patches _coalesced_run_refresh_cmd to raise CancelledErrorbefore registering an in-flight future, then drives _fetch_tokens_with_refresh through the auth-expiry path. On unmodified code it printed generation: 1 → phantom bump confirmed.
Confirmed the fix resolves it: Yes. With the patch the same script prints generation: 0 and still raises CancelledError to the caller; I verified the before/after difference via / .
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
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.
Original code: src/notebooklm/_auth/refresh.py:487-537 (tier-10), src/notebooklm/auth.py:961-1026 (tier-9 E origin)
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-$
git stash
git stash pop
Edge cases: Considered all four cancel-recovery paths — (1) normal success (observed_inflight=True → bump kept), (2) cancel while a registered future is still running (loop re-awaits, still bumps after success), (3) cancel with a done future that failed/was cancelled (subprocess_exc path, no bump, unchanged), (4) cancel before registration (new: no bump, cancellation propagated). Also checked that the registry intentionally never removes settled futures (_settle leaves them in place) and is keyed per event loop, so inflight is None can only mean "never registered on this loop" — no false negatives. The facade in auth.py only forwards, so no duplicate copy needed patching.
Ran existing tests: Yes. tests/integration/concurrency (94 passed, incl. all four refresh-cmd race/cancel tests), tests/unit/test_refresh_lock_registry.py, tests/unit/test_auth_cookie_save_race.py, tests/unit/test_public_shims.py all pass after installing missing dev deps (pytest-asyncio, pytest-httpx, vcrpy). The full tests/unit run has 12 failures/21 errors that are all Playwright/browser-environment issues, identical before and after my change. ruff check and ruff format --check are clean on the modified file. I did not modify or add any tests; I also added a CHANGELOG "Fixed" entry.