Repository navigation
Send pub/sub messages on the server's event loop - #1349
Open
CraigBurden wants to merge 1 commit into
Open
CraigBurden wants to merge 1 commit into
CraigBurden wants to merge 1 commit into
Conversation
PubSub.publish() is called from the StationWatcher thread and from test output callbacks, not the tornado loop that owns the websocket connections. Called off-loop, write_message() still writes the frame (unsafely, from the wrong thread), but schedules its completion task on the calling thread's event loop, which never runs. Those tasks leak for the life of the process and are destroyed at exit, logging "Task was destroyed but it is pending!" for every message published. Capture the serving loop when the first client subscribes and hand off-loop publishes to it with call_soon_threadsafe.
This branch has not been deployed
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.
Send pub/sub messages on the server's event loop
Problem
PubSub.publish()is called from theStationWatcherthread and from test output callbacks (StationServer.publish_final_state), not from the tornado event loop that owns the websocket connections. sockjs-tornado's websocket transport callswrite_message()directly, so off-loop:IOStreamfrom the wrong thread, which isn't thread-safe, andwrite_message()schedules its completion task on the calling thread's event loop.StationWatchercreates that loop but never runs it, so every task stays pending for the life of the process.At shutdown every one of these tasks gets reported:
A station that runs for a shift with the GUI open logs thousands of these lines on exit.
Fix
PubSubrecords the serving loop when the first client subscribes (on_openalways runs on that loop).publish()sends directly when it's already on that loop, and otherwise hands the send to the loop withcall_soon_threadsafe. If nobody has subscribed, it does nothing, as before. If the loop has closed, the publish is dropped, because the server is shutting down._loopis stored per subclass, like_lockandsubscribers, soStationPubSubandDashboardPubSubdon't share it.Testing
New
test/output/servers/pub_sub_test.py: publishing from another thread, from the serving loop, with a client filter, with no subscribers, and after the loop has closed. Three of its cases fail without the fix.Full suite passes:
pytest test(588 passed).I also ran a script that starts a
StationServer, connects a websocket client and runs 5 tests that each log heavily and then prompt. The client answers each prompt over HTTP.🤖 Generated with Claude Code