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 repository structure and the relevant auth refresh code to understand the spurious generation bump race.
- Assistant
Next I'll open the cancel-recovery loop in
_coalesced_run_refresh_cmdand related call sites. - Assistant
Let me continue reading the rest of the relevant code.
- Assistant
I need
_coalesced_run_refresh_cmditself next, since that's where the early-cancel race lives. - Assistant
Let me read the
_coalesced_run_refresh_cmdfunction. - Assistant
Let me read more of _coalesced_run_refresh_cmd
- Assistant
I'll look for existing tests covering cancel recovery and generation bumping.
- Assistant
rg not found, use grep
- Assistant
There's already a test about phantom bump. Let me read the relevant test file carefully.
- Assistant
Let me read the concurrent cancel test and more context around the recommended fix.
- Assistant
Let me read more of the concurrent cancellation test setup.
- Assistant
I'll check whether
auth.pyis a facade and scan for related comments or TODOs. - Assistant
Checking sisyphus plans for extra guidance on this fix.
- Assistant
Implementing the fix: treat early cancel with no this-cycle inflight as no refresh, and only bump generation when a real inflight was observed.
- Assistant
Now I'll apply the edit carefully.
- Assistant
Verifying the remainder of the edit looks correct.
- Assistant
I'll write a temporary repro script for the early-cancel phantom-bump race, then run the existing race tests.
- Assistant
The create may have failed silently — checking and re-running the repro.
- Assistant
The create command didn't write the body correctly — I'll use the shell to write the repro script instead.
- Assistant
Confirming whether facade patches write through to the refresh module — that affects how the repro binds.
- Assistant
Next I'll run the existing refresh-race and related unit tests to check for regressions.
- Assistant
All race tests passed. Running a broader auth-related suite next.
- Assistant
I'll do a quick syntax check, clean up the temp repro, and review the final diff.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.