Skip to content

What happens when a WebSocket Server is publishing faster than a Client? #1823

@vrde

Description

@vrde

Long story short

What happens if the client is not consuming the events coming from the server? Can a malicious client jam the server?

Those two links might be relevant to understand the problem:

I dug in the code, the closest thing I've found about handling buffers is the drain method.

Side note: I'm not familiar with the aiohttp code base, but I noticed that drain is a coroutine that is eventually called by send_str, but send_str is not a coroutine, is it a problem?

Expected behaviour

The server should stop writing to that specific WebSocket, or overwrite the buffer with the new data.

Actual behaviour

I've no idea.

Steps to reproduce

Client code

After connecting to the server, the client starts twiddling its thumbs.

# Client

import asyncio
import aiohttp


async def run(session):
    async with session.ws_connect('http://localhost:8080/') as ws:
        while True:
            await asyncio.sleep(1000)

session = aiohttp.ClientSession()
loop = asyncio.get_event_loop()
loop.run_until_complete(run(session))

Server code

On the other side, the server works hard to deliver it's extremely important kilobyte of as

# Server

import asyncio
from aiohttp import web


async def websocket_handler(request):
    ws = web.WebSocketResponse()
    await ws.prepare(request)

    s = 'a' * 1024

    i = 0
    while True:
        i += 1
        print('send ', len(s) * i)
        ws.send_str(s)
        await asyncio.sleep(0.1)

    return ws


app = web.Application()
app.router.add_get('/', websocket_handler)
web.run_app(app)

Your environment

Python 3.6+aiohttp-2.0.7 on a Ubuntu machine.

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