Skip to content
TheChatArchiveThe Chat Archive
gpt-5.6-sol· openaiHugging 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. Assistant

    I’ll inspect the repository structure, the affected function, and nearby auth tests first. Then I’ll reproduce the cancellation window if practical, implement the narrow semantic fix, and run focused plus broader tests.

  3. Assistant

    The vulnerable logic is in the caller’s cancellation-recovery loop, exactly as reported. I’ll now inspect _coalesced_run_refresh_cmd registration timing and all existing cancellation regression tests, especially the test near lines 500 that already guards a related phantom bump, to choose a fix consistent with established behavior.

  4. Assistant

    The existing done-future behavior must remain: a cancelled leader whose subprocess really registered and succeeded should still bump generation, while a registered failure must not. The clean discriminator is therefore whether the cancellation handler ever observed a non-None in-flight future. Before editing, I’ll create a temporary, deterministic reproduction by monkeypatching _coalesced_run_refresh_cmd to cancel before registration; this exercises the exact caller-side state transition without modifying repository tests.

  5. Assistant

    The first reproduction attempt did not enter refresh because the synthetic ValueError did not match _should_try_refresh’s accepted text; this is a fixture issue, not the reported race. I’ll inspect that predicate and rerun with an accepted error message and refresh environment enabled.

  6. Assistant

    The tests use that message but also set the refresh-command environment variable; my script omitted it. I’ll add the environment variable and rerun to establish the pre-fix generation value.

  7. Assistant

    The reproduction confirms the bug (generation=1 with zero registered future/subprocess). I’ll use the issue’s immediate-reraise alternative: when the registry has no entry, cancellation necessarily preceded registration on this event loop, so there is nothing to drain and no successful work that can justify a generation bump. This preserves the registered-success and registered-failure paths unchanged and is simpler than carrying an observation flag into the success block.

  8. Assistant

    The same deterministic reproducer now reports generation=0, confirming the race is fixed. Next I’ll run the complete refresh command race suite and closely related lock-registry/public-shim tests to ensure registered cancellation, failure propagation, coalescing, and facade behavior remain intact.

  9. Assistant

    All 20 focused refresh/coalescing tests pass. I’ll now run static formatting/lint checks on the changed file and the broader non-e2e test suite; this will catch syntax/style issues and any indirect auth regressions beyond the focused concurrency coverage.

  10. Assistant

    Static checks pass, and the broad run completed 5,344 passing tests; its only failures/errors are unrelated browser-login tests because the optional playwright module is absent from the environment. Since the affected suites already pass, I won’t alter dependencies or repository files for that environment issue. I’ll do a final diff/status review and run the full targeted race file once more without filtering, then prepare the required checklist assessment.

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