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 repository structure, the refresh implementation, and nearby tests first. Then I’ll reproduce the cancellation window deterministically if possible, make the narrowest semantic fix, and run targeted plus broader tests.
- Assistant
The reported vulnerable branch is present exactly as described: cancellation can reach recovery before an in-flight future exists, yet
subprocess_excremainsNoneand generation is bumped. I’ll now inspect the coalescing helper and existing race tests to understand registry cleanup and preserve intended leader/waiter cancellation behavior. - Assistant
A deterministic stand-in for the tiny scheduling window is to replace
_coalesced_run_refresh_cmdwith a coroutine that raisesCancelledErrorbefore registering anything. This produces the exact state entering the recovery handler (caller_cancelled=True, registry entry absent). I’ll run that against the current code and inspect the generation counter. - Assistant
The direct interpreter does not have the
srclayout on its import path, so the first reproduction attempt did not execute project code. I’ll rerun it withPYTHONPATH=src; no repository files are being changed. - Assistant
The refresh path is gated by configuration, so the simulated auth error was correctly re-raised before reaching the vulnerable code. I’ll inspect the gate and test fixtures, then set only the required environment variable in the reproducer.
- Assistant
The only missing condition was
NOTEBOOKLM_REFRESH_CMD. I’ll rerun with a dummy value; the subprocess itself remains replaced and therefore cannot execute. - Assistant
The reproduction confirms the defect (
generation=1with no registry entry). The invariant here is strong:_coalesced_run_refresh_cmdregisters its future synchronously before its first suspension, and settled futures are intentionally retained. Therefore, an absent entry means there is no shared subprocess to recover or account as successful. I’ll split theNonecase from thedone()case and immediately re-raise the caller cancellation before generation update. - Assistant
The change is intentionally local and preserves all existing handling for registered pending, successful, failed, and subprocess-cancelled futures. I’ll rerun the same deterministic reproduction to verify that cancellation still propagates while generation remains unchanged.
- Assistant
The reproducer now passes: cancellation propagates and the generation stays at zero. Next I’ll identify the project’s configured test/lint commands, then run the refresh race suites and relevant unit tests.
- Assistant
The project uses
uvwith pytest. I’ll run the complete refresh command race integration module and refresh lock registry unit module, which cover the affected generation, cancellation, same-loop, and cross-loop behavior. - Assistant
All 280 targeted tests pass, including existing leader-cancel/settle-failure and cross-loop generation scenarios. The edge cases remain correctly partitioned: absent future propagates immediately; pending future is re-awaited under the lock; done success may bump; done failure/cancellation cannot bump. I’ll run source lint and type checking before the full non-e2e suite.
- Assistant
Lint, formatting, and mypy all pass. I’ll now run the full configured test suite (which excludes e2e per
pyproject.toml) to detect any unrelated interaction with authentication flows. - Assistant
The full suite reached 5,344 passes; every failure/error is due to the optional
playwrightPython package being absent, not the change. Since the repository declares it in thebrowserextra and permits installing missing packages, I’ll sync that existing extra (without modifying dependency declarations) and rerun the full suite. - Assistant
Syncing only
browserremoved the dev tools from the environment becauseuv syncexactly reconciles selected extras. I’ll resync both declareddevandbrowserextras, then rerun the suite. This affects only the virtual environment; git confirms the source file remains the sole repository change. - Assistant
The full configured suite now passes with the declared optional dependency installed: 5,376 passed and 25 skipped. I’ll perform a final diff/status review to ensure there are no generated or dependency-file changes and that the patch remains narrowly scoped.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.