fix: resolve lost-wakeup races in InMemoryTaskStore.wait_for_update#2536
Open
blackwell-systems wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: resolve lost-wakeup races in InMemoryTaskStore.wait_for_update#2536blackwell-systems wants to merge 1 commit intomodelcontextprotocol:mainfrom
blackwell-systems wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
Two races in wait_for_update: 1. Concurrent waiters: second caller overwrites the first's event in _update_events[task_id], so the first waiter hangs forever. Fix: use a list of events per task_id so each waiter gets its own. 2. Notify before wait: if update_task completes before wait_for_update is called, the signal is lost because no event exists yet. Fix: track pending updates in a set; wait_for_update checks and consumes pending flags before creating an event. Both races are reachable via task_result_handler.py:126 when multiple clients poll the same task or when a task completes between status checks. Adds two tests: concurrent waiters and notify-before-wait. Fixes modelcontextprotocol#2535
17b3ab2 to
065264d
Compare
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.
Summary
Two races in
InMemoryTaskStore.wait_for_updatethat can cause indefinite hangs when polling task status.Race 1: Concurrent waiters
wait_for_updateoverwrites_update_events[task_id]with a fresh event on every call. If two clients poll the same task, the second overwrites the first's event.notify_updatesets only the latest event, so the first waiter hangs forever.Fix: Use a list of events per task_id. Each waiter appends its own event.
notify_updatesets and removes all events for the task.Race 2: Notify before wait
If
update_taskcallsnotify_updatebefore anywait_for_updateis active, the signal is lost (no event exists to set). The nextwait_for_updatecreates a fresh event and waits for an update that already happened.Fix: Track pending updates in a
set.notify_updateadds to the set when no waiters exist.wait_for_updatechecks and consumes the pending flag before creating an event.Reproducer
Both races are reproducible with a 30-line script (included in issue #2535). Output before fix:
After fix:
Test plan
pytest tests/experimental/tasks/ -v)Fixes #2535