Skip to content

Fix support for generator-based WSGI apps - #887

Merged
florimondmanca merged 15 commits into
encode:masterfrom
Singletoned:master
Mar 29, 2020
Merged

Fix support for generator-based WSGI apps#887
florimondmanca merged 15 commits into
encode:masterfrom
Singletoned:master

Conversation

@Singletoned

Copy link
Copy Markdown
Contributor

The WSGI adapter was trying to take the status code from start_response straightaway, rather than waiting until the first non-empty chunk, as per the spec: https://www.python.org/dev/peps/pep-3333/#the-start-response-callable

I've made a small change that iterates through the response until it gets a non-empty chunk, and then passes everything on as normal. I've also added a test for that scenario.

@florimondmanca
florimondmanca requested a review from a teamMarch 28, 2020 21:02

@florimondmancaflorimondmanca left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good catch, thanks! I checked out the code locally and it looks good (tests are failing without the fix). Left some suggestions here to reuse existing tests.

Comment threadhttpx/_dispatch/wsgi.py Outdated
Comment threadhttpx/_dispatch/wsgi.py Outdated
Comment threadtests/client/test_client.py Outdated
@Singletoned

Copy link
Copy Markdown
ContributorAuthor

I removed the "Content-length" header from the tests because it isn't actually used anywhere in the code, and if people look at the tests as an example, it might be misleading.

@yeraydiazdiazyeraydiazdiaz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @Singletoned! Just left a question regarding an edge case

Relevant section of PEP 3333 for reference:

However, the start_response callable must not actually transmit the response headers. Instead, it must store them for the server or gateway to transmit only after the first iteration of the application return value that yields a non-empty bytestring, or upon the application's first invocation of the write() callable. In other words, response headers must not be sent until there is actual body data available, or until the application's returned iterable is exhausted. (The only possible exception to this rule is if the response headers explicitly include a Content-Length of zero.)

This delaying of response header transmission is to ensure that buffered and asynchronous applications can replace their originally intended output with error output, up until the last possible moment. For example, the application may need to change the response status from "200 OK" to "500 Internal Error", if an error occurs while the body is being generated within an application buffer.

Comment threadhttpx/_dispatch/wsgi.py
Comment threadhttpx/_dispatch/wsgi.py Outdated
Comment threadtests/test_wsgi.py Outdated
Comment threadtests/test_wsgi.py Outdated
Comment threadhttpx/_dispatch/wsgi.py
Singletonedand others added 3 commits March 29, 2020 11:47
Co-Authored-By: Florimond Manca <florimond.manca@gmail.com>
Co-Authored-By: Florimond Manca <florimond.manca@gmail.com>
Co-Authored-By: Florimond Manca <florimond.manca@gmail.com>

@florimondmancaflorimondmanca left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All set on my side once this last fix is in! 👍

Thank you so much!

Comment threadhttpx/_dispatch/wsgi.py Outdated
Co-Authored-By: Florimond Manca <florimond.manca@gmail.com>
@florimondmancaflorimondmanca changed the title Handle generator WSGI appFix support for generator-based WSGI appsMar 29, 2020
@florimondmanca
florimondmanca merged commit 94323f9 into encode:masterMar 29, 2020
@lovelydinosaurlovelydinosaur mentioned this pull request May 21, 2020
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@Singletoned@yeraydiazdiaz@florimondmanca