File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed
instrumentation/opentelemetry-instrumentation-fastapi
src/opentelemetry/instrumentation/fastapi Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -392,7 +392,11 @@ def _instrument(self, **kwargs):
392
392
fastapi .FastAPI = _InstrumentedFastAPI
393
393
394
394
def _uninstrument (self , ** kwargs ):
395
- for instance in _InstrumentedFastAPI ._instrumented_fastapi_apps :
395
+ # Create a copy of the set to avoid RuntimeError during iteration
396
+ instances_to_uninstrument = list (
397
+ _InstrumentedFastAPI ._instrumented_fastapi_apps
398
+ )
399
+ for instance in instances_to_uninstrument :
396
400
self .uninstrument_app (instance )
397
401
_InstrumentedFastAPI ._instrumented_fastapi_apps .clear ()
398
402
fastapi .FastAPI = self ._original_fastapi
Original file line number Diff line number Diff line change 19
19
20
20
from opentelemetry .instrumentation .fastapi import FastAPIInstrumentor
21
21
22
+ # Check if sys.getrefcount is available (not available in PyPy)
23
+ HAS_GETREFCOUNT = hasattr (sys , "getrefcount" )
24
+
22
25
23
26
class TestFastAPIMemoryLeak (unittest .TestCase ):
24
27
"""Test for memory leak in FastAPIInstrumentor.uninstrument_app()"""
25
28
26
29
def test_refcount_after_uninstrument (self ):
27
30
"""Test that refcount is restored after uninstrument_app()"""
31
+ if not HAS_GETREFCOUNT :
32
+ self .skipTest (
33
+ "sys.getrefcount not available in this Python implementation"
34
+ )
35
+
28
36
app = fastapi .FastAPI ()
29
37
30
38
# Instrument the app
@@ -53,6 +61,11 @@ def test_refcount_after_uninstrument(self):
53
61
54
62
def test_multiple_instrument_uninstrument_cycles (self ):
55
63
"""Test that multiple instrument/uninstrument cycles don't leak memory"""
64
+ if not HAS_GETREFCOUNT :
65
+ self .skipTest (
66
+ "sys.getrefcount not available in this Python implementation"
67
+ )
68
+
56
69
app = fastapi .FastAPI ()
57
70
58
71
initial_refcount = sys .getrefcount (app )
@@ -84,6 +97,11 @@ def test_multiple_instrument_uninstrument_cycles(self):
84
97
85
98
def test_multiple_apps_instrument_uninstrument (self ):
86
99
"""Test that multiple apps can be instrumented and uninstrumented without leaks"""
100
+ if not HAS_GETREFCOUNT :
101
+ self .skipTest (
102
+ "sys.getrefcount not available in this Python implementation"
103
+ )
104
+
87
105
apps = [fastapi .FastAPI () for _ in range (3 )]
88
106
initial_refcounts = [sys .getrefcount (app ) for app in apps ]
89
107
You can’t perform that action at this time.
0 commit comments