Skip to content

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

@juliabell

Description

@juliabell

Description

WsClient.on_message_async crashes with AttributeError: 'str' object has no attribute 'get' when the WebSocket server sends a non-object JSON message (e.g. a string heartbeat).

Root Cause

The sync on_message method correctly handles raw string messages:

def on_message(self, ws, message):
    if isinstance(message, str):
        message = json.loads(message)
    message_type = message.get("type")

But the async on_message_async skips the type check after parsing:

async def on_message_async(self, ws, message):
    message = json.loads(message)
    message_type = message.get("type")  # crashes if json.loads returns a str

When the server sends a JSON string value (e.g. "heartbeat"), json.loads('"heartbeat"') returns "heartbeat" (a str), and str has no .get() method.

Impact

This causes run_async() to raise an unhandled AttributeError, disconnecting the WebSocket client. In a reconnect loop, this results in a permanent crash cycle with no useful work being done.

Suggested Fix

Add an isinstance check after json.loads, matching the pattern in on_message:

async def on_message_async(self, ws, message):
    if isinstance(message, str):
        message = json.loads(message)
    if not isinstance(message, dict):
        return
    message_type = message.get("type")
    ...

Environment

  • lighter-sdk 1.0.3
  • Python 3.12
  • websockets (legacy async client)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions