feat: Add experimental async transport (port of PR #4572)#5646
feat: Add experimental async transport (port of PR #4572)#5646
Conversation
Semver Impact of This PR🟡 Minor (new features) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨Pydantic Ai
Other
Bug Fixes 🐛
Documentation 📚
Internal Changes 🔧Docs
Openai Agents
Other
🤖 This preview updates automatically when you update the PR. |
Codecov Results 📊✅ 13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 7.72s All tests are passing successfully. ❌ Patch coverage is 18.41%. Project has 14359 uncovered lines. Files with missing lines (7)
Generated by Codecov Action |
Codecov Results 📊Generated by Codecov Action |
Add an experimental async transport using httpcore's async backend,
enabled via `_experiments={"transport_async": True}`.
This is a manual port of PR #4572 (originally merged into `potel-base`)
onto the current `master` branch.
Key changes:
- Refactor `BaseHttpTransport` into `HttpTransportCore` (shared base) +
`BaseHttpTransport` (sync) + `AsyncHttpTransport` (async, conditional
on httpcore[asyncio])
- Add `Worker` ABC and `AsyncWorker` using asyncio.Queue/Task
- Add `close_async()` / `flush_async()` to client and public API
- Patch `loop.close` in asyncio integration to flush before shutdown
- Add `is_internal_task()` ContextVar to skip wrapping Sentry-internal tasks
- Add `asyncio` extras_require (`httpcore[asyncio]==1.*`)
- Widen anyio constraint to `>=3,<5` for httpx and FastAPI
Refs: GH-4568
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
8c808bf to
4f8a00c
Compare
The base class _make_pool returns a union of sync and async pool types, so mypy sees _pool.request() as possibly returning a non-awaitable. Add type: ignore[misc] since within AsyncHttpTransport the pool is always an async type. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The asyncio extra on httpcore pulls in anyio, which conflicts with starlette's anyio<4.0.0 pin and causes pip to downgrade httpcore to 0.18.0. That old version crashes on Python 3.14 due to typing.Union not having __module__. Keep httpcore[http2] in requirements-testing.txt (shared by all envs) and add httpcore[asyncio] only to linters, mypy, and common envs. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- AsyncWorker.kill() now calls self._task.cancel() before clearing the reference, preventing duplicate consumers if submit() is called later - close() with AsyncHttpTransport now does best-effort sync cleanup (kill transport, close components) instead of silently returning - flush()/close() log warnings instead of debug when async transport used - Add __aenter__/__aexit__ to _Client for 'async with' support Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Asyncio and gevent don't mix — async tests using asyncio.run() fail under gevent's monkey-patching. Add skip_under_gevent decorator to all async tests in test_transport.py and test_client.py. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| assert calls[0] == ("on_dropped_event", "connection_error") | ||
| assert calls[1][0:2] == ("record_lost_event", "network_error") | ||
| assert calls[2][0:2] == ("record_lost_event", "network_error") | ||
|
|
||
|
|
||
| @skip_under_gevent | ||
| @pytest.mark.asyncio | ||
| @pytest.mark.parametrize("debug", (True, False)) | ||
| @pytest.mark.parametrize("client_flush_method", ["close", "flush"]) | ||
| @pytest.mark.parametrize("use_pickle", (True, False)) | ||
| @pytest.mark.parametrize("compression_level", (0, 9, None)) | ||
| @pytest.mark.parametrize("compression_algo", ("gzip", "br", "<invalid>", None)) | ||
| @pytest.mark.skipif(not PY38, reason="Async transport only supported in Python 3.8+") | ||
| async def test_transport_works_async( |
There was a problem hiding this comment.
Excessive test parameterization creates combinatorial explosion
The test_transport_works_async test at line 932 has 5 parametrized dimensions: debug (2), client_flush_method (2), use_pickle (2), compression_level (3), and compression_algo (4). This creates 2×2×2×3×4 = 96 test combinations. Combined with inherent async test overhead, this may significantly slow down CI runs and makes debugging failures difficult.
Verification
Read the test file to count parametrization decorators and calculated the Cartesian product of all parameter values.
Identified by Warden code-review · 9RA-X2A
Add an experimental async transport using httpcore's async backend,
enabled via
_experiments={"transport_async": True}.This is a manual port of PR #4572 (originally merged into
potel-base)onto the current
masterbranch.Key changes
transport.py: Refactor
BaseHttpTransportintoHttpTransportCore(shared base) +
BaseHttpTransport(sync) +AsyncHttpTransport(async, conditional on
httpcore[asyncio]). Extract shared helpers:_handle_request_error,_handle_response,_update_headers,_prepare_envelope. Updatemake_transport()to detect thetransport_asyncexperiment.worker.py: Add
WorkerABC base class andAsyncWorkerimplementation using
asyncio.Queue/asyncio.Task.client.py: Add
close_async()/flush_async()with async-vs-synctransport detection. Extract
_close_components()/_flush_components().api.py: Expose
flush_async()as a public API.integrations/asyncio.py: Patch
loop.closeto flush pending eventsbefore shutdown. Skip span wrapping for internal Sentry tasks.
utils.py: Add
is_internal_task()/mark_sentry_task_internal()via ContextVar for async task filtering.
setup.py: Add
"asyncio"extras_require (httpcore[asyncio]==1.*).config.py / tox.ini: Widen anyio to
>=3,<5for httpx and FastAPI.Notes
tox.iniwas manually edited (the generation script requires afree-threaded Python interpreter). A full regeneration should be done
before merge.
Refs: GH-4568