Do multiple reads for WSGI even with content length#37
Merged
anuraaga merged 2 commits intoconnectrpc:mainfrom Oct 22, 2025
Merged
Do multiple reads for WSGI even with content length#37anuraaga merged 2 commits intoconnectrpc:mainfrom
anuraaga merged 2 commits intoconnectrpc:mainfrom
Conversation
stefanvanburen
approved these changes
Oct 21, 2025
src/connectrpc/_server_sync.py
Outdated
| environ: WSGIEnvironment, content_length: int | ||
| ) -> bytes: | ||
| bytes_read = 0 | ||
| chunks = [] |
Member
There was a problem hiding this comment.
nit: could we pre-allocate a bytearray to be size content_length and then use readinto?
Collaborator
Author
There was a problem hiding this comment.
WSGI doesn't actually define readinto.
https://peps.python.org/pep-3333/#input-and-error-streams
Since most servers will prebuffer anyways, I added a small optimization for that case instead
spenczar
reviewed
Oct 21, 2025
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
ea29372 to
f5f1139
Compare
spenczar
approved these changes
Oct 22, 2025
i2y
approved these changes
Oct 22, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For
.read()methodshttps://docs.python.org/3/library/io.html#io.RawIOBase.read
In practice, all WSGI servers seem to buffer the request to content-length before calling the app...except for pyvoy ;) Incidentally that means pyvoy is able to pass bidi streaming tests with WSGI too. I'm currently debugging an occasional macOS crash which seems envoy-related, but am feeling pretty good about the project.
I also took care to follow the WSGI spec "The application should not attempt to read more data than is specified by the CONTENT_LENGTH variable.". If this text wasn't there, it would be easiest to just call
.read()with no parameter.https://peps.python.org/pep-3333/#input-and-error-streams
I'm interested in adding pyvoy to conformance tests after some more debugging, but without it it's quite challenging to add a regression test for this change. I wanted to separate out the logic change from any testing changes but let me know if I should hold off until having that regression test.