'types.SimpleNamespace' object has no attribute '_sentry_hub'
Environment
How do you use Sentry? Sentry SaaS (sentry.io)
Which SDK and version? Sentry-sdk v1.5.2
I'm facing the same issue as #1290, but with the latest (v1.5.2) version of Sentry-sdk
This is the public link to the issue: https://sentry.io/share/issue/0464a5d631244872a70b7afacefa01aa/
I cannot recreate this. This seems to work as expected using:
aiofiles==0.8.0
certifi==2021.10.8
httptools==0.3.0
multidict==5.2.0
sanic==21.12.1
sanic-routing==0.7.2
sentry-sdk==1.5.2
ujson==5.1.0
urllib3==1.26.8
uvloop==0.16.0
websockets==10.1
import sentry_sdk
from sentry_sdk.integrations.sanic import SanicIntegration
from sanic import Sanic, text
sentry_sdk.init(
dsn="https://[email protected]/5909200",
integrations=[SanicIntegration()],
)
app = Sanic(__name__)
@app.exception(ZeroDivisionError)
async def zero(*_):
return text("0")
@app.get("/")
async def handler(request):
1 / 0
app.run(port=9999, debug=True)
I'm still having this issue while being on
sanic==21.12.0 sentry_sdk==1.5.4 (the latest as of now)
The original issue seems to be from:
./site-packages/sanic/server/protocols/http_protocol.py, line 110:
error_logger.exception("protocol.connection_task uncaught")
I suspect that since the issue occurs later in the process, it is executed outside the with wrapper for Sentry, causing the _sentry_hub key to not be available anymore
There also might be an issue with my "connection_task" executed for "http.lifecycle.begin" ?
If you need anything else, please ask.
I'm continuing this issue as I recently pushed an update on my code that triggered two new similar issues at Sentry. I'm guessing they are "new issues" because they originate from another part of the code: I certainly have a bug that results in an exception at someplace that triggers Sentry out of place. I'm sharing these here in case it helps figure out where.
Unfortunately, Sentry doesn't provide any more details on the origin of the issue so it's hard for me to understand where the problem originates and to go up the ladder until I identify the problem.
- https://sentry.io/share/issue/e429706316b141159ae08ed19547ac08/
- https://sentry.io/share/issue/61cd71ddba8f49f2be028cb8aa91b9cc/
One of my guesses is that I do quite some work once the request is finished (I commit to the database, run some asynchronous tasks (via Celery). I suspect that the exception occurs once the requests have finished, which causes the with command to have finished, removing that _sentry_hub, that is then expected to be present, but I don't have a grasp of how the code run on this Sanic/Sentry part.
I'm also facing this issue. application is working fine, just test are not running. If i just remove the sentry codes, then test are passed.
code to reproduce the error:
# requirements.txt
pytest==7.0.1
pytest-asyncio==0.18.2
sanic==21.12.1
sanic-routing==0.7.2
sanic-testing==0.8.2
sentry-sdk==1.5.7
# server.py
from sanic import Sanic
from sanic.response import json
import sentry_sdk
from sentry_sdk.integrations.sanic import SanicIntegration
sentry_sdk.init(
dsn="https://[email protected]/6253212",
integrations=[SanicIntegration()]
)
app = Sanic("test")
@app.route('/')
async def test(request):
return json({'message': 'hola'})
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8000)
# test_sample.py
from server import app
async def test_index():
_, resp = await app.asgi_client.get('/')
assert resp.status_code == 200
assert resp.json == {'message': 'hola'}
$ pytest -vvv
I also started to implement tests and discovered that the _sentry_hub issues goes crazy there. I was planning on updating this ticket but @hrshadhin did the job perfectly! Thanks!
I think this pull request resolve this problem: https://github.com/sanic-org/sanic/pull/2451
Is this still an issue?
Using Sanic v22.6.2 and the above code

I'll update today to 22.6.2 and see if I still have these. I'll let you know!
I tried with Sanic v22.6.2 and my tests are passed. issue resolved.
@sl0thentr0py Can we get this issue closed? I will add a PR to update the docs to add the specific version.
Sorry to be THAT guy, but I'm still having issues with Sanic v22.12.0. Here's one of these:
https://cyril-nicodeme.sentry.io/share/issue/36c0531d78ab462994eae3e0f6f930b6/
Looking on Sentry, I can't provide more details than what is present on the link above, but if you need any, please ask.