fix(pydantic-ai): Use first-class hooks when available #5947
1 issue
find-bugs: Found 1 issue (1 medium)
Medium
Span resource leak when ctx.metadata is not a dict - `sentry_sdk/integrations/pydantic_ai/__init__.py:50-52`
In on_request, the span is unconditionally entered via span.__enter__() at line 50, but the span is only stored in metadata if isinstance(run_context_metadata, dict) is True (lines 47-48). If metadata is not a dict (e.g., user passes a non-dict metadata value or pydantic-ai provides a different type), the span will be entered but never exited in on_response or on_error, causing a span resource leak. While the agent_run wrappers try to set metadata to a dict, they only do so if not metadata (falsy), so a truthy non-dict value would still trigger this bug.
Duration: 7m 51s · Tokens: 2.0M in / 25.5k out · Cost: $3.06 (+fix_gate: $0.00)
Annotations
Check warning on line 52 in sentry_sdk/integrations/pydantic_ai/__init__.py
sentry-warden / warden: find-bugs
Span resource leak when ctx.metadata is not a dict
In `on_request`, the span is unconditionally entered via `span.__enter__()` at line 50, but the span is only stored in metadata if `isinstance(run_context_metadata, dict)` is True (lines 47-48). If metadata is not a dict (e.g., user passes a non-dict metadata value or pydantic-ai provides a different type), the span will be entered but never exited in `on_response` or `on_error`, causing a span resource leak. While the agent_run wrappers try to set metadata to a dict, they only do so if `not metadata` (falsy), so a truthy non-dict value would still trigger this bug.