fix(dispatcher): atomic select-and-reserve in LeastLoadedDispatcher - #20
Merged
Conversation
The dispatcher read load(), picked the least-loaded subscriber, then called it -- a TOCTOU race where two concurrent fires could both land on the same capacity-1 node. Now it reserves the pick under a lock via Node.try_acquire (holding the slot for the call, releasing after), invoking the node's handler directly so it isn't double-counted. Plain load()-only subscribers keep the prior best-effort behaviour, so existing usage is unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
LeastLoadedDispatcherreadload(), picked the least-loaded subscriber, then called it — a TOCTOU race where two concurrentfire()s could both pick the same capacity-1 node (the same double-booking bug fixed in torchestrator's pools, but on the fire-and-forgetEventfulpath).Now selection reserves the pick atomically under a lock via
Node.try_acquire, holds the slot for the call, and releases after — invoking the node'shandlerdirectly so the reservation isn't double-counted byNode.__call__. Subscribers that only implementload()/__call__(not Nodes) keep the previous best-effort routing, so existing usage and tests are unchanged.New test: a capacity-1 Node under concurrent dispatch is reserved for the call (second dispatch sees it saturated), never double-runs, and releases after. Full suite 187 green; lint/format/mypy clean.