Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cnicodeme opened this issue Jan 14, 2022 · 12 comments
Closed

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

cnicodeme opened this issue Jan 14, 2022 · 12 comments
Labels
Type: Bug Something isn't working

Comments

@cnicodeme
Copy link

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/

@ahopkins
Copy link
Contributor

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://555f76ca77cc40f9ba5dd92c69a2d4a3@o622198.ingest.sentry.io/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)

@cnicodeme
Copy link
Author

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
Copy link
Author

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.

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.

@hrshadhin
Copy link

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://d155ac31cc134af588a34fa2b0d870f4@o316508.ingest.sentry.io/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

@cnicodeme
Copy link
Author

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!

@zozzz
Copy link

zozzz commented May 10, 2022

I think this pull request resolve this problem: sanic-org/sanic#2451

@ahopkins
Copy link
Contributor

ahopkins commented Aug 23, 2022

Is this still an issue?

@ahopkins
Copy link
Contributor

Using Sanic v22.6.2 and the above code

image

@cnicodeme
Copy link
Author

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

@hrshadhin
Copy link

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

@ahopkins
Copy link
Contributor

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

@cnicodeme
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants