Skip to content

fix: wrapping context __exit__ leak [backport 2.21] #13635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.21
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ddtrace/internal/wrapping/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,14 @@

CONTEXT_RETURN.parse(
r"""
pop_block
load_const {context}
load_method $__return__
rot_three
rot_three
call_method 1
rot_two
pop_top
"""
)

Expand All @@ -257,11 +260,14 @@

CONTEXT_RETURN.parse(
r"""
pop_block
load_const {context}
load_method $__return__
rot_three
rot_three
call_method 1
rot_two
pop_top
"""
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
code origin: fixed a potential memory leak when collecting entry span
location information.
19 changes: 19 additions & 0 deletions tests/internal/test_wrapping.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
from contextlib import asynccontextmanager
import gc
import inspect
import sys
from types import CoroutineType
Expand Down Expand Up @@ -805,3 +806,21 @@ async def fibonacci(n):
await asyncio.gather(*[fibonacci(n) for n in range(1, N)])

assert set(values) == {(n, n) for n in range(0, N)}


def test_wrapping_context_method_leaks():
def foo():
return 42

wc = DummyWrappingContext(foo)
wc.wrap()

method_count = len([_ for _ in gc.get_objects() if type(_).__name__ == "method"])

for _ in range(10000):
foo()

gc.collect()

new_method_count = len([_ for _ in gc.get_objects() if type(_).__name__ == "method"])
assert new_method_count <= method_count + 1
Loading