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 to understand the codebase and locate the relevant file.
- Assistant
Let me examine both files to understand the current state of the code.
- Assistant
Let me scroll down to find the
_coalesced_run_refresh_cmdfunction. - Assistant
Let me look at the
_coalesced_run_refresh_cmdfunction. - Assistant
Let me scroll down to see the
_fetch_tokens_with_refreshfunction where the cancel-recovery loop is. - Assistant
Let me see more of the function to see the generation bump logic.
- Assistant
Now I have a clear understanding of the issue. Let me look at the exact lines involved in the cancel-recovery loop (lines 493-516).
- Assistant
Now I understand the issue clearly. Let me trace through the bug scenario:
await _coalesced_run_refresh_cmd(...)is called (line 489)- Task is cancelled before the future is registered (between the
try:andwith _REFRESH_STATE_LOCK:block in_coalesced_run_refresh_cmd) CancelledErroris caught at line 493inflight = registry.get(refresh_key)returnsNone(no future was registered)if inflight is None or inflight.done():is Trueif inflight is not None:is False, so inner block is skippedsubprocess_excstaysNone- Falls through to line 522:
if subprocess_exc is not None:— False - Spurious generation bump at line 538-543
- Then
if caller_cancelled: raise asyncio.CancelledError()at line 545
Let me check for existing tests:
- Assistant
Let me check the test files to understand what tests exist and how they're structured.
- Assistant
Let me also check the
auth.pyfile to see if it also has similar code: - Assistant
Now let me look at the existing tests to understand the test patterns used:
- Assistant
Now I understand the issue perfectly. Let me trace through the exact bug:
await _coalesced_run_refresh_cmd(...)is called at line 489- Cancellation happens before any future is registered in the registry (inside
_coalesced_run_refresh_cmd, between thetry:and thewith _REFRESH_STATE_LOCK:block) CancelledErroris caught at line 493registry.get(refresh_key)at line 500 returnsNone(no future was ever registered)inflight is None or inflight.done()→True, enters the blockif inflight is not None:→False, so inner block is skipped;subprocess_excstaysNonebreakexits the loopsubprocess_exc is not None→False, so the failure path is skipped- Spurious generation bump at lines 538-543
- Then
caller_cancelledisTrue, soraise asyncio.CancelledError()at line 550
The fix is simple: when
inflight is Nonein the cancel handler, the subprocess was never registered — no real refresh happened. We should immediately re-raise theCancelledErrorwithout bumping generation. - Assistant
Now I'll apply the fix. The key insight: when
inflight is None, the cancel arrived before any future was registered — no real refresh happened. We should immediately re-raiseCancelledErrorwithout bumping generation. - Assistant
Let me use the bash terminal to do the edit directly.
- Assistant
Let me use the special
editcommand with proper syntax: - Assistant
Let me open the file first and then use the edit command:
- Assistant
Now I can see the issue clearly. Let me use the
replacecommand to make the change. The fix is to handleinflight is Noneseparately frominflight.done():When
inflight is None, the cancel arrived before any future was registered. We should raiseCancelledErrorimmediately without bumping generation.When
inflight.done(), the subprocess has settled. We inspect its state and setsubprocess_excaccordingly.Let me use the
replacecommand: - Assistant
Now let me apply the fix using the
replacecommand: - Assistant
The fix looks correct. Let me verify the full diff:
- Assistant
Good. The diff is clean and minimal. Now let me run the existing tests to make sure nothing is broken:
- Assistant
Let me install the package first.
- Assistant
Now let me run the existing tests:
- Assistant
Now let me also run the other test files that were mentioned earlier:
- Assistant
All 322 unit tests pass. Let me also run the integration test:
- Assistant
All existing tests pass. Let me also run the full test suite:
- Assistant
The failures are in
cli/test_session.pyandtest_windows_compatibility.pywhich are unrelated to my change (Playwright/browser tests). Let me verify these were pre-existing: - Assistant
Good - the failures are pre-existing (missing playwright module). Let me restore my changes:
- Assistant
Let me also run all the refresh-related tests and the concurrency tests specifically:
- Assistant
All 64 related tests pass. Let me verify the final state of the diff:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.