fix(pydantic-ai): Use first-class hooks when available #5947
3 issues
find-bugs: Found 3 issues (3 medium)
Medium
Span entered but never exited when ctx.metadata is not a dict - `sentry_sdk/integrations/pydantic_ai/__init__.py:91`
In on_request, span.__enter__() is called unconditionally at line 91, but the span is only stored in metadata if run_context_metadata is a dict (lines 88-89). When metadata is not a dict, on_response returns early at line 103-104 without calling span.__exit__(). This causes a span leak - the span will remain open indefinitely, potentially corrupting the span hierarchy for subsequent operations.
Also found at:
sentry_sdk/integrations/pydantic_ai/__init__.py:106
are_request_hooks_available incorrectly set to True when hooks are unavailable - `sentry_sdk/integrations/pydantic_ai/__init__.py:66`
At line 66, when ImportError is raised (meaning pydantic_ai.capabilities.Hooks is not available), the code sets PydanticAIIntegration.are_request_hooks_available = True. This is incorrect - it should be False since the hooks mechanism is not available. This causes the agent_run wrapper to incorrectly add metadata to kwargs when using the fallback patch approach.
Agent created before sentry_init() bypasses capability hooks injection - `tests/integrations/pydantic_ai/test_pydantic_ai.py:654`
In test_without_pii_tools, the test_agent is instantiated via get_test_agent() on line 654 before sentry_init() is called on line 661. The PydanticAI integration patches Agent.__init__ during setup_once() to inject capability hooks for request/response tracking. Since the agent is created before this patch is applied, it won't have the hooks injected, potentially causing incomplete instrumentation or test failures on newer pydantic-ai versions that use the Hooks capability.
Duration: 7m 32s · Tokens: 1.7M in / 27.3k out · Cost: $2.72 (+merge: $0.00, +fix_gate: $0.00)
Annotations
Check warning on line 91 in sentry_sdk/integrations/pydantic_ai/__init__.py
sentry-warden / warden: find-bugs
Span entered but never exited when ctx.metadata is not a dict
In `on_request`, `span.__enter__()` is called unconditionally at line 91, but the span is only stored in metadata if `run_context_metadata` is a dict (lines 88-89). When metadata is not a dict, `on_response` returns early at line 103-104 without calling `span.__exit__()`. This causes a span leak - the span will remain open indefinitely, potentially corrupting the span hierarchy for subsequent operations.
Check warning on line 106 in sentry_sdk/integrations/pydantic_ai/__init__.py
sentry-warden / warden: find-bugs
[9QU-Q26] Span entered but never exited when ctx.metadata is not a dict (additional location)
In `on_request`, `span.__enter__()` is called unconditionally at line 91, but the span is only stored in metadata if `run_context_metadata` is a dict (lines 88-89). When metadata is not a dict, `on_response` returns early at line 103-104 without calling `span.__exit__()`. This causes a span leak - the span will remain open indefinitely, potentially corrupting the span hierarchy for subsequent operations.
Check warning on line 66 in sentry_sdk/integrations/pydantic_ai/__init__.py
sentry-warden / warden: find-bugs
are_request_hooks_available incorrectly set to True when hooks are unavailable
At line 66, when `ImportError` is raised (meaning `pydantic_ai.capabilities.Hooks` is not available), the code sets `PydanticAIIntegration.are_request_hooks_available = True`. This is incorrect - it should be `False` since the hooks mechanism is not available. This causes the agent_run wrapper to incorrectly add `metadata` to kwargs when using the fallback patch approach.
Check warning on line 654 in tests/integrations/pydantic_ai/test_pydantic_ai.py
sentry-warden / warden: find-bugs
Agent created before sentry_init() bypasses capability hooks injection
In `test_without_pii_tools`, the `test_agent` is instantiated via `get_test_agent()` on line 654 **before** `sentry_init()` is called on line 661. The PydanticAI integration patches `Agent.__init__` during `setup_once()` to inject capability hooks for request/response tracking. Since the agent is created before this patch is applied, it won't have the hooks injected, potentially causing incomplete instrumentation or test failures on newer pydantic-ai versions that use the Hooks capability.