fix issue leading to slowdown/unbounded memory growth in binary json processing#1199
Open
tomqext wants to merge 2 commits intoRobotWebTools:ros1from
Conversation
tomqext
added a commit
to Extend-Robotics/rosbridge_suite
that referenced
this pull request
Mar 25, 2026
Member
|
Is this only related to the ros1 branch? |
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.
Public API Changes
None
Description
When a WebSocket client sends JSON as binary frames (ros-sharp does this),
onMessagein the autobahn handler only decodes text frames - binary frames get pushed to theIncomingQueueas rawbytes. InProtocol.incoming(), the buffer concatenation doesself.buffer = self.buffer + str(message_string), which in Python 3 produces the repr rather than decoding:This isn't valid JSON (leading
b'), sojson.loads()fails on every message and falls through to the bracket-scanning fallback (designed for reassembling partial TCP fragments). That fallback iterates over every{/}pair tryingjson.loads()on each substring - O(n²) in the number of brackets. It does eventually find the JSON inside the garbage, so everything worked, just very slowly.In our case we were publishing 52-transform messages at 50Hz from ros-sharp (~322 bracket pairs per message). The fallback took ~48ms per message vs ~2.6ms for the actual deserialization + publish. The consumer thread couldn't keep up, so the
IncomingQueue(unboundeddeque) grew without bound - memory climbed indefinitely and data fell further and further behind real-time as the session progressed.The fix decodes bytes before buffering instead of calling
str(). The bracket-scanning fallback is still there for TCP fragmentation, it just stops being the hot path for every binary WebSocket message.Most rosbridge clients (roslibjs, roslibpy, C++ clients) send text frames so they never hit this - the
str()call is a no-op on an already-decoded string. ros-sharp is the main client that sends binary frames which is why this went undetected