Skip to content

fix(integrations): Remove asyncio iscoroutinefunction warning#6090

Open
cooperoptigrid wants to merge 9 commits intogetsentry:masterfrom
cooperoptigrid:fix-integration-asyncio-iscoroutine-function-warning
Open

fix(integrations): Remove asyncio iscoroutinefunction warning#6090
cooperoptigrid wants to merge 9 commits intogetsentry:masterfrom
cooperoptigrid:fix-integration-asyncio-iscoroutine-function-warning

Conversation

@cooperoptigrid
Copy link
Copy Markdown

@cooperoptigrid cooperoptigrid commented Apr 17, 2026

Description

Uses Django ASGI function patching to use inspect.iscoroutinefunction instead of asyncio.iscoroutinefunction to avoid depreaction warning in 3.14.
Rolled out to all uses of asyncio.iscoroutinefunction in the integrations package.

Repro in issue doesn't raise warning with these changes. tox linters and test pass on py3.12, py3.14.

Issues

Reminders

@sdk-maintainer-bot sdk-maintainer-bot Bot added issue-already-assigned Used for automated community contribution checks. violating-contribution-guidelines Used for automated community contribution checks. labels Apr 17, 2026
@sdk-maintainer-bot
Copy link
Copy Markdown

This PR has been automatically closed. The referenced issue is already assigned to someone else.

If you believe this assignment is outdated, please comment on the issue to discuss before opening a new PR.

Please review our contributing guidelines for more details.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 17, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

  • (ci) Cancel in-progress PR workflows on new commit push by joshuarli in #5994
  • Add db.driver.name spans to database integrations by ericapisani in #6082

Bug Fixes 🐛

  • (google_genai) Redact binary data in inline_data and fix multi-part message extraction by ericapisani in #5977
  • (grpc) Add isolation_scope to async server interceptor by robinvd in #5940
  • (integrations) Asyncio iscoroutinefunction warning by cooperoptigrid in #6090
  • (profiler) Stop nulling buffer on teardown by ericapisani in #6075

Internal Changes 🔧

  • (celery) Remove unused NoOpMgr from utils by sentrivana in #6078
  • (ci) Update outdated pinned action version comments by JoshuaMoelans in #6088
  • (pydantic-ai) Remove dead Model.request patch by alexander-alderman-webb in #5956
  • (tests) Replace deprecated enable_tracingwith traces_sample_rate by sentrivana in #6077
  • Set explicit base-branch for codecov action by ericapisani in #5992

🤖 This preview updates automatically when you update the PR.

@ericapisani ericapisani reopened this Apr 20, 2026
@ericapisani ericapisani removed violating-contribution-guidelines Used for automated community contribution checks. issue-already-assigned Used for automated community contribution checks. labels Apr 20, 2026
Copy link
Copy Markdown
Contributor

@alexander-alderman-webb alexander-alderman-webb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inspect.iscoroutinefunction is not a drop-in replacement for asyncio.iscoroutinefunction on old Python versions (which we support).

There's some details in this blog post: https://lucumr.pocoo.org/2016/10/30/i-dont-understand-asyncio/

