Skip to content

Closed Behaviour Change 14.0 #1538

@cl-dqh

Description

@cl-dqh

One of our tests started failing as of 14.0 - if the server sends a message then closes the websocket before the client has recv() the message, then the client will not see the message before the ConnectionClosed event.

This test passes in 13.1, but fails in 14.0:

import asyncio
import websockets

server_done_event   = None
client_ws           = None

async def server_handler(ws):
    # Send a message, then immediately close the connection
    await ws.send("Hello, Client!")
    await ws.close()
    # Signal that the server has completed its task
    server_done_event.set()

async def server_start():
    # Start the WebSocket server
    server = await websockets.serve(server_handler, "localhost", 8765)
    print("Server started on ws://localhost:8765")
    return server

async def client_connect():
    global client_ws
    # Connect to the WebSocket server and handle messages
    client_ws = await websockets.connect("ws://localhost:8765")

async def client_recv():
    recieved_message = False
    while True:
        try:
            # Then attempt to receive any remaining message frames
            message = await client_ws.recv()
            print("Client received message:", message)
            recieved_message = True
        except websockets.ConnectionClosed:
            if not recieved_message:
                print("FAIL: Client did not recieve any messages before the connection was closed.")
            else:
                print("SUCCESS: Client received message.")
            break

async def main():
    global server_done_event

    # Create an event to signal when the server is done
    server_done_event   = asyncio.Event()
    server              = await server_start()

    # Wait for the client to connect, and for the server to respond to it
    await asyncio.gather(
        asyncio.create_task(client_connect()),
        server_done_event.wait()
    )

    # Now attempt to recieve the message from the server
    await client_recv()

# Run the script
asyncio.run(main())

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions