getsentry / sentry-python

The official Python SDK for Sentry.io
https://sentry.io/for/python/
MIT License
1.93k stars 510 forks source link

Sanic- Sentry integration: Sending 2 requests consecutively to an endpoint in a test function raised error #1460

Closed namnguyenbk closed 1 year ago

namnguyenbk commented 2 years ago

Environment

SaaS (https://sentry.io/)

Version

No response

Steps to Reproduce

Hi guys. Recently I upgraded Sanic and Sentry SDK's version of my web app. Yet there is a problem. Here I have a very rough Sanic web app and I also use Sentry for error tracking to demonstrate the problem: I got IndexError: pop from empty list When trying to write a test where I fired a GET request to an endpoint and verify the HTTP response code then I did the same in the same test function.](https://github.com/namnguyenbk/sanic-sentry-bug/blob/main/test.py#L10). See full traceback below.

Expected Result

The test could run successfully with Sanic=20.12.1. Still, the latest version of Sanic and Sentry makes it fail. Not sure what's going on and whether it affects other parts of the application. 🙏 Anyone has ideas to work around or thoughts?

Actual Result

ERROR    sanic.error:handlers.py:259 Exception occurred while handling uri: 'http://127.0.0.1:63443/hello'
Traceback (most recent call last):
  File "handle_request", line 102, in handle_request
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sanic/signals.py", line 193, in dispatch
    return await dispatch
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sanic/signals.py", line 163, in _dispatch
    retval = await maybe_coroutine
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sentry_sdk/integrations/sanic.py", line 184, in _hub_exit
    request.ctx._sentry_hub.__exit__(None, None, None)
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sentry_sdk/hub.py", line 246, in __exit__
    old = self._old_hubs.pop()
IndexError: pop from empty list
ERROR    sanic.error:handlers.py:259 Exception occurred while handling uri: 'http://127.0.0.1:63443/hello'
Traceback (most recent call last):
  File "handle_request", line 102, in handle_request
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sanic/signals.py", line 193, in dispatch
    return await dispatch
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sanic/signals.py", line 163, in _dispatch
    retval = await maybe_coroutine
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sentry_sdk/integrations/sanic.py", line 184, in _hub_exit
    request.ctx._sentry_hub.__exit__(None, None, None)
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sentry_sdk/hub.py", line 246, in __exit__
    old = self._old_hubs.pop()
IndexError: pop from empty list

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sanic/http.py", line 146, in http1
    await self.protocol.request_handler(self.request)
  File "handle_request", line 133, in handle_request
    "_state",
  File "handle_exception", line 105, in handle_exception
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sanic/signals.py", line 193, in dispatch
    return await dispatch
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sanic/signals.py", line 163, in _dispatch
    retval = await maybe_coroutine
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sentry_sdk/integrations/sanic.py", line 184, in _hub_exit
    request.ctx._sentry_hub.__exit__(None, None, None)
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sentry_sdk/hub.py", line 246, in __exit__
    old = self._old_hubs.pop()
IndexError: pop from empty list
ERROR    sanic.error:connection_task:19 protocol.connection_task uncaught
Traceback (most recent call last):
  File "handle_request", line 102, in handle_request
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sanic/signals.py", line 193, in dispatch
    return await dispatch
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sanic/signals.py", line 163, in _dispatch
    retval = await maybe_coroutine
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sentry_sdk/integrations/sanic.py", line 184, in _hub_exit
    request.ctx._sentry_hub.__exit__(None, None, None)
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sentry_sdk/hub.py", line 246, in __exit__
    old = self._old_hubs.pop()
IndexError: pop from empty list

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sanic/http.py", line 146, in http1
    await self.protocol.request_handler(self.request)
  File "handle_request", line 133, in handle_request
    "_state",
  File "handle_exception", line 105, in handle_exception
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sanic/signals.py", line 193, in dispatch
    return await dispatch
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sanic/signals.py", line 163, in _dispatch
    retval = await maybe_coroutine
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sentry_sdk/integrations/sanic.py", line 184, in _hub_exit
    request.ctx._sentry_hub.__exit__(None, None, None)
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sentry_sdk/hub.py", line 246, in __exit__
    old = self._old_hubs.pop()
IndexError: pop from empty list

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "connection_task", line 15, in connection_task
    from sanic.http import Http, Stage
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sanic/http.py", line 168, in http1
    await self.error_response(e)
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sanic/http.py", line 439, in error_response
    await app.handle_exception(self.request, exception)
  File "handle_exception", line 105, in handle_exception
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sanic/signals.py", line 193, in dispatch
    return await dispatch
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sanic/signals.py", line 163, in _dispatch
    retval = await maybe_coroutine
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sentry_sdk/integrations/sanic.py", line 184, in _hub_exit
    request.ctx._sentry_hub.__exit__(None, None, None)
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sentry_sdk/hub.py", line 246, in __exit__
    old = self._old_hubs.pop()
IndexError: pop from empty list
ERROR    sanic.root:testing.py:116 GET http://127.0.0.1:63443/hello received no response!
Traceback (most recent call last):
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/httpx/_transports/default.py", line 60, in map_httpcore_exceptions
    yield
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/httpx/_transports/default.py", line 353, in handle_async_request
    resp = await self._pool.handle_async_request(req)
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/httpcore/_async/connection_pool.py", line 253, in handle_async_request
    raise exc
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/httpcore/_async/connection_pool.py", line 237, in handle_async_request
    response = await connection.handle_async_request(request)
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/httpcore/_async/connection.py", line 90, in handle_async_request
    return await self._connection.handle_async_request(request)
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/httpcore/_async/http11.py", line 102, in handle_async_request
    raise exc
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/httpcore/_async/http11.py", line 81, in handle_async_request
    ) = await self._receive_response_headers(**kwargs)
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/httpcore/_async/http11.py", line 143, in _receive_response_headers
    event = await self._receive_event(timeout=timeout)
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/httpcore/_async/http11.py", line 186, in _receive_event
    raise RemoteProtocolError(msg)
