You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
test_write_timeout(trio) is failing on python3.14 #3686
===============================================================================================testsessionstarts================================================================================================platformdarwin--Python3.13.3, pytest-8.4.1, pluggy-1.6.0rootdir: /Users/augustogoulart/Workspace/httpxconfigfile: pyproject.tomlplugins: anyio-4.11.0collected2itemstests/test_timeouts.py .F [100%]
=====================================================================================================FAILURES=====================================================================================================_____________________________________________________________________________________________test_write_timeout[trio] _____________________________________________________________________________________________agen=<async_generatorobjectByteStream.__aiter__at0x108b8d840>
@_core.enable_ki_protectiondeffinalizer(agen: AsyncGeneratorType[object, NoReturn]) ->None:
try:
self.foreign.remove(id(agen))
exceptKeyError:
is_ours=Trueelse:
is_ours=Falseagen_name=name_asyncgen(agen)
ifis_ours:
runner.entry_queue.run_sync_soon(
finalize_in_trio_context,
agen,
agen_name,
)
# Do this last, because it might raise an exception# depending on the user's warnings filter. (That# exception will be printed to the terminal and# ignored, since we're running in GC context.)>warnings.warn(
f"Async generator {agen_name!r} was garbage collected before it ""had been exhausted. Surround its use in 'async with ""aclosing(...):' to ensure that it gets cleaned up as soon as ""you're done using it.",
ResourceWarning,
stacklevel=2,
source=agen,
)
EResourceWarning: Asyncgenerator'httpx._content.ByteStream.__aiter__'wasgarbagecollectedbeforeithadbeenexhausted. Surrounditsusein'async with aclosing(...):'toensurethatitgetscleanedupassoonasyou'redoneusingit.
venv/lib/python3.13/site-packages/trio/_core/_asyncgens.py:122: ResourceWarning
AFAIK asyncio is more lenient (although providing shutdown_asyncgens() for such cases) and doesn't monitor async generators for resource leaks, while Trio does.
If an async generator gets garbage collected before being exhausted, asyncio will do nothing while Trio will raise the ResourceWarning shown above.
In the patch below you can see that if we replace the generator returned by ByteStream.__aiter__ with an async iterator (ByteStreamAsyncIterator), Trio will have nothing to watch for and the tests will pass.
Not saying this is a solution (maybe?) but it helps illustrating the issue.
Activity
augustogoulart commentedon Oct 10, 2025
I can see this in python3.13.3 already.
augustogoulart commentedon Oct 10, 2025
AFAIK asyncio is more lenient (although providing
shutdown_asyncgens()for such cases) and doesn't monitor async generators for resource leaks, while Trio does.If an async generator gets garbage collected before being exhausted, asyncio will do nothing while Trio will raise the
ResourceWarningshown above.In the patch below you can see that if we replace the generator returned by
ByteStream.__aiter__with an async iterator (ByteStreamAsyncIterator), Trio will have nothing to watch for and the tests will pass.Not saying this is a solution (maybe?) but it helps illustrating the issue.
fix: resolve async stream resource leak on timeout
Add real async iterator for ByteStreams