You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Task was destroyed but it is pending: _coro_creating_hub_and_span #2908
I'm having a lot of "Task was destroyed but it is pending!" issues reported at Sentry.
I'm running Sanic (v23.6.0) and here's a sample of the errors:
Task was destroyed but it is pending!
task: <Task pending name='Task-532' coro=<patch_asyncio.._sentry_task_factory.._coro_creating_hub_and_span() running at /var/www/fernand/env/lib/python3.9/site-packages/sentry_sdk/integrations/asyncio.py:52> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f7af084d2b0>()]>>
Thanks for confirming, I expected as much. The SDK being responsible for the errors being created is one thing, but we're even reporting them to Sentry, which is doubly wrong.
If you want to get rid of them before we fix this, one workaround would be to filter them out in a custom before_send (see docs).
FWIW we're seeing the same/similar thing, and it looks like the asyncio extension is the culprit (in our case):
INFO: Application shutdown complete.
INFO: Finished server process [223]
Exception ignored in: <coroutine object patch_asyncio.<locals>._sentry_task_factory.<locals>._coro_creating_hub_and_span at 0xffff876de890>
Traceback (most recent call last):
File "/app/.venv/lib/python3.12/site-packages/sentry_sdk/integrations/asyncio.py", line 46, in _coro_creating_hub_and_span
with sentry_sdk.isolation_scope():
File "/usr/local/lib/python3.12/contextlib.py", line 158, in __exit__
self.gen.throw(value)
File "/app/.venv/lib/python3.12/site-packages/sentry_sdk/scope.py", line 1638, in isolation_scope
_current_scope.reset(current_token)
ValueError: <Token var=<ContextVar name='current_scope' default=None at 0xffffa2f49760> at 0xffff91567c80> was created in a different Context
Task was destroyed but it is pending!
task: <Task pending name='Task-16' coro=<patch_asyncio.<locals>._sentry_task_factory.<locals>._coro_creating_hub_and_span() done, defined at /app/.venv/lib/python3.12/site-packages/sentry_sdk/integrations/asyncio.py:42> wait_for=<Future pending cb=[Protocol._on_waiter_completed(), Task.task_wakeup()]>>
INFO: Stopping reloader process [211]
When asyncio tasks are shut down mid execution, the Task was destroyed but it is pending! message is emitted. This is happening if Sentry is added to project or not. There is not much we can do about this.
If Sentry is enabled there is another message emitted, because we wrap the users coroutine and use a ContextVar in the wrapper. And this triggers a similar message during shut down.
What we can improve is how present the errors during shutdown and make it clear to the user what is happening and why. This is done in this PR: #4129
Activity
sentrivana commentedon Mar 28, 2024
Hey @cnicodeme, thank you for reporting this!
Are the errors thrown when something specific happens (worker restarting, app shutdown, etc.) or just at random?
cnicodeme commentedon Mar 28, 2024
Yes! When I restart the app, I get a ton of these
sentrivana commentedon Mar 28, 2024
Thanks for confirming, I expected as much. The SDK being responsible for the errors being created is one thing, but we're even reporting them to Sentry, which is doubly wrong.
If you want to get rid of them before we fix this, one workaround would be to filter them out in a custom
before_send(see docs).cnicodeme commentedon Mar 28, 2024
Thank you!
I can implement a
before_sendfilter, but what should I filter?21 remaining items
knksmith57 commentedon Aug 16, 2024
FWIW we're seeing the same/similar thing, and it looks like the asyncio extension is the culprit (in our case):
szokeasaurusrex commentedon Aug 19, 2024
@knksmith57 does the workaround work for you?
antonpirker commentedon Mar 12, 2025
I have now tested this using this project:
https://github.com/antonpirker/testing-sentry/tree/main/test-plain-python-asyncio-shutdown
When asyncio tasks are shut down mid execution, the
Task was destroyed but it is pending!message is emitted. This is happening if Sentry is added to project or not. There is not much we can do about this.If Sentry is enabled there is another message emitted, because we wrap the users coroutine and use a ContextVar in the wrapper. And this triggers a similar message during shut down.
What we can improve is how present the errors during shutdown and make it clear to the user what is happening and why. This is done in this PR: #4129
Improve asyncio integration error handling. (#4129)