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
Let me start by understanding the repository structure and finding the relevant code files mentioned in the issue.
The issue is about
_coalesced_run_refresh_cmdinsrc/notebooklm/_auth/refresh.py. Let me explore the repository structure first.Assistant
Let me look for the
_authdirectory which should have therefresh.pyfile.Assistant
Let's look at what's in that directory and open the refresh.py file.
Assistant
Let me open the refresh.py file to look at the
_coalesced_run_refresh_cmdfunction.Assistant
I need to use the bash_terminal tool, not the open tool directly. Let me use
openas a command.Assistant
Let me find the
_coalesced_run_refresh_cmdfunction. The issue says it's around lines 487-Assistant
Now let me look at the relevant sections:
_coalesced_run_refresh_cmdstarting at line 144 and_fetch_tokens_with_refreshat line 401Assistant
Let me use scroll_down to see the contents starting from line 144.
Assistant
Let me continue viewing the rest of the
_coalesced_run_refresh_cmdfunction.Assistant
Now let me look at the
_fetch_tokens_with_refreshfunction starting around line 401Assistant
Let me view the exact lines of the cancel handler and the generation bump section more precisely
Assistant
Now I understand the issue clearly. At line 506-514, when
inflight is None(meaning no future was ever registered because the task was cancelled before_coalesced_run_refresh_cmdentered itswith _REFRESH_STATE_LOCK:block),subprocess_excstaysNone, we break out of the loop, and the generation gets bumped spuriously at lines 537-543.The fix: when
inflight is None, we should setsubprocess_exc = asyncio.CancelledError()so the generation bump is skipped.Let me apply the fix:
Assistant
I see there are duplicate/remnant lines. The edit left old lines behind. Let me fix this by cleaning up lines 527-528
Assistant
There's still a duplicate line. Let me remove it.
Assistant
Good, the file looks clean now. Let me verify the complete modified section:
Assistant
Let me verify the full function with wider context to ensure correctness
Assistant
The code looks correct. Let me now run the relevant tests to make sure nothing is broken
Assistant
Let me now run the relevant unit tests
Assistant
Let me install the package first
Assistant
Now let me run the relevant
Assistant
All pass. Now let me run the other related test
Assistant
All pass. Let me also run any broader auth-related tests and the integration concurrency test
Assistant
Let me also run all auth-related tests to ensure nothing is
Assistant
The 4 failures are pre-existing and unrelated to my change (VCR cassette issues and a CLI string assertion). Let me verify my changes are clean
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.