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 this issue:
-
There's a race condition in
_coalesced_run_refresh_cmdwhere_REFRESH_GENERATIONS[refresh_key]can be bumped even when no real refresh happened. -
The scenario:
- A task is cancelled between the
try:block and the firstwith _REFRESH_STATE_LOCK:block - No future is registered in
_REFRESH_INFLIGHT_BY_LOOP[loop][refresh_key] - When the exception is caught,
inflightisNone, so the inflight block is skipped subprocess_excstaysNone- The generation is bumped even though no subprocess ran
- This causes other waiters to skip their own refresh unnecessarily
- A task is cancelled between the
-
The suggested 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.
Let me first explore the repository structure and find the relevant file.
-
Assistant
Let me find the refresh.py file mentioned in the issue.
<tool_call> <function=bash_terminal> <parameter=command> find /notebooklm-py -name "refresh.py" -type f </tool_call>
Assistant
I need to use the bash_terminal function to run the command.
Assistant
Good, let me look at the refresh.py file, specifically around lines 487-537 as mentioned in the issue.
<tool_call> <function=bash_terminal> <parameter=command> wc -l /notebooklm-py/src/notebooklm/_auth/refresh.py </tool_call>
Assistant
I need to use the bash_terminal function properly to execute commands.
Assistant
The file has 744 lines. Let me open it and look at the relevant section. Let me use the open command to view the file.
Assistant
Now let me see the
_coalesced_run_refresh_cmdfunction itself. Let me search for it.<tool_call> <function=bash_terminal> <parameter=command> grep -n "_coalesced_run_refresh_cmd" /notebooklm-py/src/notebooklm/_auth/refresh.py </tool_call>
Assistant
I need to use the bash_terminal function properly to search for the function.
Assistant
Good, so the
_coalesced_run_refresh_cmdfunction starts at line 144. Let me see the full function.<tool_call> <function=bash_terminal> <parameter=command> sed -n '144,240p' /notebooklm-py/src/notebooklm/_auth/refresh.py </tool_call>
Assistant
I need to use the bash_terminal function properly to execute the command. Let me try again.
Assistant
Assistant
I need to use the bash_terminal function properly. Let me try a different approach.
Assistant
Assistant
I need to properly use the bash_terminal tool to run commands. Let me try again with the correct format.
Assistant
Now I have a clear view of the code. The issue is:
- When
_coalesced_run_refresh_cmdis cancelled before registering a future,inflightisNone. - The code checks
if inflight is None or inflight.done():- this is True. - Then
if inflight is not None:- this is False, so skipped. subprocess_excstaysNone.breakhappens.- The check
if subprocess_exc is not None:is False, so no raise happens. - Generation is bumped even though no subprocess ran.
The fix is to track whether we ever observed a non-None inflight. If we didn't, we shouldn't bump the generation.
Let me implement the fix by modifying the code to add an
observed_inflightflag.I'll use the edit command to modify the file.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '480,550p' /notebooklm-py/src/notebooklm/_auth/refresh.py </tool_call>
- When
Assistant
I need to use the bash_terminal function properly to run commands. Let me get the file content to see the exact lines I need to edit.
Assistant
Now I have a clear view of the code. The issue is:
- When
_coalesced_run_refresh_cmdis cancelled before registering a future,inflightisNone. - The code checks
if inflight is None or inflight.done():- this is True. - Then
if inflight is not None:- this is False, so skipped. subprocess_excstaysNone.breakhappens.- The check
if subprocess_exc is not None:is False, so no raise happens. - Generation is bumped even though no subprocess ran.
The fix is to track whether we ever observed a non-None inflight. If we didn't, we shouldn't bump the generation.
Let me implement the fix by adding an
observed_inflightflag:- When
Assistant
Let me verify the fix was applied correctly:
Assistant
The fix looks correct. Now let me run the existing tests to make sure the fix doesn't break anything:
<tool_call> <function=bash_terminal> <parameter=command> cd /notebooklm-py && python -m pytest tests/ -xvs 2>&1 | head -100 </tool_call>
Assistant
I need to use the bash_terminal function to run the command.
Assistant
The module isn't installed. Let me install it first.
<tool_call> <function=bash_terminal> <parameter=command> cd /notebooklm-py && pip install -e . 2>&1 | tail -20 </tool_call>
Assistant
I need to use the bash_terminal function to run the pip install command.
Assistant
Good, the package is installed. Now let me run the tests:
<tool_call> <function=bash_terminal> <parameter=command> cd /notebooklm-py && python -m pytest tests/ -xvs 2>&1 | head -200 </tool_call>
Assistant
I need to use the bash_terminal function properly to run the command.
Assistant
The vcr module is missing. Let me install it and run the tests again. But first, let me focus on unit tests only and skip integration tests.
<tool_call> <function=bash_terminal> <parameter=command> cd /notebooklm-py && python -m pytest tests/unit -xvs 2>&1 | head -200 </tool_call>
Assistant
The bash_terminal tool needs to be called properly. Let me try again with the correct syntax.
Assistant
Let me install the missing dependencies and try again.
<tool_call> <function=bash_terminal> <parameter=command> pip install pytest-httpx 2>&1 | tail -10 </tool_call>
Assistant
I need to use the bash_terminal function properly to run the command.
Assistant
Now let me run the tests again to see if there are any issues with my fix. But first, let me look for tests specifically related to refresh functionality.
Assistant
Let me run the unit tests related to refresh:
Assistant
The test failed because of a missing pytest-asyncio plugin. Let me install it and try again.
<tool_call> <function=bash_terminal> <parameter=command> pip install pytest-asyncio 2>&1 | tail -5 </tool_call>
Assistant
I need to use the bash_terminal function properly to run the command.
Assistant
Now let me run the refresh-related tests again:
Assistant
All the refresh-related unit tests passed! Now let me also run the test for the specific race condition mentioned in the issue:
<tool_call> <function=bash_terminal> <parameter=command> cat /notebooklm-py/tests/unit/test_concurrency_refresh_race.py </tool_call>
Assistant
Let me view the content of the test file to understand what tests are there.
Assistant
Assistant
I need to use the bash_terminal tool properly to run the test script.
Assistant
The file was not created properly. Let me create it again.
<tool_call> <function=bash_terminal> <parameter=command> cat > /tmp/test_spurious_bump.py << 'ENDOFFILE' """Test to reproduce the spurious _REFRESH_GENERATIONS bump on early-cancel.""" import asyncio from pathlib import Path from notebooklm._auth.refresh import ( _REFRESH_GENERATIONS, _REFRESH_STATE_LOCK, _get_inflight_registry, _coalesced_run_refresh_cmd, )
async def test_generation_bump_logic(): """ Test that _REFRESH_GENERATIONS is NOT bumped when _coalesced_run_refresh_cmd is cancelled before it can register a future. """ refresh_key = "test_refresh_key"
Assistant
The command didn't seem to be executed. Let me try a different approach to create and run the test file.
Assistant
The logic test passes. Now let me run more comprehensive tests to ensure the fix doesn't break any existing functionality.
Assistant
The test is failing because vcr module is missing. Let me skip this file and continue testing.
<tool_call> <function=bash_terminal> <parameter=command> cd /notebooklm-py && python -m pytest tests/unit/ -x --ignore=tests/unit/concurrency/ --ignore=tests/unit/test_cassette_patterns.py -q 2>&1 | tail -100 </tool_call>
Assistant
I need to use the bash_terminal tool to run the pytest command.
Assistant
Let me skip all tests that import vcr:
<tool_call> <function=bash_terminal> <parameter=command> cd /notebooklm-py && python -m pytest tests/unit/ -x --ignore=tests/unit/concurrency/ --ignore=tests/unit/test_cassette_patterns.py --ignore=tests/unit/test_cassette_sanitizer.py -q 2>&1 | tail -100 </tool_call>
Assistant
I need to use the bash_terminal tool to run the pytest command. Let me try again with the proper format.
Assistant
Let me install the missing dependencies and try running only the refresh-related tests:
Assistant
Now let me run the refresh-related tests again to ensure the fix doesn't break any existing functionality:
Assistant
All the refresh-related unit tests pass! Now let me also verify the fix by looking at the modified code to make sure it's correct.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '485,560p' /notebooklm-py/src/notebooklm/_auth/refresh.py </tool_call>
Assistant
I need to use the bash_terminal tool properly to run the command.
Assistant
The fix looks correct. Let me summarize what the fix does:
-
Added
observed_inflight = Falsebefore the while loop to track whether we ever saw a non-None inflight future. -
Updated
observed_inflight = inflight is not Noneinside the lock when checking for cancellation, so we track whether a future was registered. -
Wrapped the generation bump in
if observed_inflight:so we only bump the generation if we actually observed an inflight future (i.e., a subprocess was actually running).
This fix ensures that when
_coalesced_run_refresh_cmdis cancelled before it can register a future, the generation is NOT bumped because no subprocess was actually running.Let me also run some additional tests to ensure the fix doesn't break any edge cases:
<tool_call> <function=bash_terminal> <parameter=command> cd /notebooklm-py && python -m pytest tests/unit/concurrency/ -xvs 2>&1 | tail -100 </tool_call>
-
Assistant
Let me use the bash_terminal tool to run the command.
Assistant
All the concurrency tests pass as well. Let me also check if there are any integration tests related to refresh that I should run. However, since the issue is specifically about the unit logic and the fix is small and targeted, I'm confident that the fix is correct.
Let me now verify the fix by reviewing the code changes one more time and then submit.
<tool_call> <function=bash_terminal> <parameter=command> cd /notebooklm-py && git diff src/notebooklm/_auth/refresh.py </tool_call>
Assistant
I need to use the bash_terminal tool to run the git diff command.
Assistant
The git diff shows the changes I made to fix the issue. Let me verify my fix handles all edge cases correctly:
Edge case 1: Early cancel (bug scenario)
- When
_coalesced_run_refresh_cmdis cancelled before registering a future,inflightisNone,observed_inflightstaysFalse, and the generation is NOT bumped. ✓
Edge case 2: Normal case
- When
_coalesced_run_refresh_cmdregisters a future and completes successfully,inflightis notNone,observed_inflightisTrue, and the generation IS bumped. ✓
Edge case 3: Cancel after subprocess started (subprocess succeeded)
- When
_coalesced_run_refresh_cmdis cancelled after registering a future and the subprocess succeeds,inflightis notNone,observed_inflightisTrue, and the generation IS bumped. ✓
Edge case 4: Subprocess failure
- When the subprocess fails,
subprocess_excis set, and we raise the exception without bumping the generation. ✓
All edge cases are handled correctly. Now let me verify one more time that all the refresh-related tests pass:
- When
Assistant
All tests pass. Let me now write a summary of the fix and submit.
Summary of the fix:
The issue was a race condition in
_coalesced_run_refresh_cmdwhere_REFRESH_GENERATIONS[refresh_key]could be bumped even when no real refresh happened. This occurred when the task was cancelled in the window between thetry:and the firstwith _REFRESH_STATE_LOCK:block inside_coalesced_run_refresh_cmd, so no future was registered.The fix adds an
observed_inflightflag that tracks whether we ever observed a non-None inflight future. The generation is only bumped ifobserved_inflightisTrue, which means a subprocess was actually running.Checklist:
- Reproduced the issue (understood the race condition scenario)
- Fixed the issue by adding
observed_inflighttracking - Handled edge cases (early cancel, normal case, cancel after subprocess started, subprocess failure)
- Ran existing tests (all refresh-related tests pass)
Assistant
The user is asking me to submit my changes. Let me use the submit command to finalize the fix.
Assistant
The user wants me to submit my changes. I've fixed the issue by:
-
Adding
observed_inflight = Falsebefore the while loop to track whether we ever saw a non-None inflight future. -
Updating
observed_inflight = inflight is not Noneinside the lock when checking for cancellation, so we track whether a future was registered. -
Wrapping the generation bump in
if observed_inflight:so we only bump the generation if we actually observed an inflight future (i.e., a subprocess was actually running).
All the refresh-related unit tests pass. Let me submit the changes.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.