httpcore.RemoteProtocolError: Server disconnected without sending a response.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/sanic_testing/testing.py", line 109, in _local_request
    response = await getattr(session, method.lower())(
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/httpx/_client.py", line 1729, in get
    return await self.request(
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/httpx/_client.py", line 1506, in request
    return await self.send(request, auth=auth, follow_redirects=follow_redirects)
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/httpx/_client.py", line 1593, in send
    response = await self._send_handling_auth(
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/httpx/_client.py", line 1621, in _send_handling_auth
    response = await self._send_handling_redirects(
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/httpx/_client.py", line 1658, in _send_handling_redirects
    response = await self._send_single_request(request)
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/httpx/_client.py", line 1695, in _send_single_request
    response = await transport.handle_async_request(request)
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/httpx/_transports/default.py", line 353, in handle_async_request
    resp = await self._pool.handle_async_request(req)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/contextlib.py", line 131, in __exit__
    self.gen.throw(type, value, traceback)
  File "/sentry-sanic-bug/venv/lib/python3.8/site-packages/httpx/_transports/default.py", line 77, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.RemoteProtocolError: Server disconnected without sending a response.
INFO     sanic.root:runners.py:154 Starting worker [57202]
INFO     sanic.root:runners.py:157 Stopping worker [57202]
INFO     sanic.root:runner.py:592 Server Stopped
============================================================================================== short test summary info ==============================================================================================
FAILED test.py::test_basic_test_client - ValueError: Request and response object expected, got ([<Request: GET /hello>, None])
getsentry-release commented 2 years ago

Routing to @getsentry/team-web-sdk-backend for triage. ⏲️

obataku commented 2 years ago

@namnguyenbk https://github.com/sanic-org/sanic/pull/2451 may have resolved this; can you try to reproduce with sanic 22.6.0?

ahopkins commented 2 years ago

Is this still a thing?

ankitcshah14 commented 2 years ago

@ahopkins Yes, I am still facing this issue. sanic==22.6.2 sanic-testing==22.6.0 sanic-routing==22.3.0 pytest==7.2.0 pytest-asyncio==0.20.2 pytest-json==0.4.0 asynctest==0.13.0 jsonschema==3.0.2

szokeasaurusrex commented 1 year ago

Hey @namnguyenbk, I tried to reproduce this issue with the code you provided, but the test passed with the latest Sentry SDK and Sanic packages. My full pip freeze output for the environment I ran the tests in is provided below:

aiofiles==23.2.1
anyio==4.0.0
certifi==2023.7.22
h11==0.14.0
html5tagger==1.3.0
httpcore==0.18.0
httptools==0.6.1
httpx==0.25.0
idna==3.4
iniconfig==2.0.0
multidict==6.0.4
packaging==23.2
pluggy==1.3.0
pytest==7.4.3
sanic==23.6.0
sanic-routing==23.6.0
sanic-testing==23.6.0
sentry-sdk==1.33.0
sniffio==1.3.0
tracerite==1.1.0
typing_extensions==4.8.0
ujson==5.8.0
urllib3==2.0.7
uvloop==0.19.0
websockets==12.0

I will assume that this bug has been fixed, so I will close the issue. If you or @ankitcshah14 continue to experience this bug, please reopen the issue and provide us with the specific steps to reproduce the bug.