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 relevant code.
Assistant
Let me explore the repository structure first with a single command.
Assistant
Let me look at the cancel-recovery loop section more closely.
Assistant
Let me open the relevant section of the file to see the full cancel-recovery loop.
Assistant
Now let me see the exact lines of the cancel-recovery loop and the generation bump.
Assistant
Now let me look at the existing tests to understand testing patterns and find related tests.
Assistant
Let me look at the existing refresh race tests.
Assistant
Now let me look at the facade module to understand how monkeypatching is mirrored.
Assistant
Let me look at the mirror names set to confirm
_coalesced_run_refresh_cmdis in it.Assistant
Let me check the Python environment and how tests are run.
Assistant
Python 3.13.14. Now let me create a reproduction script to confirm the bug. I'll create it as a temporary file and remove it before submitting.
Assistant
I need to install the package or set PYTHONPATH. Let me check.
Assistant
I need to install the test dependencies. Let me check what's needed.
Assistant
I've reproduced the bug — the generation is bumped to 1 even though no subprocess ran. Now let me implement the fix. Let me open the file at the relevant section.
Assistant
Now let me implement the fix. I'll open the file at the relevant section first.
Assistant
Now I'll make the edit to fix the spurious generation bump. I'll replace the cancel-recovery loop and bump section.
Assistant
Let me view the rest of the edited section to confirm correctness.
Assistant
The edit looks correct. Now let me run the reproduction test to confirm the fix works.
Assistant
The fix works — generation stays at 0. Now let me run the existing refresh-related tests to make sure I didn't break anything.
Assistant
All existing refresh tests pass. Let me now add a few more edge-case tests to my temporary reproduction script to be thorough about the success path and the cancelled-while-subprocess-running-success path.
Assistant
All edge cases pass. Now let me check whether
auth.pyhas a duplicate copy of this code (the issue mentions the code was moved fromauth.py).Assistant
Good — the code only lives in
_auth/refresh.py(auth.py is just a re-export facade). Now let me remove the temporary test file and run the broader test suite.Assistant
The 6 errors are due to a missing
httpx_mockfixture (frompytest-httpx), unrelated to my change. Let me install it and re-run to confirm.Assistant
Those tests pass now — the errors were purely environment-related. Let me run the full concurrency and auth-related test suites to confirm everything passes.
Assistant
Let me look at the failures to check whether they're related to my change.
Assistant
All failures are Playwright/CLI-login related (browser not installed), unrelated to my change. Let me confirm they're pre-existing by stashing my change and running those tests.
Assistant
Confirmed — those failures are pre-existing (Playwright not installed in this environment), not caused by my change. Let me run the auth/refresh/keepalive-related tests specifically to confirm my change is clean.
Assistant
All unit tests pass (excluding pre-existing Playwright failures). Let me do a final run of the concurrency tests to confirm everything is green.
Assistant
All concurrency tests pass. Let me review the final diff of my change.
Assistant
The diff is clean and correct. Let me do a final syntax/import check to make sure the module loads correctly.
Assistant
Let me verify the final git status shows only the intended change (no leftover temp files).
Assistant
Only the intended change remains. Let me do a final review of the complete fixed section to confirm correctness.
Assistant
The fix is complete and correct. Let me run the original refresh race tests one final time as a sanity check before submitting.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.