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 inspect the refresh implementation and its existing tests first, then reproduce the cancellation window with the project’s test tooling before making the smallest semantics-preserving fix.
- Assistant
The problematic generation update is in
_fetch_tokens_with_refresh’s recovery around_coalesced_run_refresh_cmd, as described. I’ll now inspect the coalescing mechanics and all cancellation-race tests to understand intended behavior for leader cancellation, waiter cancellation, and subprocess failure. - Assistant
A deterministic reproduction can replace the coalesced helper with a coroutine that raises
CancelledErrorbefore registering any future—the exact externally visible state of the narrow race. I’ll run that against the current implementation and assert/report the generation afterward, without adding repository tests. - Assistant
The first reproduction did not enter refresh because
_should_try_refreshalso requires the refresh-command environment variable. I’ll update only the temporary script to set it, then rerun. - Assistant
The reproducer confirms the bug (
generation: 1). The safest narrow fix is the suggested immediate re-raise when the registry has no future: there is no subprocess to recover or success to publish. Existing handling for a present done future must remain because it preserves the cancel/settle race’s exception inspection. I’ll split those branches and document why the absent-future case cannot proceed to generation publication. - Assistant
The line-range edit inadvertently removed the following
except BaseExceptionheader (visible as an orphanedexcassignment), so I’ll restore that unchanged fallback and the loop comment before running tests. - Assistant
The deterministic reproducer now reports generation
0, and syntax/diff checks pass. I’ll run the focused concurrency suite (including successful leader cancellation and cancel/settle failure) plus refresh lock-registry unit tests to verify the retained paths. - Assistant
Focused tests pass all 11 cases. I’ll now run the repository’s configured lint/format checks on the changed file and the full unit suite, which is the broadest practical regression check for this internal auth flow.
- Assistant
Lint and formatting pass. The full unit run reached 812 passing tests, then stopped because the optional
playwrightpackage is not installed; this is environment setup, unrelated to the change. Rather than alter dependencies, I’ll run all auth-focused unit and integration/concurrency tests and inspect final repository status/diff.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.