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
Let me start by exploring the repository structure to understand the codebase.
Assistant
Let me look at the specific function
_coalesced_run_refresh_cmdin the refresh.py file:Assistant
Let me look at the full function
_coalesced_run_refresh_cmdand the surrounding code in detail:Assistant
Now let me look at the key constants and helper functions referenced in the code:
Assistant
Let me look at the exact cancel-recovery loop in detail with line numbers:
Assistant
Now let me look at the existing tests for this refresh logic to understand the testing conventions:
Assistant
Let me look at the integration tests for refresh cancellation and the race:
Assistant
Let me look at the test for cancel-settle race more closely and check the facade mechanism:
Assistant
Let me check the test config (asyncio mode) and conftest:
Assistant
Let me run the existing related tests to make sure they pass before making changes:
Assistant
Now let me write a reproducer script to confirm the bug. I'll model it on the existing
test_cancel_settle_race_does_not_bump_on_failure:Assistant
I've reproduced the bug. The generation is bumped to 1 even though no subprocess ran. Now let me implement the fix. Let me open the file at the relevant section:
Assistant
Now I'll implement the fix. I'll modify the cancel handler to raise immediately when
inflight is None(the cancel-before-registration race), preventing the spurious generation bump:Assistant
The fix is in place. Now let me run the reproducer again to confirm the bug is fixed:
Assistant
The bug is fixed. Now let me verify the existing tests still pass:
Assistant
Let me find and run all tests related to refresh to ensure nothing broke:
Assistant
All passing. Now let me write a comprehensive verification script covering the key edge cases to make sure I didn't break the legitimate bump paths:
Assistant
I need to restore the original functions between cases. Let me fix the verification script:
Assistant
All edge cases pass. The fix is surgical — it only changes the cancel-before-registration case (Case A) while leaving the legitimate bump paths (Cases B, C) and failure path (Case D) unchanged.
Let me run the broader test suite to ensure nothing else broke:
Assistant
Let me check whether these failures are pre-existing or caused by my change. Let me look at the failures and errors:
Assistant
All failures are in Playwright/browser-session tests (
test_session.py,test_windows_compatibility.py), unrelated to my change. Let me confirm they're pre-existing by checking the error cause and running them against the original code:Assistant
Confirmed — these are
ModuleNotFoundError: No module named 'playwright'(Playwright not installed in this environment), completely unrelated to my change. Let me verify by stashing my change and re-running one:Assistant
Confirmed pre-existing — these fail identically without my change due to the missing
playwrightmodule. Let me run the full unit + integration concurrency suite excluding the playwright-dependent tests:Assistant
All tests pass (4628 passed, 13 skipped). Let me review the final state of my change:
Assistant
Let me view the final state of the edited section to confirm correctness:
Assistant
The change is clean and correct. Let me verify the module compiles without syntax errors and run a lint check:
Assistant
Ruff flags B904 on the
raiseinside theexceptclause. Let me check how the codebase handles this pattern elsewhere and the ruff config:Assistant
Ruff's B904 flags raising a new exception inside an
exceptclause withoutfrom. Since I'm inside theexcept asyncio.CancelledError:handler re-raising the caller's own cancellation, the idiomatic and most correct fix is a bareraise(which re-raises the caught cancellation, preserves asyncio's cancellation context in 3.11+, and naturally avoids B904). Let me update:Assistant
Now let me re-run ruff, mypy, and my verification scripts:
Assistant
All clean. Let me run the relevant existing test suites once more to confirm everything passes with the final version:
Assistant
All tests pass. Let me check whether the repository expects a CHANGELOG entry for fixes like this:
Assistant
The CHANGELOG is extensive and is organized by release. There's an
[Unreleased]section. The convention seems to be adding entries under sections like "Added", "Changed", "Fixed". Let me check if there's a "Fixed" section in the Unreleased portion.Actually, let me search for "Fixed" section in the Unreleased part. But honestly, the CHANGELOG appears to be a roll-up for a major release. Adding a CHANGELOG entry might be appropriate but I should be careful about the format. Let me check if there's a "Fixed" section.
Actually, let me reconsider. The task is to resolve the issue (fix the bug). The CHANGELOG is a nice-to-have but the core deliverable is the code fix. The instructions say "UNDER NO CIRCUMSTANCES should you modify existing tests". They don't prohibit CHANGELOG updates. But I want to be careful not to introduce noise.
Let me check whether there's a "Fixed" section in Unreleased and whether recent PRs added entries. Given the issue references PR #814 and this is a "dedicated follow-up", a CHANGELOG entry under "Fixed" would be appropriate. Let me look for the Fixed section.
Assistant
There's a "Fixed" section under
[Unreleased]. I'll add a concise entry consistent with the existing style:Assistant
I'll add a concise entry to the "Fixed" section under
[Unreleased]:Assistant
The CHANGELOG entry is added. Now let me clean up the temp files and do a final review of the complete diff:
Assistant
Let me do a final verification: re-run the key refresh tests and an inline reproduction to confirm the fix is in place:
Assistant
Everything passes. Let me do a final git status check to confirm only the intended files are modified and no temp files remain:
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.