Repository navigation
Conversation
…hemy runs the driver in
… bridge A greenlet's parent is not necessarily its caller, so following any parent could attribute a span to unrelated code. Only SQLAlchemy's bridge greenlet, which it marks with __sqlalchemy_greenlet_provider__, is known to be spawned by the coroutine awaiting the query. Test through SQLAlchemy's real greenlet_spawn, add a case where an unrelated parent gives no source, and install greenlet for the SQLAlchemy 2.x test environments, where it is only an optional extra.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want fixes drafted automatically? Bugbot Autofix can create code changes for findings. A team admin can enable Autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit dc44691. Configure here.
….0.31 Earlier releases don't set __sqlalchemy_greenlet_provider__ on the bridge greenlet, so identify it by its class there.
…hout greenlet Skip on SQLAlchemy before 1.4 only, so the tests can't silently stop running if greenlet drops out of the 1.4+ environments.
SQLAlchemy 1.4 only depends on greenlet on some platforms, not macOS arm64, where the async bridge tests would otherwise fail.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
As discussed in #8205 (thanks @sentrivana).
With async SQLAlchemy, DB spans never get query source (
code.function,code.filepath, …), becauseadd_sourcenever finds an application frame.SQLAlchemy's asyncio extension runs the sync driver inside a greenlet that it starts from the coroutine awaiting the query. The cursor events that create the span run in that greenlet. Walking
f_backfrom there reaches only SQLAlchemy and greenlet frames, then stops at the greenlet's boundary, so it never sees the application code that issued the query.When the current greenlet is SQLAlchemy's bridge,
add_sourcenow continues into the parent greenlet's suspended frames, which hold the awaiting coroutine. Other greenlets aren't followed, since a greenlet's parent isn't necessarily its caller and following it could attribute the span to unrelated code. It only reads frames, and it does nothing ifgreenletisn't imported or the lookup fails.The bridge is recognised by
__sqlalchemy_greenlet_provider__, the attribute SQLAlchemy checks itself, which it sets from 1.4.53 and 2.0.31. Earlier releases don't set it, so there it is recognised by its class,_AsyncIoGreenletfrom asqlalchemymodule. I checked both paths locally against 1.4.52 and 2.0.30, where the test fails without the class check.The test runs a query through SQLAlchemy's real
greenlet_spawnfrom a coroutine and checks the coroutine is its source, for both static and streamed spans; it fails on master.It skips only before SQLAlchemy 1.4, not when
greenletis missing, so it fails instead of silently not running ifgreenletever drops out of the 1.4+ environments.greenletis only an optional extra of SQLAlchemy 2.x, and 1.4 installs it only on some platforms (not macOS arm64), so I added it to the 1.4+ test environments inscripts/populate_tox/config.py. Intox.iniI applied only the lines the generator adds for it, leaving out the 2.1.6 → 2.1.8 bump it would also make, to keep this PR scoped. Happy to commit the full regeneration instead if you prefer; a full run currently also bumps most other integrations.With the reproduction from the issue (SQLAlchemy 2.1.6, aiosqlite):
Issues
Written with Claude (Opus 5.5). I reviewed the change and ran the reproduction and the SQLAlchemy 1.2, 1.4, 2.0 and 2.1 test suites locally before opening this.