Skip to content

fix: Gracefully close sitemap stream on SitemapRequestLoader abort#1979

Merged
Pijukatel merged 2 commits into
apify:masterfrom
Mantisus:gracefull-stream-close
Jun 22, 2026
Merged

fix: Gracefully close sitemap stream on SitemapRequestLoader abort#1979
Pijukatel merged 2 commits into
apify:masterfrom
Mantisus:gracefull-stream-close

Conversation

@Mantisus

@Mantisus Mantisus commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

Description

Use contextlib.aclosing to let the
stream request finishes gracefully and avoids errors when closing generators.

Example error:

an error occurred during closing of asynchronous generator <async_generator object HttpxHttpClient.stream at 0x7bad2c3fbe10>
asyncgen: <async_generator object HttpxHttpClient.stream at 0x7bad2c3fbe10>
RuntimeError: aclose(): asynchronous generator is already running
@Mantisus Mantisus self-assigned this Jun 18, 2026
@Mantisus Mantisus requested a review from Pijukatel June 18, 2026 21:13

@vdusek vdusek left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Isn't this just a non-optimal workaround? 🙂

CC suggests utilizing this pattern:

  from contextlib import aclosing, suppress  # already importing suppress here

  # in _load_sitemaps, replace the bare `async for item in parse_sitemap(...)`:
  async with aclosing(
      parse_sitemap(
          [SitemapSource(type='url', url=sitemap_url)],
          self._http_client,
          proxy_info=self._proxy_info,
          options=parse_options,
      )
  ) as sitemap_items:
      async for item in sitemap_items:
          ...
@Mantisus

Copy link
Copy Markdown
Collaborator Author

Well, asyncio.sleep(0) is pretty common when working with HTTP clients. But yeah, contextlib.aclosing is better in this case.

@vdusek vdusek left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@Pijukatel Pijukatel merged commit 202726d into apify:master Jun 22, 2026
32 checks passed
Mantisus added a commit to Mantisus/crawlee-python that referenced this pull request Jun 30, 2026
…pify#1979)

### Description

Use `contextlib.aclosing` to let the
stream request finishes gracefully and avoids errors when closing
generators.

Example error:
```bash
an error occurred during closing of asynchronous generator <async_generator object HttpxHttpClient.stream at 0x7bad2c3fbe10>
asyncgen: <async_generator object HttpxHttpClient.stream at 0x7bad2c3fbe10>
RuntimeError: aclose(): asynchronous generator is already running
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants