Skip to content

fix: handle non-object JSON messages in on_message_async#1

Open
AmSach wants to merge 1 commit into
mainfrom
fix/on_message_async-non-object-json
Open

fix: handle non-object JSON messages in on_message_async#1
AmSach wants to merge 1 commit into
mainfrom
fix/on_message_async-non-object-json

Conversation

@AmSach
Copy link
Copy Markdown
Owner

@AmSach AmSach commented May 20, 2026

Fixed the bug described in issue elliottech#123. Here's what was wrong and how I fixed it:

Problem: The on_message_async method crashes with AttributeError: 'str' object has no attribute 'get' when the WebSocket server sends a non-object JSON message (e.g., a string heartbeat). This is because json.loads('"heartbeat"') returns a Python str, and calling .get() on a string raises an AttributeError.

Root Cause: Unlike the sync on_message method, on_message_async skipped the isinstance check after parsing. It directly called message.get('type') assuming the parsed result was always a dict.

Fix: Added the same isinstance(message, dict) check that exists in on_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:

  • Raw string heartbeat: returns early (non-dict)
  • JSON string '"heartbeat"': parses to Python str, returns early
  • Valid JSON object {'type': 'ping'}: processes correctly

Fixes: elliottech#123

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: on_message_async crashes on non-object JSON messages (e.g. heartbeats)

1 participant