kimjinmyeong / webchat-app

a multi-user single-room real-time web chat application built with aiohttp and asyncio
1 stars 0 forks source link

all clients WebSocket connections becomes unresponsive when a first client leaves #10

Open kimjinmyeong opened 1 year ago

kimjinmyeong commented 1 year ago

Description

a bug where all WebSocket connections are becoming unresponsive when a client leaves the chat room. It seems that there is an issue with the asynchronous handling of the connections.

Steps to Reproduce:

Start the server and establish multiple WebSocket connections. Have one of the clients leave the chat room or close their connection. Observe that all the remaining WebSocket connections are also disconnected.

Expected Behavior:

When a client leaves the chat room or closes their WebSocket connection, only that specific client's connection should be disconnected, not all the other connections.

Actual Behavior:

All WebSocket connections are getting unresponsive when a client leaves the chat room, which is not the expected behavior.

kimjinmyeong commented 1 year ago

I have temporarily fixed it, but it is not a fundamental solution.

RuntimeError: readuntil() called while another coroutine is already waiting for incoming data I added this RuntimeException

async def listen_to_redis(redis_pubsub):

    while True:
        try:
            message = await redis_pubsub.get_message()
        except RuntimeError:
            # RuntimeError: readuntil() called while another coroutine is already waiting for incoming data
            # Even if above error occurs, the server continues to function.
            pass
        finally:
            if message:
                if message["type"] == "message":
                    await send_messages(message["data"].decode())
            await asyncio.sleep(0.01)