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 tests, trace the cancellation path, then make the smallest targeted change and run the relevant test suite (plus a focused temporary reproduction if practical). I will not modify repository tests.
- Assistant
The suspected bump is exactly in the cancel-recovery caller, and the repository already contains regression coverage around refresh races. I’ll inspect the in-flight registry semantics and the existing late race test to choose the safest condition: distinguish “no in-flight refresh was ever observed” from a real refresh that completed during cancellation.
- Assistant
The registry intentionally retains settled futures, so
inflight is not Noneis the right evidence that this caller reached a registered refresh, whether it is still running or already settled. I’ll add anobserved_inflightflag, set it only when the cancellation handler sees a future, and gate the generation bump on both successful completion and that flag. This preserves cancellation recovery for real refreshes while preventing the pre-registration cancellation case from changing shared state. - Assistant
The code change is in place. The first test invocation failed during test collection because the package source directory is not on
PYTHONPATHin this environment, not because of the patch. I’ll rerun with the repository’s source path configured and inspect project tooling if dependencies are still missing. - Assistant
The focused concurrency and registry tests now pass (11/11), and the diff has no whitespace errors. I’ll run the broader unit suite next to catch facade/import or cancellation behavior regressions, then review the final diff before submission.
- Assistant
The broader unit suite reached 812 passing tests before stopping on an unrelated missing optional
playwrightdependency during browser CLI test setup. The affected refresh tests pass, and the patch is limited to cancellation recovery. I’ll do a final syntax/status check and submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.