Skip to content

aiohttp - incorrect handling of connection pooling with web sockets. #1018

@FuNK3Y

Description

@FuNK3Y

Reading the official aiohttp documentation: https://docs.aiohttp.org/en/stable/client_reference.html

I optimized my code to have a single ClientSession within my app to take advantage of connection pooling

class My_class:
    _clientSession= aiohttp.ClientSession()

    async def my_method():
        async with self._clientSession.ws_connect(uri) as ws:
            await ws.receive_json()
            ...
            await ws.send_json(stuff)

my_method is invoked twice - to different web sockets, over TLS simultaneously.

I get randomly served with a Uncaught exception in callback: (-28928, 'MBEDTLS_ERR_SSL_BAD_INPUT_DATA').

Creating the ClientSession within my_method completely resolves the issue - but at the expense of connection pooling. I strongly suspect a racing condition tied to the pooling.

This is happening on a pico 2 w running 1.25.0

Activity

Carglglz

Carglglz commented on Jun 4, 2025

@Carglglz
Contributor

Can you test this without TLS?, Can you test unix port?

As a workaround asyncio.Lock may help https://docs.micropython.org/en/latest/library/asyncio.html#class-lock,

I get randomly served with a Uncaught exception in callback: (-28928, 'MBEDTLS_ERR_SSL_BAD_INPUT_DATA').

Concurrency and TLS is tricky, if you want to debug this for mbedTLS add at mbedtls_config_port.h

 #define MBEDTLS_DEBUG_C  (1)
FuNK3Y

FuNK3Y commented on Jun 11, 2025

@FuNK3Y
Author

Thank you for your answer.

As a workaround, I create a ClientSession per remote endpoint and it works well enough.

I sadly can't test without TLS. This bug is out of my depth to troubleshoot - I raised an issue to give a chance to others to correlate their observations with mine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @FuNK3Y@Carglglz

        Issue actions

          aiohttp - incorrect handling of connection pooling with web sockets. · Issue #1018 · micropython/micropython-lib