sentry-python icon indicating copy to clipboard operation
sentry-python copied to clipboard

'types.SimpleNamespace' object has no attribute '_sentry_hub'

Open cnicodeme opened this issue 3 years ago • 6 comments

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/

cnicodeme avatar Jan 14 '22 15:01 cnicodeme

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)

ahopkins avatar Jan 16 '22 07:01 ahopkins

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.

cnicodeme avatar Feb 01 '22 17:02 cnicodeme

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.

cnicodeme avatar Feb 12 '22 08:02 cnicodeme

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

hrshadhin avatar Mar 10 '22 19:03 hrshadhin

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!

cnicodeme avatar Mar 11 '22 07:03 cnicodeme

I think this pull request resolve this problem: https://github.com/sanic-org/sanic/pull/2451

zozzz avatar May 10 '22 14:05 zozzz

Is this still an issue?

ahopkins avatar Aug 23 '22 12:08 ahopkins

Using Sanic v22.6.2 and the above code

image

ahopkins avatar Aug 23 '22 12:08 ahopkins

I'll update today to 22.6.2 and see if I still have these. I'll let you know!

cnicodeme avatar Aug 26 '22 07:08 cnicodeme

I tried with Sanic v22.6.2 and my tests are passed. issue resolved.

hrshadhin avatar Aug 26 '22 14:08 hrshadhin

@sl0thentr0py Can we get this issue closed? I will add a PR to update the docs to add the specific version.

ahopkins avatar Aug 28 '22 10:08 ahopkins

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.

cnicodeme avatar Mar 17 '23 08:03 cnicodeme