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 understanding the repository structure and then locate the relevant file mentioned in the issue description.
The issue is about a spurious
_REFRESH_GENERATIONSbump in_coalesced_run_refresh_cmdfunction insrc/notebooklm/_auth/refresh.py. Let me first explore the repository structure and then look at the specific code.Assistant
Let me look at the specific file mentioned in the issue.
Assistant
Let me look at the refresh.py file, specifically around the lines mentioned (487-537).
Assistant
Let me open the file at the relevant section.
Assistant
Let me try using the command line to view the file contents.
Assistant
Now let me look at the
_coalesced_run_refresh_cmdfunction:Assistant
Let me read the
_coalesced_run_refresh_cmdfunction starting at line 144Assistant
Let me read the caller context more fully:
Assistant
Now I have a clear understanding of the code. Let me look at the exact lines I need to modify:
Assistant
Now I have a clear understanding of the issue. Let me use the
editcommand to fix it. The problem is in the cancel handler: wheninflight is None(subprocess never registered), we should setsubprocess_excto prevent the phantom generation bump.Assistant
Let me use the
replacecommand with the heredoc syntax insteadAssistant
Let me try open the file first, then use the commands.
Assistant
Now let me use the edit command to replace the relevant lines.
Assistant
Let me use sed to make the edit directly.
Assistant
Let me create a Python script to make the precise edit.
Assistant
Let me verify the change by viewing the modified section
Assistant
The change looks correct. Let me now run the relevant tests to make sure nothing is broken
Assistant
Let me install the package first
Assistant
Now let me run the tests, specifically the ones related to auth/refresh.
Assistant
Let me find and run the unit tests specifically
Assistant
Let me run the refresh and concurrency-related
Assistant
Let me see the test failure details
Assistant
The test failure is unrelated to my change - it needs
pytest-asyncio. Let me install it and re-run.Assistant
Now let me re-run the tests.
Assistant
33 tests passed; the failure is about a missing
httpx_mockfixture which is unrelated to my change. Let me run the refresh state machine tests specificallyAssistant
All 19 tests pass. Let me also run the remaining tests
Assistant
All tests pass. Let me verify the fix with a quick script that simulates the race condition
Assistant
The fix is verified. Let me do a final review of the changed code
Assistant
The diff looks clean. Let me run a broader set of unit tests to ensure nothing else is broken
Assistant
Let me install the missing dependency
Assistant
Now let me run the unit tests again.
Assistant
Let me install vcrpy and then run the tests again.
Assistant
812 tests passed, 1 unrelated error (missing
playwrightmodule). Let me run the auth testsAssistant
All tests pass. Let me now go through the checklist:
Checklist
-
If an error was reported in the issue description, I have successfully reproduced it.
- ✅ I traced through the code logic and confirmed the bug: in the
except asyncio.CancelledErrorhandler within , when (subprocess was never registered because cancellation happened before could create the future), stays , causing the code to fall through to the generation bump even though no real refresh ran. I verified this with a script tracing the four possible cases.
- ✅ I traced through the code logic and confirmed the bug: in the
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.