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 understand the issue:
-
In
_coalesced_run_refresh_cmdinsrc/notebooklm/_auth/refresh.py, there's a race condition where a task can be cancelled before it registers a future in_REFRESH_INFLIGHT_BY_LOOP[loop][refresh_key]. -
When this happens, the cancel-recovery loop in
_fetch_tokens_with_refresh(the caller) doesn't find an inflight future, so it bumps the generation counter even though no refresh actually happened. -
This spurious generation bump can cause a concurrent waiter on a different event loop to think the storage has been refreshed when it hasn't, leading to a wasted round-trip.
The fix should track whether
inflightwas ever non-None during the cancel-recovery loop, and only bump generation ifsubprocess_exc is None AND observed_inflight is True.Let me first look at the relevant code.
-
Assistant
Let me look at the relevant file:
Assistant
Let me look at the relevant section around line 487-537 mentioned in the issue.
Assistant
Let me just use cat or grep to look at the file.
Assistant
Now let me look at the
_coalesced_run_refresh_cmdfunction:Assistant
Let me look at the function definition.
Assistant
Now I understand the issue. Let me look at the full function to understand the context better
Assistant
Now I have a clear picture. Let me trace the race:
caller_cancelled = False,subprocess_exc = None- Task enters
try:and awaits_coalesced_run_refresh_cmd(...) - Task is cancelled before
_coalesced_run_refresh_cmdregisters a future in_REFRESH_INFLIGHT_BY_LOOP - Control jumps to
except asyncio.CancelledError: caller_cancelled = True;registry.get(refresh_key)returnsNoneinflight is None or inflight.done()→ True;inflight is not None→ False →breaksubprocess_exc is not None→ False → falls through to generation bump — BUG!
Let me look at the full function and then implement the fix:
Assistant
Let me get the exact line numbers:
Assistant
Now I'll implement the fix. I need to make three changes:
- Add
observed_inflight = Falsebefore the while loop - Set
observed_inflight = Truewhen we observe a non-None inflight in the cancel handler - Only bump generation when `not caller_cancelled or observed_inflight
- Add
Assistant
Let me use the proper heredoc syntax for the edit command
Assistant
Good. Now I need to add
observed_inflight = Truewhen we observe a non-None inflight. Let me edit that section:Assistant
I need to close the heredoc properly. Let me try
Assistant
Now I need to add the guard around the generation bump. Let me edit lines 535-
Assistant
I see there's a duplicate
if caller_cancelled:block. Let me check and fix itAssistant
I need to remove the duplicate block. Let me fix
Assistant
Now let me verify the full modified section looks correct
Assistant
The changes look correct. Let me now verify the fix by tracing through all scenarios:
- Race (cancelled before registration):
observed_inflight=False,caller_cancelled=True→not True or False= False → skip bump ✓ - Normal success:
caller_cancelled=False→not False or anything= True → bump ✓ - Cancelled but subprocess ran:
observed_inflight=True,caller_cancelled=True→not True or True= True → bump ✓ - Subprocess failed:
subprocess_exc is not None→ raise → never reaches bump ✓
Now let me run the existing tests to make sure nothing is broken.
- Race (cancelled before registration):
Assistant
Let me run the relevant unit tests
Assistant
Need to install the package first.
Assistant
Now let me run the tests
Assistant
Let me see the full test output
Assistant
The test failure is due to missing
pytest-asyncio, not my changes. Let me install itAssistant
Now let me run the tests again.
Assistant
All 3 tests pass. Let me run the other related test files too.
Assistant
All tests pass. Let me run the broader unit test suite
Assistant
Missing dependency, not related to my changes. Let me install it and run the full unit test suite.
Assistant
Let me install the remaining missing dependencies and try again.
Assistant
812 tests passed, 1 error is unrelated (missing playwright module). Let me verify my changes with git diff
Assistant
The diff looks clean and correct. Let me write a quick script to verify the fix works correctly for all scenarios
Assistant
Let me also check the cancellation propagation test to make sure it's not
Assistant
All tests pass. Let me clean up and do a final review.
Assistant
Let me do a final review of the complete modified
Assistant
Everything looks good. Here's the summary of my changes:
Summary
The fix addresses a spurious
_REFRESH_GENERATIONSbump that occurs when a task is cancelled between entering the block and registering a future in the inflight registry.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.