Skip to content

Conversation

@winstxnhdw
Copy link
Contributor

@winstxnhdw winstxnhdw commented Oct 12, 2025

Description

To prevent cross-thread reference errors with Stream iterators, we keep iterators on the main context and listen for a disconnect asynchronously instead of vice versa.

I have properly split the changes into 3 separate commits for better diffs.

Other changes

  • Reuse stream_event dictionary to avoid recreating the dictionary on every iteration.
  • Remove redundant type checks, e.g. AsyncIterable covers AsyncIterator too.
  • Some miscellaneous inlining and using terse expressions like chain(self.cookies, cookies or ())

@codecov
Copy link

codecov bot commented Oct 12, 2025

Codecov Report

❌ Patch coverage is 95.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 97.84%. Comparing base (08e8dad) to head (83b668e).

Files with missing lines Patch % Lines
litestar/response/streaming.py 95.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4432   +/-   ##
=======================================
  Coverage   97.83%   97.84%           
=======================================
  Files         296      296           
  Lines       15286    15287    +1     
  Branches     1711     1712    +1     
=======================================
+ Hits        14955    14957    +2     
  Misses        189      189           
+ Partials      142      141    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@provinzkraut
Copy link
Member

Can you explain this change a bit more? Where is something handed off to a different thread?

@github-actions
Copy link

Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/4432

@winstxnhdw
Copy link
Contributor Author

winstxnhdw commented Oct 12, 2025

Can you explain this change a bit more? Where is something handed off to a different thread?

Originally, we start self._stream in another task like so.

async with create_task_group() as task_group:
    task_group.start_soon(partial(self._stream, send))
    await self._listen_for_disconnect(cancel_scope=task_group.cancel_scope, receive=receive)

If a user, who holds the reference to the iterator, tries to do something with it, Python raises a ValueError: generator already executing error. This only happens with a sync generator, presumably because we use sync_to_thread which actually hands it off to a different thread, and the combination of the stream being in a different context and iterating in a separate thread somehow leads to this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants