Skip to content

Possible memory leak in aiohttp-client instrumentation #4991

Description

@larnaldo-dev

Describe your environment

OS: Microsoft Windows 11 Enterprise, 10.0.26100 Build 26100
Python version: Python 3.13.13 (tags/v3.13.13:01104ce, Apr 7 2026, 19:25:48) [MSC v.1944 64 bit (AMD64)
Package version: opentelemetry-instrumentation-aiohttp-client 0.65b0

What happened?

Possible memory leak when instrumenting aiohttp client connections. Every new connection creates a meter at

and it is never cleaned up.

Steps to Reproduce

This setup is based on the sample code from https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/aiohttp_client/aiohttp_client.html.

import asyncio
import aiohttp
from opentelemetry.instrumentation.aiohttp_client import (
    AioHttpClientInstrumentor
)
import tracemalloc

# Enable instrumentation
AioHttpClientInstrumentor().instrument()


# Create a session and make an HTTP get request
async def get(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url, ssl=False) as response:
            await response.text()

async def main():
    # Enable tracemalloc
    tracemalloc.start()
    before = tracemalloc.take_snapshot()

    for i in range(1000):
        print(f"Request {i+1}")
        await get("<URL>")
        await asyncio.sleep(5)  # Add a small delay

    after = tracemalloc.take_snapshot()
    stats = after.compare_to(before, 'lineno')
    for stat in stats[:10]:
        print(stat)
    tracemalloc.stop()


if __name__ == "__main__":
    asyncio.run(main())

Expected Result

The expected result is no memory leaks.

Actual Result

This is the snapshot diff

\venv\Lib\site-packages\opentelemetry\metrics\_internal\__init__.py:163: size=143 KiB (+143 KiB), count=1001 (+1001), average=146 B
\venv\Lib\site-packages\opentelemetry\metrics\_internal\__init__.py:581: size=112 KiB (+112 KiB), count=1000 (+1000), average=115 B
\venv\Lib\site-packages\opentelemetry\instrumentation\_semconv.py:697: size=78.1 KiB (+78.1 KiB), count=1000 (+1000), average=80 B
\venv\Lib\site-packages\opentelemetry\metrics\_internal\__init__.py:199: size=62.5 KiB (+62.5 KiB), count=1000 (+1000), average=64 B
\venv\Lib\site-packages\opentelemetry\metrics\_internal\__init__.py:498: size=54.7 KiB (+54.7 KiB), count=1000 (+1000), average=56 B
\venv\Lib\site-packages\opentelemetry\metrics\_internal\__init__.py:200: size=54.7 KiB (+54.7 KiB), count=1000 (+1000), average=56 B
\venv\Lib\site-packages\opentelemetry\metrics\_internal\__init__.py:499: size=54.5 KiB (+54.5 KiB), count=997 (+997), average=56 B
\venv\Lib\site-packages\opentelemetry\metrics\_internal\__init__.py:584: size=31.2 KiB (+31.2 KiB), count=1000 (+1000), average=32 B

Additional context

No response

Would you like to implement a fix?

No

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Activity

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions