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
me look at the specific area of the code that's mentioned in the issue:
Assistant
Now let me look at the
_coalesced_run_refresh_cmdfunction:Assistant
Now I have a clear understanding. Let me also look at the auth.py file for the same code (since the issue mentions both locations):
Assistant
Now let me look at existing tests for refresh:
Assistant
Now let me look at the test_refresh_state_machine.py:
Assistant
Now I have a clear picture of the code. Let me look at the auth.py to make sure I understand what's happening:
Assistant
Now let me run the existing tests to see the current state:
Assistant
Now I have a clear understanding. Let me look at the relevant code more carefully:
Assistant
Let me check if there's any code that removes the future from the registry:
Assistant
Now let me write a reproducer first to confirm the bug:
Assistant
Let me write a better reproducer that simulates the actual race condition through
_fetch_tokens_with_refresh:Assistant
I have successfully reproduced the bug. The generation was bumped from 0 to 1 even though
_run_refresh_cmdwas never called. Now let me implement the fix:Assistant
Now let me apply the fix. I'll use the
observed_inflightflag approach, which is more defensive:Assistant
Let me verify the fix was applied correctly:
Assistant
Now let me run the reproducer to confirm the fix works:
Assistant
The fix works. Let me now run the existing tests to make sure I haven't broken anything:
Assistant
Let me look at the failing tests:
Assistant
Let me look at the failing test:
Assistant
Let me look at the actual implementation of
_run_refresh_cmdto understand what's happening:Assistant
I see the bug in my fix. The
observed_inflightflag should also be set when the await completes normally. Let me fix this:Assistant
Let me verify the fix:
Assistant
Now let me run the tests again:
Assistant
All tests pass. Now let me run the reproducer to confirm the fix still works for the bug scenario:
Assistant
The fix works. Now let me also run the full test suite to make sure nothing else is broken:
Assistant
The only failure is unrelated (playwright not installed). Let me skip the playwright test and run everything else:
Assistant
All 3541 tests pass. Now let me also think about edge cases. Let me re-read my fix:
Assistant
Let me also write a more comprehensive reproducer to test various edge cases:
Assistant
The test 2 has an issue - the
refresh_mod._coalesced_run_refresh_cmdwas already overridden by test 1. Let me fix the test:Assistant
Excellent! All edge cases pass. Let me also test the cancel-DURING-await race (where the future is registered):
Assistant
All tests pass. Let me also verify the fix with the existing test suite one more time:
Assistant
All 427 tests pass. Let me also run the integration tests if they exist:
Assistant
All 94 integration tests in the concurrency directory pass. Let me also check the auth_refresh_vcr test:
Assistant
Let me also run the auto_refresh test:
Assistant
All tests pass. Let me also clean up my temporary scripts:
Assistant
Let me also verify the fix by looking at the final diff:
Assistant
Let me verify the file is syntactically correct:
Assistant
Let me also run a final comprehensive test to make sure everything works:
Assistant
All 3641 tests pass. Let me also verify by looking at the final version of the fix in context:
Assistant
The fix is in place. Let me now go over the checklist:
Checklist Review
-
If an error was reported in the issue description, I have successfully reproduced it.
- YES. I created a reproducer at
/tmp/reproduce_bug2.py(now deleted) that:- Mocked
_coalesced_run_refresh_cmdto raiseCancelledErrorimmediately (simulating the cancel-before-await race) - Mocked
_run_refresh_cmdto track calls - Called
_fetch_tokens_with_refreshand verified:- Before the fix: Generation was bumped from 0 to 1, despite
_run_refresh_cmdnever being called - After the fix: Generation stays at 0, no phantom bump occurs
- Before the fix: Generation was bumped from 0 to 1, despite
- Mocked
- YES. I created a reproducer at
-
Assistant
The fix is a clean 53-line addition to the
src/notebooklm/_auth/refresh.pyfile. Let me also verify that theauth.pyfile is a thin facade (it re-exports from_auth/refresh.py) so no changes are needed there:Assistant
Good,
auth.pyis just a facade that re-exports from_auth/refresh.py. No changes needed there.Let me also submit a final summary:
Assistant
Only one file was modified:
src/notebooklm/_auth/refresh.py. The fix is complete.Summary of the Fix
The bug was in the cancel-recovery loop in
_fetch_tokens_with_refresh(insrc/notebooklm/_auth/refresh.py). When a task was cancelled before_coalesced_run_refresh_cmdcould register a future in the per-loop in-flight registry, the cancel-recovery loop would observeinflight is None, break out of the loop withsubprocess_exc = None, and then fall through to the generation bump — even though no subprocess actually ran.The Fix
Added an
observed_inflightflag that tracks whether the in-flight future was ever observed during the cancel-recovery loop:-
Set to
Trueafter a successful await: Since_coalesced_run_refresh_cmdregisters the future synchronously before theawait asyncio.shield(future), by the time the await completes normally, the future was definitely registered.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.