question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:17 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
ahopkinscommented, Oct 19, 2022

Ahh, okay. So this ticket should probably wait for that.

1reaction
antonpirkercommented, Oct 20, 2022

I hope to get something out today. We have a PR ready for the change: https://github.com/getsentry/sentry-python/pull/1695 Some work still needs to be done. But if this is merged we are ready for a release and will deploy a new version of the SDK today.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sanic-like background tasks · Issue #4475 · tiangolo/fastapi
I am exploring migrating this application from Sanic to FastAPI, and would like the ability to run this kind of background task.
Read more >
Sanic support background task? - Questions and Help
I just want to get the parameters passed by the post route, and then execute the task in the background. The route does...
Read more >
Sanic Documentation
Sanic is a Python 3.6+ web server and web framework that's written to go fast. It allows the usage of the async/await.
Read more >
Basic Options for Sanic | Sentry Documentation
These options are set when the SDK is first initialized, passed to the init ... If that variable also does not exist, the...
Read more >
Liberate your API: Building a task manager inside Sanic
Session language – EnglishTarget audience – DevelopersAn inside look at some of the tools inside Sanic to help build a background task ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found