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
Now I'll make the edits: add
observed_inflighttracking and gate the generation bump on it.Assistant
Now let's verify the diff and check syntax.
Assistant
Now let's check the same code in
src/notebooklm/auth.pysince the issue mentions the facade may also have similar logic. Let's check if auth.py still has the actual implementation or just a forward.Assistant
Good,
auth.pyjust forwards to_auth/refresh.py, so no duplicate logic to fix there. Now let's find and run existing tests related to this.Assistant
Good, these failures are pre-existing (likely due to missing pytest-asyncio plugin), not caused by my change. Let's confirm pytest-asyncio is installed.
Assistant
All 336 tests now pass with only 2 unrelated errors (missing
httpx_mockfixture, needspytest-httpx). Let's install that too and confirm.Assistant
All 338 tests pass now. Let's run the full test suite to be thorough.
Assistant
These failures look environment-related (missing playwright, VCR network fixtures unrelated to our change). Let's verify by stashing and re-running full suite count comparison.
Assistant
Same numbers (142 failed, 5215 passed) confirming these are pre-existing environment-related failures, unaffected by my change.
Now let's write a targeted reproducer script to simulate the exact race described in the issue and confirm the fix works.
Assistant
The fix works as intended. Let's clean up the temp script and do a final review of the diff.
Assistant
The diff looks correct and minimal. Let's run the full unit + integration concurrency-related tests one more time to be thorough, plus check that
auth.pyfacade tests pass too.Assistant
All remaining failures relate to playwright/browser session tests, unrelated to the auth refresh module changes. This confirms my fix doesn't break anything and correctly resolves the issue.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.