frankie567 / httpx-ws

WebSocket support for HTTPX

Home Page:https://frankie567.github.io/httpx-ws/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Memory leak on multiple async connections.

T-256 opened this issue · comments

commented

Describe the bug

A clear and concise description of what the bug is.

To Reproduce

import asyncio

async def leak():
    async with httpx.AsyncClient(timeout=21) as client:
        async with httpx_ws.aconnect_ws(
            f"https://socketsbay.com/wss/v2/1/demo/",
            client,
        ) as ws_client:
            pass

input("before")
asyncio.run(asyncio.gather(*[leak() for _ in range(100)]))
input("after")

Expected behavior

No memory leak

Configuration

  • Python version: 3.7 (32 bit) on win 11 (64 bit)
  • httpx-ws version: 0.4.1

Additional context

In my run of above code memory bump from 10MB to 80MB

commented

by avoid using concurrent, there is much smaller memory leak (from 10MB to 18MB in my test):

import asyncio

async def leak():
    async with httpx.AsyncClient(timeout=21) as client:
        async with httpx_ws.aconnect_ws(
            f"https://socketsbay.com/wss/v2/1/demo/",
            client,
        ) as ws_client:
            pass

async def main():
    for _ in range(100):
        await leak()

input("before")
asyncio.run(main())
input("after")
commented

I got same result by replacing httpx_ws.aconnect_ws with client.stream.
perhaps it's httpx issue, I'll reopen there.