Skip to content

Sync WebSocket wrapper cannot receive BSON export frames, and send() crashes on bytes payloads #710

Description

@sneg55

Sync modeling WebSocket cannot receive BSON export frames, and send() crashes on commands containing bytes

Context: API Makeathon participant. Found while reviewing the sync modeling websocket wrapper.

1. Cannot receive BSON export frames

The sync ModelingCommandsWs.__iter__ and .recv() pass every received frame to WebSocketResponse.model_validate_json (kittycad/__init__.py:16905). But exports arrive as binary/BSON, not text/JSON: RawFile (kittycad/models/raw_file.py) documents this, and the wrapper supports sending BSON but has no BSON receive branch.

A modeling export therefore succeeds on the server, but when the export frame arrives the client raises a JSON/Pydantic decoding error, so the exported files are inaccessible through the wrapper.

2. send() crashes on byte payloads

send() does self.ws.send(json.dumps(data.model_dump(exclude_none=True))) (around __init__.py:16915-16918). ImportFile.data is bytes (kittycad/models/import_file.py), and json.dumps raises TypeError: Object of type bytes is not JSON serializable, so sending an import_files command crashes with an unrelated-looking error instead of base64-encoding (as model_dump_json would) or directing the user to send_binary().

Concrete failure

Exporting geometry over the sync websocket raises a decode error when the result frame arrives; sending an import_files command raises a bytes-serialization TypeError.

Verify

BSON receive: json.loads(b"\x05\x00\x00\x00\x00") (the canonical empty BSON document) is rejected as JSON. Send: build an import_files command with an ImportFile whose data is bytes and call send(): it raises the TypeError.

Suggested fix

Branch on frame type in __iter__/recv() and decode binary frames as BSON, and have send() use model_dump_json (which base64-encodes bytes) or route byte-bearing commands through send_binary().

Environment

Reviewed against the current main of KittyCAD/kittycad.py.

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