fix: handle non-object JSON messages in on_message_async#1
Open
AmSach wants to merge 1 commit into
Open
Conversation
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.
Fixed the bug described in issue elliottech#123. Here's what was wrong and how I fixed it:
Problem: The
on_message_asyncmethod crashes withAttributeError: 'str' object has no attribute 'get'when the WebSocket server sends a non-object JSON message (e.g., a string heartbeat). This is becausejson.loads('"heartbeat"')returns a Pythonstr, and calling.get()on a string raises anAttributeError.Root Cause: Unlike the sync
on_messagemethod,on_message_asyncskipped theisinstancecheck after parsing. It directly calledmessage.get('type')assuming the parsed result was always a dict.Fix: Added the same
isinstance(message, dict)check that exists inon_message_sync. After parsing, if the result is not a dict, the method returns early — allowing the client to gracefully handle heartbeat messages without crashing.Tested by: Verified the fix handles:
heartbeat: returns early (non-dict)'"heartbeat"': parses to Pythonstr, returns early{'type': 'ping'}: processes correctlyFixes: elliottech#123