Skip to content

asyncio.CancelledError exceptions are swallowed by capture_internal_exceptions() #4853

@aconchillo

Description

@aconchillo

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.38.0

Steps to Reproduce

This issue occurs when using OpenAI SDK and Sentry. If you try to cancel a task that is in the middle of processing OpenAI completion iterators, the task will never be cancelled because Sentry SDK is swallowing the exception.

This occurs in the following code:
https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/integrations/openai.py#L337-L370

The following code of course fixes the issue but it defeats the purpose of suppressing internal exceptions:

class CaptureInternalException:
    __slots__ = ()

    def __enter__(self):
        # type: () -> ContextManager[Any]
        return self

    def __exit__(self, ty, value, tb):
        # type: (Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]) -> bool
        if ty is not None and value is not None:
            capture_internal_exception((ty, value, tb))

        if isinstance(ty, asyncio.CancelledError):          <-------------------- ADDED
            return False

        return True

However, I believe asyncio.CancelledError is an special case that should be allowed.

Expected Result

Task cancellations should work.

Actual Result

Tasks are never cancelled leaving applications in an invalid state.

Metadata

Metadata

Assignees

Projects

Status

Waiting for: Product Owner

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions