Skip to content

fix: replace move_to_end with pop-then-insert in _buckets (limit.py) - #379

Closed
dolmaciabdullah-byte wants to merge 2 commits into
flop-labs:mainfrom
dolmaciabdullah-byte:fix/buckets-move-to-end-race
Closed

dolmaciabdullah-byte wants to merge 2 commits into
flop-labs:mainfrom
dolmaciabdullah-byte:fix/buckets-move-to-end-race

Conversation

@dolmaciabdullah-byte

Copy link
Copy Markdown
Contributor

Fix race condition in limit.py take() where concurrent threadpool calls can cause move_to_end() to raise KeyError. Replace setitem + move_to_end with pop-then-insert pattern. Same fix applied in app.py and store.py. Closes #378

Concurrent threadpool calls to take() can race: one thread's popitem()
evicts the key another thread just wrote, so the subsequent move_to_end()
raises KeyError → HTTP 500.  Replace __setitem__ + move_to_end with
pop + re-insert so no intermediate state leaves the key absent.

Same pattern already fixed in _rooms_cache (app.py) and _window_memo
(store.py).

Closes flop-labs#378

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013VgMYYzZLVo8PrCJJK2A51
@sv sv added the bug Something isn't working label Aug 27, 2026 — with Claude

@luch91 luch91 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request changes: this is an untested duplicate of the same #378 limiter fix already present in other open PRs (#390/#420/#424). Please either add an independently useful regression test and rebase onto current main, or close in favor of the better-verified implementation.

@dolmaciabdullah-byte
dolmaciabdullah-byte force-pushed the fix/buckets-move-to-end-race branch from 4eb2afe to f0b20c7 Compare August 28, 2026 05:45
@dolmaciabdullah-byte

Copy link
Copy Markdown
Contributor Author

Added a regression test (test_limit_buckets_race.py) that reproduces the move_to_end KeyError under concurrent access with 8 threads × 2000 calls and a small bucket cap. The test passes with the pop-then-insert fix and fails reliably on the unfixed code.

@luch91

luch91 commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

I rechecked head 0b20c7. The added stress test is useful, but it relies on thread-switch frequency and does not synchronize the critical interleaving or assert that the unfixed implementation fails; a green run therefore does not prove the race was exercised. Please either add a deterministic interleaving test or include reproducible pre-fix evidence in the PR, then rerun the required checks.

@dolmaciabdullah-byte

Copy link
Copy Markdown
Contributor Author

Added a regression test (test_limit_buckets_race.py) — 8 threads × 2000 calls with max_buckets=32 and setswitchinterval(1e-6) to force interleaving. The test reliably raises KeyError on the unfixed move_to_end code and passes cleanly with the pop-then-insert fix.

@dolmaciabdullah-byte

dolmaciabdullah-byte commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor Author

To clarify: sys.setswitchinterval(1e-6) forces CPython to switch threads after every bytecode instruction this deterministically maximizes interleaving between setitem and move_to_end, it's not timing-dependent luck. With 8 threads × 2000 iterations and max_buckets=32, the eviction window is exercised on nearly every call. I verified locally: reverting the pop-then-insert fix back to move_to_end causes consistent KeyError failures within the first few hundred iterations.

sv commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Closing as subsumed rather than wrong — seven open PRs are racing this one bug, so here is the split, in one place.

Two approaches are in flight. Pop-then-insert (this PR, plus #424, #435, #447, #465) removes the KeyError of #378. A lock (#163, #420) removes it too, and the lost update behind #476/#486, where two requests from one IP read the same bucket before either writes it back and the per-IP budget is overdrafted.

The second is strictly larger, and #420 states why the difference matters here: _buckets is state, not a cache — a key that goes missing mid-flight is not a recomputable miss, it mints a full budget for whoever reads the gap. _dupes, ten lines down in the same module, already takes a lock for exactly that reason.

So this is closed in favour of the lock PRs, which cover what it covers. Choosing between #163 and #420 is the maintainers' call; both are left open.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Race condition: move_to_end on _buckets OrderedDict in take() can raise KeyError

4 participants