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, then trace the cancellation-recovery state transitions. I’ll implement the narrowest fix that prevents a generation bump unless an inflight refresh was actually observed, add no repository tests per instructions, and run the relevant existing test suite plus a focused temporary reproduction if practical.
- Assistant
The vulnerable path is present exactly as described: after cancellation recovery, the code unconditionally treats the operation as a successful refresh when no exception was recorded. I’m now examining
_coalesced_run_refresh_cmdand the existing race tests to ensure the fix preserves leader cancellation, follower cancellation, and normal success semantics. - Assistant
The correct minimal change is to introduce
observed_inflight = Falsefor each refresh attempt, set it whenever cancellation recovery finds a non-Nonefuture, and require it alongsidesubprocess_exc is Nonebefore the generation bump. This handles the early-cancel case by propagating cancellation without claiming success, while still bumping after a shielded future that was observed and settled successfully. I’ll make that targeted edit now. - Assistant
The edit now distinguishes normal completion from cancellation recovery: ordinary success still bumps the generation, cancellation after observing an inflight future still bumps only on successful settlement, and cancellation with no registered future exits before the bump. I’ll run the focused concurrency regression tests and unit refresh-lock tests, then inspect formatting/type/lint expectations.
- Assistant
The first test command was malformed due to an extra shell quote and did not execute any tests. I’ll rerun the same validation command correctly.
- Assistant
The focused tests are currently blocked by the environment’s import path (
notebooklmis not installed when invoking barepytest), not by a test failure. I’ll inspect the project’s prescribed test command and rerun with the repository source path or package runner as appropriate. - Assistant
With
PYTHONPATH=src, collection progressed but the environment lacks the test dependencyvcr; the project providesuv, so I’ll useuv runto obtain the locked development dependencies and execute the focused tests. - Assistant
The focused suite passes all 11 tests, including existing cancellation-settle and generation-race regressions. I’ll do a final code review and run the broader auth/concurrency unit coverage; I’ll also verify the early-cancel branch explicitly with a temporary script if the surrounding API can be exercised without changing repository tests.
- Assistant
The broader auth/concurrency coverage also passes (304 tests). My first temporary reproduction was too strict:
_fetch_tokens_with_jaris called once before refresh and must raise auth expiry only on that first call; because the script raised on every call, it never reached the refresh branch. I’ll correct the temporary harness (outside the repository), rerun it, and then remove it. - Assistant
The temporary harness also exposed that the public facade does not mirror
_fetch_tokens_with_jarin that ad hoc assignment, so it still called the real implementation; it was not evidence against the fix. The repository’s own tests exercise the moved module through its supported facade and all pass. I’ll perform a direct private-module smoke reproduction using the actual resolution target, then finalize. - Assistant
The direct smoke harness omitted the required
NOTEBOOKLM_REFRESH_CMDenvironment flag, so the production guard correctly re-raised the auth error before attempting refresh. I’ll rerun once with that flag set; this is only a temporary external script and will be removed afterward. - Assistant
The focused temporary reproduction now reaches the cancellation-recovery path and reports
generation: 0, confirming the phantom bump is prevented. Final validation is clean: compilation and whitespace checks pass, 11 focused race/registry tests pass, and 304 broader auth/concurrency tests pass. The only repository change is the targeted refresh logic. 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.