open-telemetry / open-telemetry/opentelemetry-python-contrib

Possible memory leak in aiohttp-client instrumentation

Open
#4,991 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
1.1k
Forks
1.1k
Avg merge
4d 15h
Merged PRs (30d)
16

Description

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 https://github.com/open-telemetry/opentelemetry-python-contrib/blob/466ae4c00de77d1f54f77a9600f1d08eba2306b5/instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py#L395 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/init.py around line 395, then reproduce the reported growth with the provided aiohttp and tracemalloc example. Compare snapshots across repeated sessions and trace the created meters; done means confirming the retention cause and defining a verified fix or documenting why the objects remain allocated.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
observability-sre
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.