Skip to content

frame with invalid opcode from server hangs asyncio client #1651

@jmuhlich

Description

@jmuhlich

I have a misbehaving server (embedded device, nothing I can do about it) that, after a client sends a CLOSE frame, responds with a frame containing an invalid opcode (15). The python-websockets protocol parsing code does notice this and raises ProtocolError("invalid opcode"), but somehow the asyncio client silently swallows this. My script then just hangs rather than at least bubbling the exception up to the top level and dying. I'm not sure if the invalid opcode is a problem just in response to CLOSE, or if it would hang the client at any point.

Below is a minimal reproducer using the asyncio client, if you can somehow put together your own ws server script that sends an invalid opcode. Expected behavior is for this script to silently exit immediately. On my misbehaving server, it hangs indefinitely. It also takes two Ctrl-C presses to interrupt, if that means anything.

In addition to not cleanly exiting when the script is finished, any exceptions that occur within the async with block are never reported up. You can replace the pass in the script below with raise Exception and it still hangs indefinitely without showing a traceback.

import asyncio
from websockets.asyncio.client import connect

endpoint = ...

async def func():
    async with connect(endpoint) as websocket:
        pass

if __name__ == "__main__":
    asyncio.run(func())

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