Skip to content

Task was destroyed but it is pending: _coro_creating_hub_and_span #2908

Closed

Description

@cnicodeme

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.34.0

Steps to Reproduce

Hey team,

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>()]>>

Here's a sample:
https://cyril-nicodeme.sentry.io/issues/5081979263/events/6a88812f7d1d4c269d792b5f4b010541/

(Hopefully, you can also access more details about the issue via the UUID internally).

Expected Result

Should not have a "Task was destroyed but it is pending!" issue but properly send any issues

Actual Result

Fails with "Task was destroyed but it is pending"

Activity

  1. moved this to Waiting for: Product Owner in GitHub Issues with 👀 2on Mar 26, 2024
  2. sentrivana commented on Mar 28, 2024

    @sentrivana
    Collaborator

    Hey @cnicodeme, thank you for reporting this!

    Are the errors thrown when something specific happens (worker restarting, app shutdown, etc.) or just at random?

  3. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 2on Mar 28, 2024
  4. cnicodeme commented on Mar 28, 2024

    @cnicodeme
    Author

    Yes! When I restart the app, I get a ton of these

  5. moved this to Waiting for: Product Owner in GitHub Issues with 👀 2on Mar 28, 2024
  6. sentrivana commented on Mar 28, 2024

    @sentrivana
    Collaborator

    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).

  7. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 2on Mar 28, 2024
  8. cnicodeme commented on Mar 28, 2024

    @cnicodeme
    Author

    Thank you!

    I can implement a before_send filter, but what should I filter?

  9. 21 remaining items

  10. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 2on Apr 23, 2024
  11. knksmith57 commented on Aug 16, 2024

    @knksmith57

    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]
    
  12. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Aug 16, 2024
  13. szokeasaurusrex commented on Aug 19, 2024

    @szokeasaurusrex
    Member

    @knksmith57 does the workaround work for you?

  14. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Aug 19, 2024
  15. self-assigned this
    on Mar 12, 2025
  16. antonpirker commented on Mar 12, 2025

    @antonpirker
    Contributor

    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

  17. added a commit that references this issue on Mar 12, 2025
    50b1919
  18. ghost added on Aug 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions