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 files. Let me first look at the directory.
- Assistant
Let me look at the auth.py file and the _auth/refresh.py file mentioned in the issue.
- Assistant
Let me look at the refresh.py file to understand the current implementation.
- Assistant
Let me open the file and look at the relevant section of
_fetch_tokens_with_refresh: - Assistant
Let me look at lines 401 onwards to see the
_fetch_tokens_with_refreshfunction: - Assistant
Let me continue reading to see the full cancel-recovery loop section.
- Assistant
Now I understand the issue. Let me also look at the
_coalesced_run_refresh_cmdfunction to see where the race condition occurs: - Assistant
Now I understand the issue. Let me trace through the bug:
_fetch_tokens_with_refreshcalls_coalesced_run_refresh_cmd- Task is cancelled before the
with _REFRESH_STATE_LOCK:block inside_coalesced_run_refresh_cmd(line 172) — NO future is registered - Control jumps to
except asyncio.CancelledError:in caller (line 493) registry.get(refresh_key)returnsNoneif inflight is None or inflight.done():is taken, butif inflight is not None:block is skippedsubprocess_excstaysNone;breakif subprocess_exc is not None:is False- Generation is bumped even though no subprocess ran
- Then
if caller_cancelled: raise asyncio.CancelledError()
The fix: when
inflight is None(the subprocess never registered because cancellation happened before await), we should treat it as a failure and setsubprocess_excso the generation bump is skipped.Let me also check the existing tests to understand the test structure:
- Assistant
Let me find the actual test files.
- Assistant
Now let me understand the fix needed. Looking at lines 493-516 in the cancel handler:
pythonexcept asyncio.CancelledError: caller_cancelled = True registry = _get_inflight_registry() with _REFRESH_STATE_LOCK: inflight = registry.get(refresh_key) if inflight is None or inflight.done(): if inflight is not None: if inflight.cancelled(): subprocess_exc = asyncio.CancelledError() else: subprocess_exc = inflight.exception() break - Assistant
Let me look at the test files related to refresh race conditions:
- Assistant
Let me look at tests related to refresh cancellation and the refresh cmd race:
- Assistant
Let me check the refresh cancellation propagation test and the refresh cmd race test:
- Assistant
Now I have a clear understanding of the issue. Let me implement the fix. The problem is in lines 501-516 of
_fetch_tokens_with_refresh: - Assistant
Now I understand the issue clearly. The fix is to handle the
inflight is Nonecase properly. Wheninflight is None, it means the subprocess was never registered (early cancellation before await), so no refresh happened. We need to setsubprocess_excto prevent the generation bump.Let me apply the fix:
- Assistant
Let me use the correct syntax for the edit command:
- Assistant
Let me use the edit command with proper syntax:
- Assistant
There's a duplicate comment line on line 526. Let me fix that:
- Assistant
I now have a duplicate line. Let me remove it:
- Assistant
Good. Now let me verify the full change looks correct by viewing the complete cancel handler section:
- Assistant
Now let me also check if the same code exists in
auth.py: - Assistant
The
auth.pyre-exports_coalesced_run_refresh_cmdfrom_auth/refresh.py, so the fix in_auth/refresh.pyis sufficient. Let me also check if auth.py has its own copy of the_fetch_tokens_with_refreshfunction: - Assistant
Good —
auth.pyre-exports from_auth/refresh.py, so our fix in_auth/refresh.pyis the only place that needs changing.Now let me write a reproduction script to verify the bug is fixed:
- Assistant
Let me check how to run the tests properly.
- Assistant
Let me check the project root and how to install/run things.
- Assistant
Let me run the reproduction script with the venv python:
- Assistant
The fix works. Now let me verify that the original code would have the bug by reverting and running the test again:
- Assistant
The reproduction confirms the bug exists in the original code. Let me restore my fix:
- Assistant
Let me verify the fix again:
- Assistant
Good. Now let me run the existing tests to make sure nothing is broken:
- Assistant
Let me run the refresh state machine tests and the auth tests:
- Assistant
All 306 tests pass. Let me run the integration concurrency tests related to refresh:
- Assistant
All tests pass. Let me also run the unit test for the refresh lock lazy init:
- Assistant
All tests pass. Let me clean up the reproduction script:
- Assistant
Let me check the final diff:
- Assistant
The diff looks correct. Let me run a broader set of tests to make sure nothing else is broken:
- Assistant
812 unit tests passed. The only failure is a missing
playwrightmodule (unrelated to our change). Let me run the integration concurrency tests too: - Assistant
All 94 integration concurrency tests pass. The fix is working correctly.
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.