Skip to content

bug(py): Rare error when closing an unread stream Response with active #242

@Mantisus

Description

@Mantisus

This is an error that regularly occurs in the context of reading sitemap.xml in crawlee-python.

However, I was unable to create a test case to reproduce it in clean conditions using only impit.

The context of the error occurrence is the interruption of the iterator Response.aiter_bytes, the call await Response.asclose, and the end of the execution of asyncio.run. Adding a small pause asyncio.sleep(0.01) before closing the context completely eliminates the error.

Switching to sync Response.close also avoids the error.

Traceback

an error occurred during closing of asynchronous generator <async_generator object ImpitHttpClient.stream at 0x75041ded28a0>
asyncgen: <async_generator object ImpitHttpClient.stream at 0x75041ded28a0>
handle_traceback: Handle created at (most recent call last):
  File "/home/mantisus/.local/share/uv/python/cpython-3.13.3-linux-x86_64-gnu/lib/python3.13/asyncio/runners.py", line 194, in run
    with Runner(debug=debug, loop_factory=loop_factory) as runner:
  File "/home/mantisus/.local/share/uv/python/cpython-3.13.3-linux-x86_64-gnu/lib/python3.13/asyncio/runners.py", line 62, in __exit__
    self.close()
  File "/home/mantisus/.local/share/uv/python/cpython-3.13.3-linux-x86_64-gnu/lib/python3.13/asyncio/runners.py", line 71, in close
    loop.run_until_complete(loop.shutdown_asyncgens())
  File "/home/mantisus/.local/share/uv/python/cpython-3.13.3-linux-x86_64-gnu/lib/python3.13/asyncio/base_events.py", line 706, in run_until_complete
    self.run_forever()
  File "/home/mantisus/.local/share/uv/python/cpython-3.13.3-linux-x86_64-gnu/lib/python3.13/asyncio/base_events.py", line 677, in run_forever
    self._run_once()
  File "/home/mantisus/.local/share/uv/python/cpython-3.13.3-linux-x86_64-gnu/lib/python3.13/asyncio/base_events.py", line 2026, in _run_once
    handle._run()
  File "/home/mantisus/.local/share/uv/python/cpython-3.13.3-linux-x86_64-gnu/lib/python3.13/asyncio/events.py", line 89, in _run
    self._context.run(self._callback, *self._args)
  File "/home/mantisus/.local/share/uv/python/cpython-3.13.3-linux-x86_64-gnu/lib/python3.13/asyncio/tasks.py", line 873, in _done_callback
    outer.set_result(results)
RuntimeError: aclose(): asynchronous generator is already running

Metadata

Metadata

Assignees

No one assigned

    Labels

    t-toolingIssues with this label are in the ownership of the tooling team.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions