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 analyze the issue:
- There's a race condition in
_coalesced_run_refresh_cmdinsrc/notebooklm/_auth/refresh.py - When a task is cancelled between
try:and the firstwith _REFRESH_STATE_LOCK:block, no future is registered - This causes a generation bump even when no real refresh happened
- The fix should track whether
inflightwas ever non-None during the cancel-recovery loop
Let me start by exploring the repository structure and understanding the code.
- There's a race condition in
Assistant
I need to find the refresh.py file specifically.
Assistant
Found the file. Let me open it and look at the
_coalesced_run_refresh_cmdfunction around lines 487-537.Assistant
Let me open the file and look at the relevant section.
Assistant
I see the special commands require a specific format. Let me try using the bash command to read the file.
Assistant
Now let me see the
_coalesced_run_refresh_cmdfunction:Assistant
Let me look at the
_coalesced_run_refresh_cmdfunction:Assistant
Let me search for more context about the registry and the state variables:
Assistant
Now I have a clear understanding of the code. Let me read the relevant section more carefully and look at the surrounding context:
Assistant
Now I understand the issue clearly. Let me trace through the race condition:
- The caller enters
_coalesced_run_refresh_cmd(refresh_key, ...) - In
_coalesced_run_refresh_cmd, the code enterstry:and then tries to acquire_REFRESH_STATE_LOCK - If the task is cancelled in this window (between
try:and the firstwith _REFRESH_STATE_LOCK:), no future is registered in the registry - The exception propagates to the
except asyncio.CancelledError:handler in_fetch_tokens_with_refresh - In the handler:
caller_cancelled = Trueregistry.get(refresh_key)returnsNone(because no future was registered)inflight is None→ takes the branchif inflight is None or inflight.done():if inflight is not None:is False, so stays
- The caller enters
Assistant
Now I have a clear understanding of the issue. Let me create a script to reproduce the race condition first.
Assistant
The command exited with exit code 0, which means it executed but I didn't see the output. Let me run the script to see the output.
Assistant
The reproducer confirms the bug: the generation is bumped from 0 to 1 even though no refresh happened. Now let me apply the fix to the source file.
The fix is to track whether
inflightwas ever non-None during the cancel-recovery loop, and only bump generation ifsubprocess_exc is None AND observed_inflight is True.Assistant
Now I understand the issue and have confirmed it with my reproducer. Let me apply the fix. The solution is to track whether
inflightwas ever non-None during the cancel-recovery loop, and only bump generation ifsubprocess_exc is None AND observed_inflight is True.Assistant
Now let me apply the fix. I need to:
- Track whether
inflightwas ever non-None during the cancel-recovery loop - Only bump generation if
subprocess_exc is None AND observed_inflight is True
- Track whether
Assistant
Now I'll apply the fix. I need to track whether
inflightwas ever non-None during the cancel-recovery loop:Assistant
Wait, I need to verify the patch was applied correctly. Let me check the file:
Assistant
Let me see more of the file to check the indentation:
Assistant
The indentation is broken. Let me revert and apply the fix properly.
Assistant
Let me verify the original file is restored and then apply the fix with proper indentation.
Assistant
Good, the file is restored. Let me apply the fix properly step by step using the edit command.
First, let me add the
observed_inflightvariable:Assistant
Let me use sed to make the changes properly:
Assistant
The user wants me to continue fixing the file. Let me check the current state of the file and apply the remaining changes.
Assistant
Good, the first change is applied. Now let me apply the remaining changes. First, let me add the
observed_inflight = Trueline:Assistant
I need to apply the remaining changes:
- Add
observed_inflight = Truebefore theif inflight is None or inflight.done():check - Change the generation bump to only happen if
observed_inflightis True
Let me use sed properly.
- Add
Assistant
Let me check the result of the sed command and then apply the remaining fixes.
Assistant
The first two changes look good. Now I need to apply the third change - modify the generation bump section to only run when
observed_inflightis True. Let me find the relevant lines:Assistant
Let me see the lines around line 536 to understand the structure and apply the fix properly.
Assistant
Now I need to replace the generation bump section with one that's guarded by
observed_inflight. Let me use sed to replace lines 536-546:Assistant
The user wants me to continue fixing the file. I deleted lines 536-546 and now I need to insert the new generation bump code with the
observed_inflightguard. Let me check the current state and insert the replacement code.Assistant
I need to insert the generation bump code with the
observed_inflightguard. Let me insert it after line 534.Assistant
Let me check the result:
Assistant
The fix has been applied correctly. Let me verify by running the reproducer script to confirm the fix works:
Assistant
The reproducer is simulating the bug manually, not testing the actual fixed code. I need to create a test that actually exercises the fixed
_fetch_tokens_with_refreshfunction. Let me update the reproducer to properly test the fix.Assistant
Now let me run the updated reproducer to see if the fix works.
Assistant
The fix works. The generation is NOT bumped when the early-cancel race condition occurs. Let me now run the existing tests to make sure I haven't broken anything.
Assistant
Let me run the existing tests related to refresh to make sure the fix doesn't break anything.
Assistant
Let me check how to run the tests properly.
Assistant
Now let me run the tests:
Assistant
The tests need pytest-asyncio. Let me install it and try again:
Assistant
Now let me run the tests again:
Assistant
All 19 tests pass. Let me run more tests to make sure the fix doesn't break anything else:
Assistant
The user's fix is working correctly. All the relevant tests pass. Let me clean up the test file I created and submit the solution.
Assistant
Let me verify the final fix one more time before submitting:
Assistant
Let me run the relevant tests one more time to confirm everything still works:
Assistant
All tests pass. Let me submit the fix.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.