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
I'll start by exploring the repository structure and understanding the code that needs to be fixed.
Assistant
I found the file structure. Let me look at the
_auth/refresh.pyfile mentioned in the issue.<tool_call> <function=bash_terminal> <parameter=command> find /notebooklm-py -type f -name "refresh.py" -path "/_auth/" </tool_call>
Assistant
The user wants me to find the refresh.py file in the _auth directory. I need to use the bash_terminal tool correctly with a command parameter. Let me search for the file.
Assistant
Found it. Let me explore the repository structure and then open the relevant file.
Assistant
Good, found the file. Now let me open it to understand the structure of
_coalesced_run_refresh_cmdand the cancel-recovery loop in_fetch_tokens_with_refresh.Assistant
Let me try the open command with the correct format.
Assistant
The user is having trouble with the tool calls. Let me try using the bash_terminal to cat the file instead.
Assistant
The output was truncated. Let me see the specific parts I need - the
_fetch_tokens_with_refreshfunction and the cancel handler.Assistant
Let me view the specific sections around the cancel-recovery loop and the generation bump:
Assistant
Let me see the
_fetch_tokens_with_refreshfunction which contains the cancel-recovery loop:Assistant
Now let me see the
_fetch_tokens_with_refreshfunction where the cancel-recovery loop and generation bump are:Assistant
Now I understand the issue clearly. Let me analyze the bug:
- In
_fetch_tokens_with_refresh, when a task is cancelled betweentry:(line 488) and the firstwith _REFRESH_STATE_LOCK:inside_coalesced_run_refresh_cmd(line 172), no future gets registered. - In the
except asyncio.CancelledError:handler (line 493),registry.get(refresh_key)returnsNone. - Since
inflight is None or inflight.done()isTrue, we enter the branch and skip theif inflight is not None:block. subprocess_excstaysNone, webreak, and then incorrectly bump the generation at lines 538-543.
Let me first create a reproducer script to confirm the bug:
- In
Assistant
Bug confirmed. The generation was bumped from 5 to 6 even though no subprocess ran. Now let me implement the fix. I'll track whether
inflightwas ever non-None during the cancel-recovery loop, and only bump the generation ifsubprocess_exc is None AND we observed an inflight future.Let me look at the exact lines to modify:
Assistant
Good, I've confirmed the bug. Now let me implement the fix. The issue is that when
inflight is Nonein the cancel handler,subprocess_excstaysNone, which causes the generation to be bumped even though no subprocess ran.The suggested fix is to raise immediately when
inflight is Noneat the top of the cancel handler, since the subprocess never registered. I'll implement this by treating theinflight is Nonecase as a subprocess cancellation (settingsubprocess_exc = asyncio.CancelledError()), which will cause us to skip the generation bump.Assistant
The user wants me to edit the file. Let me try the edit command again with proper formatting. I need to replace lines 493-520 in the file. Let me be more careful with the syntax.
Assistant
Let me try using the bash_terminal with sed to make the edit instead:
Assistant
Let me use Python to do the replacement more reliably:
Assistant
Now let me verify the changes were applied correctly:
Assistant
Now let me run the reproducer to confirm the fix works:
Assistant
The test script was simulating the bug scenario directly rather than testing the actual module. Let me create a proper test that exercises the real code path:
Assistant
Excellent! The fix is working. Now let me run the existing tests to make sure I haven't broken anything:
Assistant
I found the test files. Let me run the auth-related tests to make sure my fix doesn't break anything.
Assistant
Let me install the package or set up the environment properly:
Assistant
The user wants me to run the existing tests to make sure my fix doesn't break anything. Let me try running the tests again now that the package is installed.
Assistant
Let me install the missing dependencies and try running the tests again.
Assistant
I need to run the existing tests to make sure my fix doesn't break anything. Let me try running the tests again now that pytest-httpx is installed.
Assistant
Let me see the remaining test results and understand the failures:
Assistant
Let me check if these test failures existed before my change by temporarily reverting:
Assistant
Good, my changes are exactly what I intended. Now let me check if the test failures existed before my change by temporarily reverting:
Assistant
The test failures existed BEFORE my changes. Let me restore my fix:
Assistant
The test failures existed before my changes, so they're pre-existing issues not caused by my fix. Let me verify my fix is in place:
Assistant
Let me run my custom tests one more time to confirm the fix works:
Assistant
Let me verify the fix handles all edge cases correctly by reviewing the complete changed section one more time:
Assistant
The fix is complete and verified. Let me clean up the temporary files:
Assistant
My fix is complete. Let me provide a final summary of the changes made:
Summary of Changes
Issue
The
_coalesced_run_refresh_cmdfunction had a race condition where_REFRESH_GENERATIONScould be bumped even though no real refresh happened. This occurred when a task was cancelled in the narrow window between thetry:statement and the firstwith _REFRESH_STATE_LOCK:block inside , so no future was registered in .Assistant
The user wants me to submit my changes. I've already verified that:
- The bug was reproduced successfully
- The fix works correctly (both test cases passed)
- The fix handles all edge cases properly
- Pre-existing test failures are not caused by my changes
- Temporary files have been cleaned up
Now I should submit the changes using the
submitcommand.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.