fix: provide init ctx to eventHandler#1133
Conversation
4ea80e4 to
478a19a
Compare
| ctx context.Context | ||
| option Option |
There was a problem hiding this comment.
Bug: The ctx field is not propagated in eventHandler.WithAttrs() and eventHandler.WithGroup(), leading to loss of context in derived handlers.
Severity: CRITICAL | Confidence: 1.00
🔍 Detailed Analysis
The ctx field, newly added to the eventHandler struct, is not propagated when new handlers are created via WithAttrs() or WithGroup(). When these methods are called, the derived eventHandler instances will have ctx initialized to its zero value (nil). Consequently, calls to sentry.GetHubFromContext(h.ctx) within the Handle() method will receive a nil context, causing the system to incorrectly fall back to sentry.CurrentHub() instead of using the intended context from the original handler. This defeats the purpose of the context propagation fix introduced by this PR.
💡 Suggested Fix
Ensure the ctx field is explicitly copied from the original handler to the new eventHandler instance within both the WithAttrs() and WithGroup() methods. For example, add ctx: h.ctx, to the return struct literal in both methods.
🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: slog/sentryslog.go#L168-L169
Potential issue: The `ctx` field, newly added to the `eventHandler` struct, is not
propagated when new handlers are created via `WithAttrs()` or `WithGroup()`. When these
methods are called, the derived `eventHandler` instances will have `ctx` initialized to
its zero value (`nil`). Consequently, calls to `sentry.GetHubFromContext(h.ctx)` within
the `Handle()` method will receive a `nil` context, causing the system to incorrectly
fall back to `sentry.CurrentHub()` instead of using the intended context from the
original handler. This defeats the purpose of the context propagation fix introduced by
this PR.
Did we get this right? 👍 / 👎 to inform future reviews.
Reference_id: 2648222
There was a problem hiding this comment.
Bug: Cloned Handlers Lose Context
The WithAttrs method creates a new eventHandler but doesn't copy the ctx field from the original handler. This breaks context propagation when the handler is cloned, causing h.ctx to be nil in subsequent Handle calls and defeating the purpose of this fix.
slog/sentryslog.go#L198-L209
Lines 198 to 209 in 478a19a
Bug: Cloned Handlers Lose Context
The WithGroup method creates a new eventHandler but doesn't copy the ctx field from the original handler. This breaks context propagation when the handler is cloned, causing h.ctx to be nil in subsequent Handle calls and defeating the purpose of this fix.
slog/sentryslog.go#L210-L223
Lines 210 to 223 in 478a19a
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1133 +/- ##
==========================================
- Coverage 86.97% 86.94% -0.03%
==========================================
Files 60 61 +1
Lines 5557 5563 +6
==========================================
+ Hits 4833 4837 +4
- Misses 539 541 +2
Partials 185 185 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Description
Following up to #1118, we also need to fix the context propagation for the slog eventHandler, in cases that slog is used without context.
Issues