Skip to content
TheChatArchiveThe Chat Archive
DeepSeekdeepseek/deepseek-v4-pro· deepseekHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION auth: spurious REFRESHGENERATIONS bump on early-cancel in…

  1. User

    ISSUE DESCRIPTION

    auth: spurious _REFRESH_GENERATIONS bump on early-cancel in _coalesced_run_refresh_cmd

    Summary

    _coalesced_run_refresh_cmd in src/notebooklm/_auth/refresh.py (moved from src/notebooklm/auth.py in 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)

    1. Caller invokes await _coalesced_run_refresh_cmd(refresh_key, path, profile) from the cancel-recovery loop in _fetch_tokens_with_refresh.
    2. Task is cancelled in the window between try: and the first with _REFRESH_STATE_LOCK: block inside _coalesced_run_refresh_cmd — so NO future is registered in _REFRESH_INFLIGHT_BY_LOOP[loop][refresh_key].
    3. Control jumps to except asyncio.CancelledError: in the caller.
    4. registry.get(refresh_key) returns None.
    5. The inner if inflight is None or inflight.done(): is taken; the if inflight is not None: block is skipped; subprocess_exc stays None; break.
    6. Outer if subprocess_exc is not None: is False, so no raise.
    7. Generation is bumped at the with _REFRESH_STATE_LOCK: ... max(existing, refresh_generation + 1) line, even though no subprocess ran.
    8. Then if caller_cancelled: raise asyncio.CancelledError().

    Impact

    A concurrent waiter on a different event loop sharing the same refresh_key would 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 to build_httpx_cookies_from_storage against 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 inflight was ever non-None during the cancel-recovery loop. Only bump generation if subprocess_exc is None AND observed_inflight is True. Or per the alternative suggestion: raise immediately when inflight is None at 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).

    Why not fixed in PR #814

    That PR's scope was "extract verbatim from auth.py into _auth/refresh.py + facade-forward". Changing async semantics was explicitly out of scope (must_not_do). Filing here for a dedicated follow-up.

    References

    ADDITIONAL ADVICE

    Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use git checkout <file> to restore the file to its original state. You can also reset all changes in the repository using git reset --hard command. Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to get the changes that need to be submitted.

    CHECKLIST

    Before submitting your solution, please go over the following checklist and make sure you've done everything:

    • If an error was reported in the issue description, I have successfully reproduced it.
    • If an error was reported in the issue description, I have confirmed that my fix resolves the error.
    • I have thought about edge cases and made sure my fix handles them as well.
    • I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.

    Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!

    (Current directory: /notebooklm-py, current file: none) bash-$

  2. DeepSeek Assistant

    Let me start by understanding the repository structure and the issue. The issue is about a spurious _REFRESH_GENERATIONS bump in _coalesced_run_refresh_cmd when there's an early cancel. Let me first explore the relevant files.

    The key file is src/notebooklm/_auth/refresh.py. Let me look at the structure of the repository first, then examine the relevant code.

  3. DeepSeek Assistant

    Let me look at the refresh.py file, particularly around lines 487-537 as mentioned in the issue.

  4. DeepSeek Assistant

    Let me open the file and look at the relevant section.

  5. DeepSeek Assistant

    Now let me look at the _coalesced_run_refresh_cmd function:

  6. DeepSeek Assistant

    Let me look at the _coalesced_run_refresh_cmd function starting at line 144.

  7. DeepSeek Assistant

    Let me look at how refresh_generation is set in the caller.

  8. DeepSeek Assistant

    Now let me also look at the global state variables and understand the full picture:

  9. DeepSeek Assistant

    Let me continue reading the global state.

  10. DeepSeek Assistant

    Now I have a clear understanding of the issue. Let me summarize:

    The Problem: In _fetch_tokens_with_refresh, the while-loop at lines 487-520 calls _coalesced_run_refresh_cmd. If cancellation happens BEFORE _coalesced_run_refresh_cmd can register its future in the registry (i.e., before line 172), the cancel handler at line 493 finds inflight = None. Since inflight is None, the if inflight is not None: block is skipped, subprocess_exc stays None, and we break. Then at line 522, subprocess_exc is not None is False, so we proceed to bump generation at lines 538-543 — even though no subprocess actually ran.

    The Fix: When inflight is None in the cancel handler, we know the subprocess was never registered. We should set subprocess_exc = asyncio.CancelledError() so that the subsequent if subprocess_exc is not None: check correctly routes to the error path (no generation bump) instead of the success path.

    Let me apply this

  11. DeepSeek Assistant

    Let me go to the right line:

  12. DeepSeek Assistant

    Now I'll apply the fix. I need to add an else clause to handle the case where inflight is None in the cancel handler

  13. DeepSeek Assistant

    The edit looks correct. Now let me verify the change by tracing through the logic and running existing tests. Let me first check if there are relevant tests.

  14. DeepSeek Assistant

    Let me check the concurrency test for the refresh cmd race, which is likely the most relevant.

  15. DeepSeek Assistant

    Let me look at the cancellation

  16. DeepSeek Assistant

    Let me look at the refresh_cmd_race

  17. DeepSeek Assistant

    Let me run the existing relevant tests to make sure my change doesn't break anything

  18. DeepSeek Assistant

    Let me install the package

  19. DeepSeek Assistant

    Now let me run the relevant tests

  20. DeepSeek Assistant

    Let me install the test dependencies

  21. DeepSeek Assistant

    Now let me run the tests

  22. DeepSeek Assistant

    All 4 tests pass. Let me now create a reproduction script to confirm the issue exists and that my fix resolves it

  23. DeepSeek Assistant

    The fix works. Let me verify that the old code would have had the bug by temporarily reverting and running the test

  24. DeepSeek Assistant

    Confirmed: the bug exists without the fix (generation bumped to 1). Let me restore the

  25. DeepSeek Assistant

    Now let me also run the broader test suite to make sure nothing is broken

  26. DeepSeek Assistant

    Let me run all related tests to ensure nothing is

  27. DeepSeek Assistant

    Let me also run the cancellation propagation test and the public

  28. DeepSeek Assistant

    All tests pass. Let me also run the full unit test suite to make sure nothing else

  29. DeepSeek Assistant

    The failures/errors (12 failed, 21 errors) are all in tests/unit/cli/test_session.py which is about Playwright/CLI tests — completely unrelated to my change. All 4673 other tests pass.

    Let me verify the diff looks correct

  30. DeepSeek Assistant

    The change is minimal and correct. Let me verify the final state of the file around the fix

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.