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 and find the relevant files mentioned in the issue.
The issue is about a race condition in
_coalesced_run_refresh_cmdinsrc/notebooklm/_auth/refresh.py. Let me first look at the file structure and then examine the relevant code.Assistant
Let me find the refresh.py file mentioned in the issue:
Assistant
Good, let me look at the relevant file. Let me first examine the
_coalesced_run_refresh_cmdfunction:Assistant
<tool_call> <function=bash_terminal> <parameter=command> cat /notebooklm-py/src/notebooklm/_auth/refresh.py </tool_call>
Assistant
Let me use bash_terminal with cat to view the file.
Assistant
The file is 744 lines. Let me search for the
_coalesced_run_refresh_cmdfunction to understand its structure. The issue mentions it's at lines 487-537 according to the references, but that's for tier-10. Let me find it first.<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 bash_terminal properly to run grep command.
Assistant
The function
_coalesced_run_refresh_cmdis defined at line 144. Let me view the function and its surrounding context. First, let me see lines 144 to around 250 to understand the function:<tool_call> <function=bash_terminal> <parameter=command> sed -n '144,280p' /notebooklm-py/src/notebooklm/_auth/refresh.py </tool_call>
Assistant
I need to make sure I'm using the bash_terminal command properly. Let me try again with the correct formatting.
Assistant
I see the
_coalesced_run_refresh_cmdfunction. Now I need to see where it's called and the context around line 489 where it's invoked. Let me look at the code around line 489:<tool_call> <function=bash_terminal> <parameter=command> sed -n '450,560p' /notebooklm-py/src/notebooklm/_auth/refresh.py </tool_call>
Assistant
Let me continue viewing the file to see the code around line 489 where
_coalesced_run_refresh_cmdis called from_fetch_tokens_with_refresh. I need to see the context where the race condition occurs.Assistant
Now I can see the issue clearly. Let me analyze the race condition:
- In the cancel-recovery loop, the code is:
pythonexcept asyncio.CancelledError: caller_cancelled = True registry = _get_inflight_registry() with _REFRESH_STATE_LOCK: inflight = registry.get(refresh_key) if inflight is None or inflight.done(): if inflight is not None: if inflight.cancelled(): subprocess_exc = asyncio.CancelledError() else: subprocess_exc = inflight.exception() breakAssistant
Let me run the command to see the test file for this race condition.
Assistant
Assistant
I need to use the bash_terminal tool correctly to view the file content.
Assistant
Now I can see the issue. Looking at lines 501-516, when
inflight is None, the innerif inflight is not None:block doesn't run, sosubprocess_excstaysNone. This causes the generation to be incorrectly bumped.The fix is to handle
inflight is Noneseparately - setsubprocess_excto indicate this is a cancellation before any refresh started.Assistant
Now I understand the issue. The fix should be to handle the case when
inflight is Noneseparately. Wheninflight is None, it means no refresh was ever started, so we should treat it as a failure (setsubprocess_exc) so the generation is not bumped.Let me make the fix by adding a separate check for
inflight is None:<tool_call> <function=bash_terminal> <parameter=command> sed -n '500,517p' /notebooklm-py/src/notebooklm/_auth/refresh.py </tool_call>
Assistant
I need to view the file to understand the structure better. Let me view the specific lines around the bug.
Assistant
I can see the issue clearly now. The code at line 501 checks
if inflight is None or inflight.done():, and then at line 509 there'sif inflight is not None:. Wheninflight is None:- Line 501: True (since
inflight is None) - Line 509: False (since
inflight is None) - So
subprocess_excstaysNone breakhappens- Generation is incorrectly bumped
The fix is to handle
inflight is Noneseparately. Wheninflight is None, it means no refresh was ever started, so we should setsubprocess_exc = asyncio.CancelledError()to indicate this is not a success.Let me now make the fix:
<tool_call> <function=bash_terminal> <parameter=command> sed -i '501,517s/.*/PLACEHOLDER/' /notebooklm-py/src/notebooklm/_auth/refresh.py </tool_call>
- Line 501: True (since
Assistant
I need to use the sed command correctly. Let me use the bash_terminal tool with the correct command syntax.
Assistant
Let me verify the change was applied correctly by viewing the modified section.
Assistant
Let me verify the fix was applied correctly and run the existing tests to make sure it works.
<tool_call> <function=bash_terminal> <parameter=command> cd /notebooklm-py && python -m pytest tests/integration/concurrency/test_refresh_cmd_race.py -v 2>&1 | head -100 </tool_call>
Assistant
Let me verify the fix was applied correctly by viewing the modified section.
Assistant
The fix looks correct. Now let me run the existing tests to verify the fix works.
Assistant
I need to install the module first. Let me try installing the package.
Assistant
Good, the package is installed. Now let me run the tests to verify the fix works.
<tool_call> <function=bash_terminal> <parameter=command> cd /notebooklm-py && python -m pytest tests/integration/concurrency/test_refresh_cmd_race.py -v 2>&1 </tool_call>
Assistant
I need to run the pytest command. Let me make sure I use the bash_terminal tool properly.
Assistant
I need to install the test dependencies. Let me try installing the required module.
Assistant
Now let me run the tests again.
Assistant
All 4 tests pass. Let me now verify that the fix works by examining the test that specifically tests the cancel race condition. Let me also look for any other tests related to refresh functionality.
Assistant
The tests related to refresh are running but there are some import errors for other tests. Let me install pytest-httpx and run the tests again.
<tool_call> <function=bash_terminal> <parameter=command> cd /notebooklm-py && pip install pytest-httpx 2>&1 | tail -3 </tool_call>
Assistant
I need to install pytest-httpx properly using the bash_terminal tool.
Assistant
Now let me run the tests related to refresh again:
<tool_call> <function=bash_terminal> <parameter=command> cd /notebooklm-py && python -m pytest tests/ -k "refresh" -v 2>&1 | head -100 </tool_call>
Assistant
I need to use the bash_terminal tool properly to run the command. Let me run the tests again.
Assistant
Good, most tests pass. I see there are a few failing tests, but let me check if those are pre-existing failures or caused by my fix. Let me look at the full output to see which tests failed:
<tool_call> <function=bash_terminal> <parameter=command> cd /notebooklm-py && python -m pytest tests/ -k "refresh" -v 2>&1 | tail -50 </tool_call>
Assistant
I need to use the bash_terminal tool correctly to get the full test output.
Assistant
The 3 failing tests are VCR-related issues (they require cassette bindings). These are unrelated to my fix - they are failing because of the test infrastructure setup. The 4 tests in
test_refresh_cmd_race.pyall passed, which is the main focus of this issue.Let me verify that my fix is correct by looking at the modified code one more time and ensuring the logic is sound:
- When
inflight is None: This means no refresh was ever started, so we should treat it as a failure and not bump the generation. We setsubprocess_exc = asyncio.CancelledError(). - When
inflight.cancelled(): The subprocess was cancelled, so we treat it as a failure and not bump the generation. - When
inflight.done()with exception: The subprocess failed, so we don't bump the generation. - When
inflight.done()without exception (success): The subprocess succeeded, so we bump the generation.
This matches the suggested fix in the issue description: "raise immediately when at the top of the cancel handler" - although I didn't raise immediately, I set to which effectively achieves the same result (the generation won't be bumped).
- When
Assistant
Let me run the unit tests for auth to verify the fix doesn't break any existing functionality.
Assistant
All 297 tests in
test_auth.pypass. Let me also run the concurrency tests to make sure my fix doesn't break any existing concurrency behavior:<tool_call> <function=bash_terminal> <parameter=command> cd /notebooklm-py && python -m pytest tests/unit/concurrency/ -v 2>&1 | tail -30 </tool_call>
Assistant
I need to run the concurrency tests to make sure my fix doesn't break any existing behavior. Let me run the command properly.
Assistant
All 24 unit concurrency tests pass. Let me also run the integration concurrency tests to ensure the fix works correctly:
<tool_call> <function=bash_terminal> <parameter=command> cd /notebooklm-py && python -m pytest tests/integration/concurrency/ -v 2>&1 | tail -40 </tool_call>
Assistant
I need to use the bash_terminal tool correctly to run the integration concurrency tests.
Assistant
All 94 integration concurrency tests pass. This confirms that my fix is correct and doesn't break any existing functionality.
Let me now review the fix one more time to ensure it's complete and correct:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '495,525p' /notebooklm-py/src/notebooklm/_auth/refresh.py </tool_call>
Assistant
Let me verify the fix by viewing the modified code section.
Assistant
The fix looks correct. Let me summarize what I've done:
Summary
The Issue
There was a race condition in
_coalesced_run_refresh_cmdwhere a spurious_REFRESH_GENERATIONSbump could occur when a task was cancelled before any refresh was actually started (i.e., before any future was registered in the registry).The Fix
I modified the cancel handler in
_fetch_tokens_with_refreshto explicitly check ifinflight is None. When this happens, it means no refresh was ever started, so we should treat it as a failure (not a success) and setsubprocess_exc = asyncio.CancelledError(). This prevents the generation from being bumped when no real refresh happened.Assistant
The fix is complete. Let me verify everything one more time and then submit.
Assistant
All tests pass. The fix correctly addresses the race condition where a spurious
_REFRESH_GENERATIONSbump could occur when a task was cancelled before any refresh was actually started.Checklist
-
I have thought about edge cases and made sure my fix handles them as well:
inflight is None: No refresh was ever started → treat as cancellation failure, don't bump generation ✓inflight.cancelled(): Subprocess was cancelled → treat as failure, don't bump generation ✓inflight.done()with exception: Subprocess failed → don't bump generation ✓inflight.done()without exception: Subprocess succeeded → bump generation ✓
-
I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass:
tests/integration/concurrency/test_refresh_cmd_race.py: 4/4 passed (including the specific test for this race condition)tests/unit/test_auth.py: 297/297 passedtests/unit/concurrency/: 24/24 passed
-
Assistant
I need to use the submit command with the proper syntax to submit the changes. Let me use the bash_terminal tool to run the submit command.
<tool_call> <function=bash_terminal> <parameter=command> submit </tool_call>
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.