Skip to content

Sanic integration does not send any errors outside a request (like in a background task) #1690

@cnicodeme

Description

@cnicodeme

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.9.9

Steps to Reproduce

Sanic is using Sentry Hub by plugging the 'http.lifecycle.request' signal to start the Hub, and the 'http.lifecycle.response' to exit it.

The problem is that when errors occurs outside the HTTP lifecycle request, like in a background task, no errors are reported by Sentry/Sanic.

Here's a reproducible bug:

from sanic import Sanic
from sanic.response import text
import asyncio, sentry_sdk
from sentry_sdk.integrations.sanic import SanicIntegration

app = Sanic("Sanic-Sentry-Test")


async def fail_later(*args, **kwargs):
    print('Starting task, might take a while...')
    await asyncio.sleep(5)
    value = 0
    print('Value is: {}'.format(value))
    print('Output is {}'.format(5/value))  # This should raise a ZeroDivisionError exception, but nothing
    print('Done task.')


@app.get("/")
async def hello_world(request):
    request.app.add_task(fail_later(), name="fail_later")
    return text("Hello, world.")


sentry_sdk.init('...url', integrations=[SanicIntegration()])

if __name__ == "__main__":
    app.run()

Of course, the same code, but with the ZeroDivisionError directly on hello_world properly appears at Sentry, which means the integration is working as expected when inside a request, but not when outside.

I've seen with the team at Sanic, including @ahopkins about this on sanic-org/sanic#2576.

Expected Result

The exception should be logged at Sentry

Actual Result

Nothing is logged

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Waiting for: Community

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions