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 relevant tests first, then reproduce the cancellation path before making the smallest semantics-preserving fix.
- Assistant
The generation update is in
_fetch_tokens_with_refresh’s recovery loop, as described. I’ll now inspect the single-flight helper and existing race tests to understand registry cleanup and distinguish “cancel before registration” from “registered future already settled.” - Assistant
A done registry future must remain a valid success/failure signal, so the safest fix is to track whether any future was observed rather than treating every
Noneas success. I’ll build a temporary deterministic reproducer by monkeypatching_coalesced_run_refresh_cmdto raiseCancelledErrorbefore registration; this directly exercises the vulnerable caller recovery branch. - Assistant
The temporary reproducer needs the src-layout package on
PYTHONPATH; I’ll rerun it with that environment and expect the pre-fix assertion to expose generation1. - Assistant
The call never entered refresh because refresh-command opt-in was absent, so the original auth
ValueErrorpropagated. I’ll set the configured command environment variable and rerun the same deterministic race. - Assistant
The reproducer confirms the bug (
CancelledError 1). Because_coalesced_run_refresh_cmdregisters its future before its first suspension, a missing registry entry proves no shared subprocess was started or joined. I’ll split the branch and immediately re-raise cancellation wheninflight is None; done futures will still be inspected exactly as before. - Assistant
The deterministic reproducer now reports
CancelledError 0, confirming no phantom bump. Next I’ll run the focused refresh race and lock-registry suites, including existing successful-cancellation and cancel/settle-failure coverage to ensure the immediate raise does not alter registered-future behavior. - Assistant
That test invocation had a punctuation typo in
--maxfail; no tests ran. I’ll rerun the intended suites correctly. - Assistant
The repository uses a src layout and the plain pytest executable does not install/add the package. I’ll inspect the project tooling and rerun through its configured environment (or with
PYTHONPATH=src, as appropriate). - Assistant
uvand a lockfile are available, so I’ll run the focused suites withuv run pytest. - Assistant
All 280 focused tests pass. I’ve also considered the key edge cases: a registered pending future still loops under shield; a registered successful done future still permits the generation bump; a registered failed/cancelled future still suppresses it; only absence of registration now propagates cancellation. I’ll run lint plus the broader auth-related unit tests, then inspect the final diff/status.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.