There's an existing PR containing some advice on approaching the migration. Generally mirroring the respective library's version checks would be great (not just copying Django's specific handling across the whole SDK)!

#4915

Comment thread sentry_sdk/integrations/_asgi_common.py Outdated
Comment on lines +34 to +36
def _markcoroutinefunction(func: "_F") -> "_F":
func._is_coroutine = asyncio.coroutines._is_coroutine # type: ignore[attr-defined]
return func
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_markcoroutinefunction is not used outside of the Django integration, so please leave it there.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've simply moved it back for this PR. Still digesting the other comment

@cooperoptigrid cooperoptigrid force-pushed the fix-integration-asyncio-iscoroutine-function-warning branch 2 times, most recently from b90710a to 6d83d5e Compare April 20, 2026 23:32
@cooperoptigrid cooperoptigrid force-pushed the fix-integration-asyncio-iscoroutine-function-warning branch from 6d83d5e to 6da51e6 Compare April 20, 2026 23:33
@cooperoptigrid
Copy link
Copy Markdown
Author

#4915

@alexander-alderman-webb Do you want me to take those changes from that PR and add those patches here and address any outstanding comments? Or were there fundamental issues blocking its merge, and I should take a different approach entirely.

@cooperoptigrid
Copy link
Copy Markdown
Author

I've switched to using the PY313 check, as per the other comments. I've left all existing uses of inspect as-is. There are dozens of uses of asyncio.iscoroutinefunction in the tests/. I decided not to change them in this PR, to try and keep it focused, but happy to swap them directly to inspect

@cooperoptigrid cooperoptigrid marked this pull request as ready for review April 23, 2026 12:10
@cooperoptigrid cooperoptigrid requested a review from a team as a code owner April 23, 2026 12:10
@cooperoptigrid cooperoptigrid changed the title fix(integrations): asyncio iscoroutinefunction warning fix(integrations): Remove asyncio iscoroutinefunction warning Apr 23, 2026
@alexander-alderman-webb
Copy link
Copy Markdown
Contributor

Hi @cooperoptigrid.

I've taken care of the integrations now in #6133, #6134 and #6135.

TL;DR: to be on the safe side we want to mirror the library's handling, and it turns out that each library has a different version cut-off. We're taking this precaution because asyncio.iscoroutinefunction is unfortunately not the same as inspect.iscoroutinefunction when it comes to functions marked with _is_coroutine. You can run this script below to verify.

This is possibly quite rare but it's also easier for us to maintain 😅. The risk would be creating an async/sync wrapper for a function that is treated as sync/async in the library, which could cause a coroutine to never be awaited or a TypeError.

If you'd like, feel free to update the tests. You should be able to just convert the occurrences of asyncio.iscoroutinefunction to inspect.iscoroutinefunction in tests 🙏 .

import asyncio
import inspect

async def real_async_handler(request):
    return f"hello {request}"

def my_handler(request):
    return real_async_handler(request)

my_handler._is_coroutine = asyncio.coroutines._is_coroutine

print(f"inspect.iscoroutinefunction(my_handler): "
      f"{inspect.iscoroutinefunction(my_handler)}")
print(f"asyncio.iscoroutinefunction(my_handler): "
      f"{asyncio.iscoroutinefunction(my_handler)}")

Comment on lines +20 to +22
_iscoroutinefunction = (
inspect.iscoroutinefunction if PY313 else asyncio.iscoroutinefunction
)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The version check for asyncio.iscoroutinefunction deprecation is incorrect. It checks for Python 3.13+ (PY313) but the deprecation occurred in Python 3.12.
Severity: LOW

Suggested Fix

Update the version check to target Python 3.12 instead of 3.13. This can be done by creating and using a PY312 constant or by reverting to the previous check hasattr(inspect, "markcoroutinefunction") which correctly identifies Python 3.12 and newer.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: sentry_sdk/integrations/_asgi_common.py#L20-L22

Potential issue: The code uses a version check for Python 3.13 (`PY313`) to decide
whether to use `inspect.iscoroutinefunction` instead of the deprecated
`asyncio.iscoroutinefunction`. However, `asyncio.iscoroutinefunction` was deprecated
starting in Python 3.12. Consequently, when running on Python 3.12, the code will still
use the deprecated function, generating a `DeprecationWarning`. This negates the
intended fix of the pull request, which was to remove this warning for supported Python
versions.

Also affects:

  • sentry_sdk/_compat.py:16~16

Did we get this right? 👍 / 👎 to inform future reviews.

@cooperoptigrid
Copy link
Copy Markdown
Author

Thanks for those PRs @alexander-alderman-webb , makes sense now I see what you meant by mirroring the library’s approach!

I’ll change the scope of this PR to simply updating test code.

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.

asyncio.iscoroutinefunction usage in integrations raises deprecation warning on 3.14

3 participants