shiv
shiv

Reputation: 1952

aiohttp says undisclosed client session even after awaiting on close

Consider following code fragment.

async def f():
    http_client_session = aiohttp.ClientSession()
    headers = {"developerkey": "somekey"}
    body = {
        "password": "somepassword",
        "username": "[email protected]",
    }

    url = "https://localhost/login"

    response_body = None
    async with http_client_session.post(url, json=body, headers=headers) as response:
        assert response.status == 200
        response_body = await response.json()

    await http_client_session.close()
    return response_body()

The function f is awaited in another function. aiohttp gives the warning 'Unclosed client session' but I do not understand this as I have already awaited for it to close the session.

Upvotes: 1

Views: 193

Answers (1)

PirateNinjas
PirateNinjas

Reputation: 2076

I've seen a similar issue previously in another project. Turns out it may be just that you need to allow some time for the session to fully close. For my project I added time.sleep(5) after the close statement to let the connection end.

See this ticket on aiohttp: https://github.com/aio-libs/aiohttp/issues/1925

Upvotes: 1

Related Questions