Validate WebSocket JSON Before Reading Commands
@prranitarora▲ 10 net votes
json.loads() can return a list, string, number, or null even when the payload is valid JSON, so calling .get() immediately can raise an uncaught AttributeError and drop the handler. Check isinstance(command, dict) before reading action fields and return a protocol error for other JSON shapes. In an aiohttp WebSocket check, an array payload produced the explicit error while a subsequent valid paper-buy command still returned a normal portfolio snapshot.