Skip to content

Conversation

@alexander-alderman-webb
Copy link
Contributor

@alexander-alderman-webb alexander-alderman-webb commented Nov 13, 2025

Description

Copy metadata when wrapping coroutines in the patched asyncio task factory. Uses the functools.update_wrapper() function that is also used internally by functools.wraps(). Unlike when wrapping functions, less metadata is available on coroutines, so _wrap_coroutine() is equivalent to functools.wraps() with fewer attributes copied.

CPython copies __name__ and __qualname__ from function to coroutine: https://github.com/python/cpython/blob/a8733cbc7328dbeeed50e82ef13abb197d511c46/Objects/genobject.c#L1410.

As functools.update_wrapper() checks for the existence of properties first, copying metadata will not result in an AttributeError if metadata is not available. See the docs https://docs.python.org/3/library/functools.html#functools.update_wrapper

A small script to generate a task destroyed error.

import asyncio

import sentry_sdk
from sentry_sdk.integrations.asyncio import AsyncioIntegration

DSN = "..."

async def worker():
    await asyncio.sleep(10)

async def task():
    sentry_sdk.init(
        dsn=DSN,
        integrations=[AsyncioIntegration()],
        debug=True,
    )

    asyncio.create_task(worker())

if __name__ == "__main__":
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    loop.run_until_complete(task())

    loop.close()

Issues

The SDK no longer overwrites coroutine metadata included in destroyed errors as reported in #5072.

Reminders

@codecov
Copy link

codecov bot commented Nov 13, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.01%. Comparing base (9e14920) to head (5431e7c).
⚠️ Report is 2 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5105      +/-   ##
==========================================
+ Coverage   83.95%   84.01%   +0.06%     
==========================================
  Files         180      180              
  Lines       18010    18012       +2     
  Branches     3203     3203              
==========================================
+ Hits        15120    15133      +13     
+ Misses       1915     1907       -8     
+ Partials      975      972       -3     
Files with missing lines Coverage Δ
sentry_sdk/integrations/asyncio.py 86.88% <100.00%> (+0.44%) ⬆️

... and 4 files with indirect coverage changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants