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 exploring the repository structure and understanding the codebase, particularly the file mentioned in the issue.
- Assistant
Let me look for the refresh.py file mentioned in the issue.
- Assistant
Let me examine both files to understand the code.
- Assistant
Let me open the refresh.py file and look at the specific area mentioned in the issue.
- Assistant
Now let me look at the
_coalesced_run_refresh_cmdfunction to understand the full picture. - Assistant
Let me view the _coalesced_run_refresh_cmd function to understand when cancellation can occur.
- Assistant
Now let me check the auth.py file to see if it has the same issue:
- Assistant
Good, auth.py just re-exports from
_auth/refresh.py. I only need to fix the code in_auth/refresh.py. Let me view the exact code I need to modify more carefully: - Assistant
Now I understand the issue completely. Let me trace through the problematic scenario:
caller_cancelled = False,subprocess_exc = Noneawait _coalesced_run_refresh_cmd(...)- cancelled before any future is registeredexcept asyncio.CancelledError:caughtinflight = registry.get(refresh_key)returnsNone(no future was ever created)if inflight is None or inflight.done():is Trueif inflight is not None:is False - skippedsubprocess_excstaysNone,breakif subprocess_exc is not None:is False - skip- Generation is bumped (lines 538-543) - spurious!
if caller_cancelled: raise CancelledError()
The fix is to raise immediately when
inflight is Nonein the cancel handler - no subprocess was ever registered, so nothing to wait for and no generation to bump. - Assistant
I see there's a duplicate comment line at line 523. Let me fix that:
- Assistant
The duplicate line is fixed. Let me verify the full modified section looks correct:
- Assistant
The fix looks correct. Now let me check if there are tests in the repository related to this code:
- Assistant
Let me search for tests in the repository specifically related to auth or refresh.
- Assistant
Let me look at the most relevant test files:
- Assistant
Let me check the other relevant test files:
- Assistant
Let me look at the most relevant test files for the cancellation race:
- Assistant
Let me also check the refresh_cmd_race test:
- Assistant
Now let me run the existing tests to make sure they pass before and to understand the test infrastructure:
- Assistant
Let me check the installation status and install if needed.
- Assistant
Now let me run the most relevant tests:
- Assistant
Let me install the test dependencies.
- Assistant
Now let me try running the tests again.
- Assistant
All 4 tests pass. Let me also run the other related tests:
- Assistant
Let me also run the unit tests related to refresh:
- Assistant
Let me install pytest-httpx and run the tests without test_auth.py:
- Assistant
All 316 tests pass. Let me also run the broader concurrency-related tests:
- Assistant
All tests pass. Now let me verify the exact change I made:
- Assistant
The diff looks correct. Now let me write a quick reproduction script to verify the fix works:
- Assistant
The fix is working correctly. Let me also verify what would happen WITHOUT the fix to confirm the bug existed. Let me temporarily revert the fix and run the reproduce script again:
- Assistant
The bug is confirmed. Without the fix, the generation was bumped to 1 spuriously. Let me restore the fix:
- Assistant
Let me run the reproduce script one more time with the fix to confirm:
- Assistant
Let me run all the tests one more time to make sure everything passes:
- Assistant
All 346 tests pass. Let me clean up the temporary test script:
- Assistant
Let me verify the final state of the diff one more time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.