frankie567 / httpx-ws

WebSocket support for HTTPX
https://frankie567.github.io/httpx-ws/
MIT License
102 stars 12 forks source link

Memory leak on multiple async connections. #42

Closed T-256 closed 1 year ago

T-256 commented 1 year ago

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

Additional context

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

T-256 commented 1 year ago

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")
T-256 commented 1 year ago

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