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). - Caller invokes
- Assistant
I'll start by exploring the relevant auth refresh code and understanding the cancel/generation-bump race described in the issue.
- Assistant
I'll open the refresh module and locate the cancel-recovery / generation-bump logic.
- Assistant
rg is not available, let me use grep instead.
- Assistant
Let me read the relevant sections of refresh.py carefully.
- Assistant
Let me continue reading more of this section.
- Assistant
Next I'll inspect
_coalesced_run_refresh_cmdand related tests to lock down the intended cancel/generation behavior. - Assistant
I'll review related tests and any prior discussion of this cancel-before-registration race.
- Assistant
Let me read the cancel-related parts of the test file more carefully, particularly around line 465-530.
- Assistant
I'll check for related issue notes and any existing guidance on the preferred fix.
- Assistant
Implementing the cleaner fix: re-raise immediately when
inflight is Noneso we never take the spurious generation-bump path. - Assistant
I'll apply the cancel-handler fix in
refresh.pynow. - Assistant
Removing the duplicate comment and fixing the wording.
- Assistant
I'll write a temporary reproducer that cancels before registration and checks for a phantom generation bump.
- Assistant
The create output was incomplete — rerunning the reproducer.
- Assistant
Strange empty output. Let me check the file content.
- Assistant
The create helper left the file empty — writing the reproducer with a shell redirect instead.
- Assistant
Validating against the unfixed code and ensuring the mock targets the correct module.
- Assistant
Confirming the old code still shows the phantom generation bump with the same reproducer.
- Assistant
Confirmed old vs fixed behavior. Running the related concurrency and lock-registry tests next.
- Assistant
All 13 related tests passed. Running a broader set of auth-related tests next.
- Assistant
Cleaning up the temp reproducer and reviewing the final diff.
- Assistant
Quick style check, then I'll submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.