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
Assistant
Now let me look at the test files related to the refresh state machine and refresh lock tests to understand existing tests better:
Assistant
Now let me look at the auth module to ensure I understand if it's just a facade or has its own implementation:
Assistant
Let me look at the
_coalesced_run_refresh_cmdfunction more carefully:Assistant
Let me look for existing tests that might exercise this behavior:
Assistant
Now let me check the CHANGELOG to see how recent changes are documented:
Assistant
Let me write a quick reproducer to confirm the bug:
Assistant
Hmm, the monkeypatch didn't work as expected. Let me debug. The result is
ValueError, notCancelledError. Let me check the auth module to understand the actual entry pointAssistant
I see, the auth module uses a facade module class. When I monkeypatch
auth_mod._fetch_tokens_with_jar, it should propagate to the underlying_auth.refresh. But it didn't work... Let me debug more.Actually wait, the test result says
ValueError("Authentication expired. Run 'notebooklm login'."). This is the first ValueError raised by_fetch_tokens_with_jar. But this ValueError should be caught by_fetch_tokens_with_refreshand trigger a refresh. The_should_try_refreshshould return True (since I set the env var). But it returnedValueErrorinstead, which means_should_try_refreshreturned False.Let me check what NOTEBOOKLM_REFRESH_CMD_ENV is:
Assistant
I've reproduced the bug. Now let me apply the fix. Looking at the suggested fix from the issue, I'll go with option 2 (raise immediately when
inflight is None) since it's the cleaner approach:Let me view the current code one more time and apply the fix:
Assistant
Now let me apply the fix:
Assistant
Now let me verify the fix was applied correctly:
Assistant
Now let me run my reproducer to confirm the fix:
Assistant
The fix works. Now let me verify with more thorough edge cases:
Assistant
tests share state because the facade module persists monkeypatches. Let me fix the tests:
Assistant
me look at how the function resolves
_coalesced_run_refresh_cmd:Assistant
I see, the facade module's write-through causes my "restore" to be a no-op. Let me use a different approach - save the original at module load time:
Assistant
All edge case tests pass. Now let me run the existing tests to make sure I didn't break anything:
Assistant
The pip list doesn't show
notebooklm-pyeven though I diduv pip install -e .. Let me check againAssistant
The src is in the path, so the package is importable. But for some reason pytest isn't picking it up. Let me try with
-p no:cacheprovideror check if there's a conftest issue.Actually wait, pytest says it can't import notebooklm. But the python import works. The difference is probably the path setup. Let me see:
Assistant
Ah, the src directory is in the path because of some
conftest.pyor.pthfile or similar. Let me check the conftest:Assistant
There's a global pytest at
/usr/local/bin/pytestbut my venv has its own pytest. The global one doesn't have access to the venv's site-packages, so it can't find notebooklm.Let me use the venv's pytest:
Assistant
There's no pytest in the venv bin. Let me install it via
Assistant
All 4 tests in
test_refresh_cmd_race.pypass. Now let me run the broader test suite:Assistant
errors are due to missing pytest-httpx (provides
httpx_mockfixture). Let me install it:Assistant
All 94 concurrency tests pass. Let me also run the unit tests related to the refresh:
Assistant
All 313 tests pass. Let me also run the broader unit test suite to check for any regressions:
Assistant
The error is due to missing playwright (which is for browser tests). Let me skip those:
Assistant
This failure is due to missing playwright module (which is a separate optional dependency). Let me check if this test failure was pre-existing by running it on the original code (without my fix):
Assistant
- the failure is pre-existing (due to missing playwright, not my fix). Let me run all unit tests except those that require playwright:
Assistant
6 tests passed, 13 skipped. All the tests that don't depend on playwright pass. Let me also check the integration tests for refresh:
Assistant
This test failure is due to VCR cassette context not being active - it's a setup issue, not related to my fix. Let me verify by running it without my fix:
Assistant
- the failure is pre-existing (due to missing VCR cassette setup, not my fix).
Let me also run my edge case tests one more time to confirm they pass:
Assistant
Let me also confirm the fix is in place by checking the final code:
Assistant
Let me also run the original reproducer to confirm:
Assistant
The fix works correctly. Let me view the final diff:
Assistant
The diff is clean. Let me also clean up my temporary test files:
Assistant
Now let me verify the final state of the fix once more:
Assistant
Only the
src/notebooklm/_auth/refresh.pyfile is modified. Let me run a final verification of the fix:Assistant
The fix is verified. Let me do a final summary and then submit.
Assistant
Now I'm ready to submit the fix:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.