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 issue and exploring the repository structure.
The issue is about a spurious
_REFRESH_GENERATIONSbump in_coalesced_run_refresh_cmdinsrc/notebooklm/_auth/refresh.py. The problem occurs when a task is cancelled before it registers a future in_REFRESH_INFLIGHT_BY_LOOP, leading to a phantom generation bump.Let me first explore the relevant file and understand the code.
Assistant
Let me look at the relevant files mentioned in the issue:
src/notebooklm/_auth/refresh.pyandsrc/notebooklm/auth.py.Assistant
Let me look at the relevant code in refresh.py around the mentioned lines.
Assistant
Let me just use
bash_terminalto open the file.Assistant
Let me find the
_coalesced_run_refresh_cmdfunction and look at the relevant code:Assistant
Let me look at the full relevant sections. I'll start with
_coalesced_run_refresh_cmdand the cancel-recovery loop.Assistant
Let me continue reading the rest of
_coalesced_run_refresh_cmd.Assistant
Now let me look at the cancel-recovery loop in
_fetch_tokens_with_refresh(around lines 440-560Assistant
Now let me also check the auth.py for the same logic (the issue mentions both files):
Assistant
Good -
auth.pyjust re-exports from_auth/refresh.py. I only need to fixrefresh.py.Let me look at the exact lines 487-510 more carefully:
Assistant
Now I have a clear understanding of the bug. Let me trace through the exact scenario:
- Line 489:
await _coalesced_run_refresh_cmd(...)— task is cancelled before it can register the future inside_coalesced_run_refresh_cmd - Line 495: Control jumps to
except asyncio.CancelledError: - Line 499:
inflight = registry.get(refresh_key)returnsNone - Line 500:
if inflight is None or inflight.done():→ True - Line 506:
if inflight is not None:→ False, skipped →subprocess_excstaysNone - Line 510:
break - Line 518:
if subprocess_exc is not None:→ False (it's still None) - Lines 532-533: Generation is spuriously bumped
- Line 535:
if caller_cancelled:→ True →raise asyncio.CancelledError()
The fix: when
inflight is Nonein the cancel handler, the subprocess was never registered, meaning no refresh happened. We should treat this as a failure by settingsubprocess_exc.- Line 489:
Assistant
Now let me implement the fix. I'll add an
elseclause for wheninflight is None:Assistant
Let me use the
replacecommand instead for multi-line editsAssistant
Let me try with heredoc syntax
Assistant
Let me use a simpler approach with Python to make the edit:
Assistant
Now I can see the exact structure. The
breakis at line 516. Let me use sed to add the else clause:Assistant
Now I'll use Python to make the precise edit:
Assistant
Let me verify the change looks correct:
Assistant
The fix looks correct. Now let me verify there are tests for this module:
Assistant
Let me look at the most relevant test files:
Assistant
Let me check the relevant integration tests
Assistant
Let me run the existing unit tests for the refresh module to ensure the fix doesn't break anything:
Assistant
Let me install the package in development mode first:
Assistant
Now let me run the tests:
Assistant
All tests pass. Now let me create a reproduction script to verify the fix works. Let me write a test that directly simulates the race condition:
Assistant
The fix works correctly. Let me also verify the diff to make sure the change is clean:
Assistant
Let me run a quick syntax check and import test:
Assistant
Let me run the broader unit test suite to make sure nothing is broken:
Assistant
The only failures are pre-existing issues in unrelated CLI/browser tests. All refresh-related tests pass. Let me also run the specific refresh integration tests:
Assistant
All integration tests pass as well. Let me also clean up the temp file:
Assistant
Let me do a final review of the changed code:
Assistant
The fix looks correct and all tests pass. Let me now go through the checklist:
Checklist:
-
[x] If an error was reported in the issue description, I have successfully reproduced it.
- The issue describes a race condition where
_REFRESH_GENERATIONSis bumped spuriously when a task is cancelled before registers its future. I traced through the code logic and confirmed the bug: when in the cancel handler, stays , causing the generation bump to execute despite no refresh having occurred. I also wrote a reproduction script that confirmed the bug path exists.
- The issue describes a race condition where
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.