ISSUE DESCRIPTION auth: spurious REFRESHGENERATIONS bump on early-cancel in…
- User
ISSUE DESCRIPTION
auth: spurious _REFRESH_GENERATIONS bump on early-cancel in _coalesced_run_refresh_cmd
Summary
_coalesced_run_refresh_cmdinsrc/notebooklm/_auth/refresh.py(moved fromsrc/notebooklm/auth.pyin 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 firstwith _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)returnsNone.- The inner
if inflight is None or inflight.done():is taken; theif inflight is not None:block is skipped;subprocess_excstaysNone;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_keywould 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 tobuild_httpx_cookies_from_storageagainst 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
inflightwas ever non-None during the cancel-recovery loop. Only bump generation ifsubprocess_exc is None AND observed_inflight is True. Or per the alternative suggestion: raise immediately wheninflight is Noneat 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
- CodeRabbit comment: https://github.com/teng-lin/notebooklm-py/pull/814#discussion_r3256142201
- Original code:
src/notebooklm/_auth/refresh.py:487-537(tier-10),src/notebooklm/auth.py:961-1026(tier-9 E origin)
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 usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto 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-$
- Caller invokes
- Assistant
I’ll inspect the refresh implementation and its existing tests first, then reproduce the cancellation window with the project’s test tooling before making the smallest semantics-focused fix.
- Assistant
The generation update is in
_fetch_tokens_with_refresh’s recovery loop, as described, while_coalesced_run_refresh_cmdcreates the registry entry. I’ll now inspect that helper and the cancellation regression tests in detail to preserve the intended leader/waiter behavior. - Assistant
The existing tests cover cancellation after registration and cancellation racing settlement, but not cancellation before
_coalesced_run_refresh_cmdregisters anything. The robust minimal rule is to record whether recovery ever observed a shared future and only classify a no-exception exit as successful when one was observed; if none exists, immediately propagate caller cancellation and never bump generation. I’ll first build a temporary deterministic reproducer by monkeypatching the helper to raiseCancelledErrorbefore registration. - Assistant
The first reproducer did not enter refresh because the refresh command environment variable was unset, so the authentication error was correctly re-raised. I’ll rerun with the refresh feature enabled; this should expose generation
1on the current code. - Assistant
The reproducer confirms the phantom bump (
generation: 1). I’ll add an explicitobserved_inflightinvariant to the recovery loop and propagate cancellation before the success/bump path when no future was ever observed. This retains the successful in-flight cancellation behavior while preventing an unregistered attempt from being treated as a completed refresh. - Assistant
The deterministic reproducer now reports generation
0. Next I’ll run the full refresh race integration module and lock-registry unit tests, which exercise successful leader cancellation, failed settlement, same-loop coalescing, and cross-loop generation coordination. - Assistant
All 280 targeted tests pass, including the existing successful-cancellation contract, and lint/format checks are clean. I’ll do a final diff review and run the broader auth-focused unit suite to catch any facade or retry-flow regressions before submission.
- Assistant
Final checklist:
- Reproduced the reported error: Yes. A deterministic temporary script simulated cancellation before in-flight registration and observed the incorrect generation value
1. - Confirmed the fix resolves it: Yes. The same script now observes generation
0while cancellation still propagates. - Considered edge cases: Yes. The guard only affects cancelled callers that never observe an in-flight future; registered successful refreshes still bump generation, registered failures do not, and caller cancellation remains prioritized.
- Ran affected existing tests: Yes. The targeted refresh/concurrency/public-shim suites passed (
280 passed), the broader auth/refresh unit selection passed (754 passed, 2 skipped), and Ruff plusgit diff --checkpassed.
The working tree contains only the intended source change, so I’ll submit it now.
- Reproduced the reported error: Yes. A deterministic temporary script simulated cancellation before in-flight registration and observed the incorrect generation value
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →