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 start by exploring the repository structure and the relevant files mentioned in the issue.
Assistant
Now let me look at the full
_coalesced_run_refresh_cmdfunction.Assistant
Now let me see the exact lines of the cancel-recovery loop.
Assistant
Now I understand the issue fully. Let me open the file at the relevant section.
Assistant
Now I'll implement the fix. I'll track whether a real subprocess was ever observed during the cancel-recovery loop, and only bump the generation if a refresh actually happened.
Assistant
I notice the comment indentation is broken on lines 497-498. Let me fix it.
Assistant
The indentation looks correct now. Let me verify the file compiles and check the logic is sound.
Assistant
The issue mentions the code is also in
src/notebooklm/auth.py. Let me check whether that file has the same code that needs fixing too.Assistant
auth.pyjust re-exports from_auth/refresh.py, so the fix is in the right place. Let me confirm the edited code is inside_fetch_tokens_with_refresh.Assistant
The edited code is within
_fetch_tokens_with_refresh. Now let me look at existing tests for the refresh logic to understand patterns and write a reproducer.Assistant
Let me look at the existing refresh cmd race test to model a reproducer.
Assistant
Let me check the facade write-through mechanism that makes
monkeypatch.setattr(auth_mod, ...)propagate to_auth/refresh.py.Assistant
So
monkeypatch.setattr(auth_mod, "_run_refresh_cmd", fake)triggers__setattr__, which writes through to_auth_refresh._run_refresh_cmd(since_run_refresh_cmdis in_AUTH_REFRESH_FACADE_NAMES). That's how the existing tests work.For my reproducer, I want to make
_coalesced_run_refresh_cmdraiseCancelledErrorimmediately (before registering a future). I can monkeypatchauth_mod._coalesced_run_refresh_cmdwith a fake that raises CancelledError. Since_coalesced_run_refresh_cmdis in_AUTH_REFRESH_FACADE_NAMES, the write-through to_auth_refresh._coalesced_run_refresh_cmdhappens. And_fetch_tokens_with_refreshreferences_coalesced_run_refresh_cmdas a module global in_auth/refresh.py, so it picks up the patched version.Let me write the reproducer. Let me check
_AUTH_REFRESH_FACADE_NAMES.Assistant
Now I'll write a temporary reproducer to confirm the bug and the fix.
Assistant
Now let me check the test config — how to run. The tests use
@pytest.mark.asyncio. Let me check the conftest and whetherpytest.mark.allow_no_vcris needed/recognized. Let me run it. But first, let me revert my fix temporarily to confirm the bug reproduces, then re-apply to confirm fix. Actually, easier: run with current (fixed) code — should pass. Then git stash the fix, run — should fail. Then re-apply.Let me first check how tests are run (uv, pytest config).
Assistant
Let me run the reproducer with the fix applied (should pass), then verify the bug exists without the fix by temporarily stashing.
Assistant
The test passes with my fix. Now let me confirm the bug reproduces without the fix by temporarily reverting.
Assistant
The bug reproduces without the fix (generation bumped to 1, phantom bump). Now let me restore my fix and confirm it passes.
Assistant
Fix confirmed. Now let me run the existing related tests to make sure I didn't break anything.
Assistant
All pass. Let me run the broader auth-related unit and integration concurrency tests to be thorough.
Assistant
Let me run the full unit test suite to ensure nothing else broke.
Assistant
Let me see the failures and errors in detail to determine if they're pre-existing and unrelated.
Assistant
All failures/errors are in Playwright-related CLI tests (
test_session.py,test_windows_compatibility.py), completely unrelated to my change. Let me confirm they're pre-existing by running them on the clean tree.Assistant
Confirmed — those failures are pre-existing (Playwright environment issue), unrelated to my change. Now let me clean up the temp test and review my final diff.
Assistant
Let me do a final review of the edited region to ensure indentation is consistent.
Assistant
The fix looks correct. Let me verify no leftover temp files in the repo and run the final targeted test suite once more.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.