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 let's view the resulting code to double check correctness.
Assistant
Now let's check that
auth.pyfacade doesn't have a duplicate copy of this same code that also needs patching.Assistant
Good,
auth.pyjust re-exports from_auth/refresh.py, so no duplicate code there. Now let's find and run relevant tests.Assistant
pytest-asyncio isn't installed — that's an environment issue unrelated to my change. Let's install it and rerun.
Assistant
Missing pytest-httpx fixture. Let's install it too.
Assistant
All 338 tests pass. Now let's run the full test suite to be safe.
Assistant
Confirmed: same 142 failed / 5215 passed baseline exists without my change — these failures are pre-existing environment issues (missing playwright browsers, VCR cassettes, etc.), unrelated to my fix. My change didn't introduce any new failures.
Now let's write a quick reproduction script to specifically validate the fix addresses the described race condition.
Assistant
This confirms the fix's logic directly. Let's clean up temp file and view final diff.
Assistant
The diff is minimal and targeted, matching the "alternative suggestion" from CodeRabbit: raise immediately when
inflight is Noneat the top of the cancel handler.Let's do a final check of the full test file for refresh cmd race to make sure semantics still hold, especially edge cases around cancellation.
Assistant
All the specific race-condition tests pass. Let's view the final code once more to confirm everything is coherent.
Assistant
No ruff available; not critical since py_compile succeeded and tests pass. Let's do a final full diff review and run the broader auth-related test directories once more for confidence.
Assistant
This single failure is an unrelated click library version message-format difference (pre-existing, confirmed earlier in baseline run). Not related to our change.
Assistant
The fix is complete and focused. Let me go through the checklist:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.