Repository navigation
Description
How do you use Sentry?
Sentry Saas (sentry.io)
Version
2.53.0
Steps to Reproduce
Detailed minimal repro project here:
https://github.com/brettdh/sentry-aiohttp-deadlock-repro/
Note: this is a very specific example of a more general issue: if an error occurs in a context where a lock is held, Sentry's inspection and serialization of the stack variables can cause a deadlock when inspecting a variable causes another attempt to acquire a lock that's already held.
In this specific case, these details fill in the general pattern above:
- the context is an aiohttp client session's
__del__via garbage collection- This error also occurs with other aiohttp
__del__methods - but it could have been a different chain of events that led to the inspection of an object on the stack that implicitly tries to grab a lock that's already held by the same thread
- This error also occurs with other aiohttp
- the lock is held in the Opentelemetry SDK, on an internal variable used when starting or ending a span
- because the span starts in Sentry's Tornado instrumentation, which itself relies on the Opentelemetry SDK
- but it could have been anything in any code, first- or third-party
- the lock is being held by the thread that is attempting to re-acquire it
- so in this case, making it a re-entrant lock avoids the deadlock
- but in another case, it could be held by another thread, and acquiring it during garbage collection (while other threads are stopped) could still lead to a deadlock
- for example, the garbage-collection could have been triggered on the main thread while a worker thread was holding the lock, and the deadlock would still occur, since the worker thread cannot wake up and release the lock
- in fact, we think we have seen this happen in production as well (but not 100% sure)
- edit: it seems like this might not be the case after all, looking at the PEP 703 description of stop-the-world garbage collection in Python 3.12 and 3.13
and these workarounds are available:
- Fix all the "Unclosed connector" issues in our application that cause the logging during
__del__- but this isn't always straightforward, and it may regress
- Ignore the
aiohttplogger in Sentry config (or disable all logging integration)- but then we don't get notified in Sentry about the above warnings or others
- and actually, this was a red herring and doesn't fix the problem
- The
__del__method calls the loop's exception handler, which is what logs aterrorlevel and gets captured by Sentry - we'd have to ignore the
asynciologger instead - but we couldn't know this without digging in to the code - what other risky code paths do we not know about yet?
- Disable stack variable capture altogether
- obvious downsides; local variable capture is extremely valuable for debugging
- Something else that I haven't thought of yet
As the repro project shows, this particular issue can be resolved by changing the Lock to a RLock in the otel SDK. That may be a good change to make, or it may have unacceptable side effects; I'm not sure yet. The reason I am reporting this as a Sentry issue is that enabling Sentry in our application has had the surprising effect of exposing us to the risk of deadlock (which we have seen happen reliably in production), and we've had to disable Sentry in our Python contexts for the time being.
I'd like to start a conversation about what changes in the SDK might be possible to reduce or eliminate this risk and similar risks caused by a large operation such as stack introspection in various contexts where locks could be held.
Expected Result
No deadlock triggered by Sentry, regardless of what code/stack Sentry is attempting to inspect
Actual Result
Deadlock triggered by Sentry (does not occur when Sentry is disabled)
Metadata
Metadata
Assignees
Projects
- StatusShow more project fieldsNo status
Activity
linear commentedon Mar 4, 2026
PY-2124
ericapisani commentedon Mar 4, 2026
Hi @brettdh ,
Thanks for reaching out and for the detailed repro. We'll take a look at this as soon as we can.
brettdh commentedon May 1, 2026
Checking in as it's been a bit - has there been any further investigation here?
sentrivana commentedon May 5, 2026
Heyo @brettdh 👋🏻 I don't fully understand how and to what degree OpenTelemetry is involved here. The repro project and also your description mention multiple times that the Sentry SDK uses OpenTelemetry internally:
None of this is true. The Sentry SDK doesn't use OpenTelemetry internally. If you use OpenTelemetry for tracing, then the Sentry SDK allows you to turn OpenTelemetry spans to Sentry spans and connect them to Sentry errors via a span processor. I assume that's what's meant here?
If that's the case, you might be interested in replacing the
SentrySpanProcessorwith a native OTLP-based approach that's now available: https://docs.sentry.io/platforms/python/integrations/otlp/ We'll be retiring the span processor way of doing things at some point, so OTLP is the preferred way to go -- it's also much less hacky than the legacy span processor way.The issue you're describing is still worth looking into for us since it seems like it might be a more general issue beyond just OTel, but I would recommend switching to OTLP either way.
brettdh commentedon May 5, 2026
I'll have to look again when I'm back at my computer, but I think the crux for our code was that we are already using otel separately from sentry, not that we were trying to connect the two (though that's something I'll look into), and I had seen otel in the stack traces. I do recall now that the repro project also involves enabling otel separately from sentry, so that part of the description may have been tied to my initial misunderstanding.
I do recall looking over the OTLP integration briefly and concluding that it probably wouldn't avoid the deadlock. Is that also your expectation?
sentrivana commentedon May 12, 2026
Sorry for the wait, had another look at this. I don't think it really matters whether you're using the Sentry
SpanProcessor, like in the repro example, or the OTLP integration. This really just comes down to an interaction between third-party code (OTel'sBoundedListimplementation, but could be anything on the current frame that locks) and the Sentry logging integration.The workarounds mentioned in your original messages are all valid:
event_level=None)...as well as the suggestion to use a re-entrant lock in OTel. Maybe worth proposing that to the OTel folks. But obviously that would just fix this one specific issue, when we're vulnerable to a whole class of these.
For some background from the SDK side of things, logging during GC is generally a problem. Would be good if we could completely disable log capture during GC cycles. We were facing a similar issue in our logs implementation where during log capture, another log was emitted during GC, leading to a similar deadlock, only that time it was all internal code.
I'll take a stab at this and see what comes out.
fix(serializer): Don't call `__iter__` on arbitrary sequences (#6304)
sentrivana commentedon May 21, 2026
Went down a different route ultimately.
While double capture during GC keeps popping up as a problem, and fixing that would also be a band aid for this issue specifically, it's not the underlying issue. The underlying issue is that we're trying to call random methods (in this case
__iter__) on user objects we know nothing about in order to serialize them.I've narrowed the set of types the serializer will attempt to iterate down to a bunch of built-in types like
listandset. The rest (includedBoundedList) will now just be__repr__'d instead.The fix will be out in the next release, likely next week.
fix(serializer): Don't call `__iter__` on arbitrary sequences (getsen…