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
Now let me look at the relevant section of the code:
Assistant
me look at the
_coalesced_run_refresh_cmdfunction:Assistant
let me look at the equivalent code in
auth.py:Assistant
So
auth.pyis just a facade. The actual code is in_auth/refresh.py. Now let me look at the tests to understand the testing patterns:Now let me look at the existing tests:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[ls /notebooklm-py/tests/]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>Assistant
Let me retry the command
Assistant
Let me look at the existing tests for refresh state machine and refresh lock:Let me look at refresh-specific tests:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cat /notebooklm-py/tests/unit/test_refresh_state_machine.py]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me retry the command.
Assistant
Now let me look at the integration test for refresh_cmd_race:
Assistant
Now let me look at the rest of the file:Let me look at the rest of the test file:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[sed -n '230,290p' /notebooklm-py/tests/integration/concurrency/test_refresh_cmd_race.py]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me retry the
Assistant
Now let me look at the test_refresh_cancellation_propagation.py file:
Assistant
Now let me look at the existing test_auth.py to understand more about how refresh tests work:
Assistant
Now let me look at the exact code path of the issue. Let me look at the part that triggers the bug:
Assistant
Now I understand the bug. Let me look at the exact lines in the file more carefully:
Assistant
Now let me write a reproducer to confirm the bug:
Assistant
I've confirmed the bug. Now let me implement the fix:
Assistant
Now let me implement the fix. I'll use the alternative suggestion from the issue which is cleaner:
Assistant
Now let me apply the fix:
Assistant
Let me verify the change:
Assistant
Now let me run my reproducer to confirm the fix:
Assistant
fix works. Now let me think about edge cases and make sure my fix is correct. Let me look at the broader context first:
Assistant
Let me run the existing tests to make sure my fix doesn't break anything:
Assistant
All 4 refresh cmd race tests pass. Let me also run the related tests:
Assistant
All 13 related tests pass. Let me run the broader test suite related to auth:
Assistant
All 297 tests in test_auth.py pass. Let me also run the public_shims test to make sure the refresh-related test passes:
Assistant
All 269 public shims tests pass. Now let me write a comprehensive test that exercises the specific bug I'm fixing:
Assistant
test 1 fix works correctly. Test 2 failed because of the test patching issue (the fake
_get_inflight_registryfrom test 1 was still in place). Let me fix the test script:Assistant
Let me debug this test issue:
Assistant
The issue is that the re-import isn't actually re-executing the module code because of caching. Let me fix the test:
Assistant
The test works now with proper module cache clearing. Let me now run all 3 tests properly:
Assistant
All three test scenarios pass:
- Test 1 (the bug): No subprocess then cancel → no phantom bump (gen=0). ✓
- Test 2: Subprocess succeeds then cancel → generation bumped (gen=1). ✓
- Test 3: Subprocess fails then cancel → no bump (gen=0). ✓
The "Future exception was never retrieved" message is just an asyncio warning about Test 1's old fake_run_refresh_cmd task being garbage collected with an unhandled exception — this is a test artifact and doesn't affect functionality.
Now let me check that my fix doesn't break any other tests in the codebase. Let me run a broader test suite:
Assistant
Let me check if these failures are pre-existing (not related to my fix):
Assistant
The failures are pre-existing and due to missing playwright module, not related to my fix. Let me restore my fix:
Assistant
Let me run all tests excluding the pre-existing playwright-related failures:
Assistant
All 4534 unit tests pass. Now let me also run the integration tests:
Assistant
All 94 concurrency tests pass, including the 4 refresh_cmd_race tests. The 130 integration test failures are unrelated to my fix (they're VCR cassette-related). Let me verify they're pre-existing:
Assistant
failures are pre-existing VCR cassette setup issues, unrelated to my fix. Let me confirm by reverting my fix temporarily:
Assistant
The tests pass both with and without my fix (existing tests don't cover the specific bug). My fix handles the bug correctly. Let me clean up my temporary files and verify the final fix:
Assistant
Let me run my comprehensive test once more to confirm:
Assistant
All three tests pass:
- Test 1 (the bug): When cancellation arrives before the future is registered (no subprocess ran), the generation must NOT be bumped. The fix correctly prevents this phantom bump.
- Test 2 (existing behavior preserved): When subprocess succeeded and caller was cancelled, the generation IS bumped (for other waiters' benefit), then CancelledError propagates to this caller.
- Test 3 (existing behavior preserved): When subprocess failed and caller was cancelled, the generation is NOT bumped.
Let me also look at the diff once more to verify it's clean and minimal:
Assistant
me clean up my temporary files:
Assistant
Now let me verify the final state of the file:
Assistant
Let me check the final state of changes and run all the affected tests once more:
Assistant
All 316 tests in the relevant test files pass. The only change is in
src/notebooklm/_auth/refresh.py.Let me do one final review of the fix:
Assistant
Let me also run the reproduce script to ensure the fix is verified:
Assistant
The fix is verified. The "Future exception was never retrieved" warning is from my test's fake_run_refresh_cmd that did run (because the test simulates a slightly different but functionally equivalent race).
Now let me also re-run all unit tests in the affected area to make sure nothing is broken:
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.