Skip to content

Commit b6d3c31

Browse files
committed
fix: memory leak in OTEL traces
- address AI review
1 parent e9a99c5 commit b6d3c31

1 file changed

Lines changed: 34 additions & 8 deletions

File tree

langfuse/_task_manager/media_manager.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,45 @@ def _process_data_recursively(data: Any, level: int) -> Any:
166166
return copied
167167

168168
if isinstance(data, list):
169-
return [_process_data_recursively(item, level + 1) for item in data]
169+
if id(data) in seen:
170+
return data
171+
172+
seen.add(id(data))
173+
174+
try:
175+
return [_process_data_recursively(item, level + 1) for item in data]
176+
finally:
177+
seen.discard(id(data))
170178

171179
if isinstance(data, dict):
172-
return {
173-
key: _process_data_recursively(value, level + 1)
174-
for key, value in data.items()
175-
}
180+
if id(data) in seen:
181+
return data
182+
183+
seen.add(id(data))
176184

177-
if hasattr(data, "model_dump") and callable(data.model_dump):
185+
try:
186+
return {
187+
key: _process_data_recursively(value, level + 1)
188+
for key, value in data.items()
189+
}
190+
finally:
191+
seen.discard(id(data))
192+
193+
if (
194+
hasattr(data, "__pydantic_fields__")
195+
and hasattr(data, "model_dump")
196+
and callable(data.model_dump)
197+
):
178198
# Pydantic v2 BaseModel
179199
if id(data) in seen:
180200
return data
181201

182202
seen.add(id(data))
183203

184-
return _process_data_recursively(data.model_dump(), level + 1)
204+
try:
205+
return _process_data_recursively(data.model_dump(), level + 1)
206+
finally:
207+
seen.discard(id(data))
185208

186209
if hasattr(data, "dict") and callable(data.dict) and hasattr(data, "__fields__"):
187210
# Pydantic v1 BaseModel
@@ -190,7 +213,10 @@ def _process_data_recursively(data: Any, level: int) -> Any:
190213

191214
seen.add(id(data))
192215

193-
return _process_data_recursively(data.dict(), level + 1)
216+
try:
217+
return _process_data_recursively(data.dict(), level + 1)
218+
finally:
219+
seen.discard(id(data))
194220

195221
return data
196222

0 commit comments

Comments
 (0)