Skip to content

Conversation

@GSVarsha
Copy link
Contributor

@GSVarsha GSVarsha commented Nov 13, 2025

Long story short

Our existing WSGI middleware implementation did not consider the fact that WSGI applications return an iterable object.

The long story

A WSGI application must return an iterable of byte strings as its response body. This iterable can be a list, a generator, or any other object that implements the iteration protocol.

Here's why and how:
  • Streaming and Efficiency: Returning an iterable allows the WSGI server to send the response body in chunks, rather than waiting for the entire response to be generated in memory. This is crucial for handling large responses, streaming data, and improving perceived performance for the client.
  • Lazy Generation: By using a generator, the application can generate parts of the response body on demand, which is beneficial for resource-intensive operations or when dealing with dynamic content that changes over time.

Examples:

• Returning a list: This is a common approach for smaller, complete responses.

def application(environ, start_response):
    status = '200 OK'
    headers = [('Content-type', 'text/plain')]
    start_response(status, headers)
    return [b"Hello, World!\n"]

• Using a generator: This allows for streaming and lazy generation.

def application(environ, start_response):
    status = '200 OK'
    headers = [('Content-type', 'text/plain')]
    start_response(status, headers)
    yield b"Hello "
    yield b"World!\n"

Important Considerations:

• close() Method: If the returned iterable has a close() method, the WSGI server is responsible for calling it upon completion of the request, allowing the application to release any resources. [1]

@GSVarsha GSVarsha added this to the H2-2025 milestone Nov 13, 2025
@GSVarsha GSVarsha self-assigned this Nov 13, 2025
@GSVarsha GSVarsha marked this pull request as ready for review November 13, 2025 09:26
@GSVarsha GSVarsha requested a review from a team as a code owner November 13, 2025 09:26
Copy link
Member

@pvital pvital left a comment

Choose a reason for hiding this comment

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

It looks good for me, even with space for a code cleanup. ;-)

Signed-off-by: Varsha GS <varsha.gs@ibm.com>
Signed-off-by: Varsha GS <varsha.gs@ibm.com>
Signed-off-by: Varsha GS <varsha.gs@ibm.com>
@sonarqubecloud
Copy link

@GSVarsha GSVarsha merged commit 6bc03da into main Nov 13, 2025
19 checks passed
@GSVarsha GSVarsha deleted the fix-wsgi-async branch November 13, 2025 13:04
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.

3 participants