fix(pydantic-ai): Use first-class hooks when available #5947
2 issues
find-bugs: Found 2 issues (1 medium, 1 low)
Medium
Span leaked when ctx.metadata is not a dict in on_request hook - `sentry_sdk/integrations/pydantic_ai/__init__.py:50`
In the on_request hook, span.__enter__() is called unconditionally (line 50), but the span is only stored in metadata if run_context_metadata is a dict (lines 47-48). When metadata is not a dict (e.g., None or another type), the span is entered but never stored, so neither on_response nor on_error can find it to call span.__exit__(). This causes a resource leak where spans remain open indefinitely, corrupting Sentry's trace tree for the affected transaction.
Low
Debug print statement left in test code - `tests/integrations/pydantic_ai/test_pydantic_ai.py:227`
A print("events", len(events)) statement at line 227 appears to be leftover debug code. While not a functional bug, it adds noise to test output and suggests incomplete cleanup before the PR.
Duration: 9m 15s · Tokens: 2.0M in / 26.0k out · Cost: $3.05 (+merge: $0.00, +fix_gate: $0.00)
Annotations
Check warning on line 50 in sentry_sdk/integrations/pydantic_ai/__init__.py
sentry-warden / warden: find-bugs
Span leaked when ctx.metadata is not a dict in on_request hook
In the `on_request` hook, `span.__enter__()` is called unconditionally (line 50), but the span is only stored in metadata if `run_context_metadata` is a dict (lines 47-48). When metadata is not a dict (e.g., None or another type), the span is entered but never stored, so neither `on_response` nor `on_error` can find it to call `span.__exit__()`. This causes a resource leak where spans remain open indefinitely, corrupting Sentry's trace tree for the affected transaction.