Skip to content

Deadlock during GC with Sentry + Opentelemetry + Tornado + aiohttp #5586

Closed

Description

@brettdh

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:

  1. 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
  2. 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
  3. 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:

  1. 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
  2. Ignore the aiohttp logger 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 at error level and gets captured by Sentry
    • we'd have to ignore the asyncio logger instead - but we couldn't know this without digging in to the code
    • what other risky code paths do we not know about yet?
  3. Disable stack variable capture altogether
    • obvious downsides; local variable capture is extremely valuable for debugging
  4. 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)

Activity

  1. linear commented on Mar 4, 2026

    @linear
  2. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Mar 4, 2026
  3. ericapisani commented on Mar 4, 2026

    @ericapisani
    Member

    Hi @brettdh ,

    Thanks for reaching out and for the detailed repro. We'll take a look at this as soon as we can.

  4. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Mar 4, 2026
  5. brettdh commented on May 1, 2026

    @brettdh
    Author

    Checking in as it's been a bit - has there been any further investigation here?

  6. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on May 1, 2026
  7. sentrivana commented on May 5, 2026

    @sentrivana
    Collaborator

    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:

    because the span starts in Sentry's Tornado instrumentation, which itself relies on the Opentelemetry SDK

    OpenTelemetry SDK (used internally by sentry-sdk 2.x as its tracing backend)

    opentelemetry-sdk / opentelemetry-api (>=1.20) - sentry-sdk uses these internally for tracing

    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 SentrySpanProcessor with 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.

  8. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on May 5, 2026
  9. brettdh commented on May 5, 2026

    @brettdh
    Author

    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?

  10. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on May 5, 2026
  11. sentrivana commented on May 12, 2026

    @sentrivana
    Collaborator

    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's BoundedList implementation, 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:

    • ignoring the asyncio logger
    • disabling the sentry logging integration
    • not creating any error events from the logging integration (event_level=None)
    • removing all potential sources of logs during GC
    • ... possibly more

    ...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.

  12. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on May 12, 2026
  13. self-assigned this
    on May 13, 2026
  14. sentrivana commented on May 21, 2026

    @sentrivana
    Collaborator

    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 list and set. The rest (included BoundedList) will now just be __repr__'d instead.

    The fix will be out in the next release, likely next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions