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 nearby tests, then reproduce the cancellation path with a temporary script if practical. I’ll implement the smallest state-tracking fix, run targeted and broader tests, review the diff, and submit only after verification.
- Assistant
The caller already has the vulnerable shape: cancellation with no registered in-flight future falls through to the success-generation bump. I’ll now inspect
_coalesced_run_refresh_cmdand project test layout to ensure the tracking variable is placed with the correct scope and semantics. - Assistant
The relevant invariant is exactly the suggested one: a successful generation bump after cancellation is valid only if this caller observed a future for the refresh. I’ll add
observed_inflight, set it whenever the registry lookup returns a future (including a done future), and guard the bump with it. This preserves successful-cancel recovery while preventing the pre-registration cancellation race from claiming success. - Assistant
The code now distinguishes “cancelled before registration” from “cancelled while/after a real shared refresh.” Compilation succeeds. I’ll run the refresh state-machine, lock-registry, and concurrency-related tests, then use a temporary focused harness to exercise the no-future cancellation branch if the existing suite does not cover it.
- Assistant
The first test command exposed environment issues rather than code failures: the package is source-layout (needs
PYTHONPATH=src), and this environment lacks the pytest-timeout plugin despite the project configuration. I’ll rerun the same targeted tests without that unsupported option. - Assistant
The targeted suite passes (19 tests). My first focused harness used an unrecognized auth error and therefore correctly did not enter refresh; I’ll correct the harness by setting the documented refresh environment and using a recognized error signal, then verify the generation remains unchanged.
- Assistant
The focused harness now reproduces the reported early-cancel condition and confirms the generation stays at zero. I noticed the moved assignment is one character over the configured line length after indentation; I’ll wrap it, run the complete unit suite, and perform a final diff/status review.
- Assistant
Verification is complete:
- Reproduced the reported error: a temporary harness forced auth expiry and cancellation before any in-flight future existed; before the fix this path would bump the generation, and with the fix it remained unchanged.
- Confirmed the fix: the harness printed
early-cancel generation unchanged; the implementation tracksobserved_inflightand only bumps after a real shared refresh was observed. - Edge cases considered: done successful futures still permit the bump; done/cancelled or failed futures do not; cancellation before registration propagates without a bump.
- Existing tests: all affected targeted tests pass: 19 passed. The full unit suite could not collect because the environment lacks the unrelated
clickdependency. Compilation and diff checks pass. - No tests were modified or added.
The final change is limited to
src/notebooklm/_auth/refresh.py. I’ll submit it now.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.