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 then look at the specific file mentioned in the issue.
- Assistant
Let me make one command at a time.
- Assistant
Let me check if the _auth directory exists.
- Assistant
Let me look at the relevant code in
refresh.py: - Assistant
Now let me look at the
_coalesced_run_refresh_cmdfunction: - Assistant
Let me look at the full
_coalesced_run_refresh_cmdfunction. - Assistant
Now let me see the full context of the cancel handler to understand the issue completely:
- Assistant
Now let me check if the same code exists in
auth.pyas well: - Assistant
Good,
auth.pyjust imports from_auth/refresh.py. The fix only needs to be in_auth/refresh.py. Let me also check if there are any existing tests related to this: - Assistant
Let me check the existing tests:
- Assistant
Let me look at the refresh_cmd_race test file to understand the testing pattern better.
- Assistant
Now I understand the issue clearly. Let me look at the specific lines I need to modify:
- Assistant
Now I understand the issue clearly. The bug is in the cancel handler (lines 501-516): when
inflight is None(meaning no subprocess was ever registered because the task was cancelled before_coalesced_run_refresh_cmdregistered a future), the code falls through withsubprocess_exc = None, and then bumps the generation at line 543 — a phantom bump since no refresh actually happened.The fix is to raise immediately when
inflight is None— the subprocess never registered, so there's nothing to wait for and no generation bump should occur. - Assistant
Let me verify the fix by looking at the diff:
- Assistant
The diff looks correct. Now let me run the existing tests to make sure I haven't broken anything:
- Assistant
Let me install the package first:
- Assistant
Now let me run the relevant tests:
- Assistant
Good, the unit tests pass. Let me also run the concurrency tests:
- Assistant
Let me also run the cancellation propagation test:
- Assistant
Let me run the remaining relevant test files:
- Assistant
All 325 tests pass. Now let me write a reproduction script to verify the fix handles the race condition described in the issue:
- Assistant
The fix works correctly. Let me also run the full unit test suite to make sure nothing is broken:
- Assistant
The 12 failures and 21 errors are all in
tests/unit/cli/test_session.pyandtests/unit/test_windows_compatibility.pywhich are about playwright/browser sessions — completely unrelated to our change. Let me verify these are pre-existing: - Assistant
The test failures are pre-existing (playwright is not installed). Let me restore our changes:
- Assistant
Now let me also run just the auth-related and concurrency tests to double check:
- Assistant
All 338 relevant tests pass. Let me do a final review of the change:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.