Skip to content

don't force close() when headers is already sent#336

Merged
nggit merged 1 commit intomainfrom
tweak-autoclose
Nov 1, 2025
Merged

don't force close() when headers is already sent#336
nggit merged 1 commit intomainfrom
tweak-autoclose

Conversation

@nggit
Copy link
Copy Markdown
Owner

@nggit nggit commented Oct 19, 2025

The current behavior when the handler returns None is absolute close() as described in the docs.

@app.route('/')
async def hello_world():
    return None

I want to tweak it a little bit so that it respects keep-alive in the following case, because I see this looks "broken" in wrk benchmark but is actually not.

@app.route('/info')
async def hello_world(response):
    await response.sendfile('/path/to/file.txt', content_type='text/plain')
    # because of implicitly return None here

So for rensponse.end() to force close is to use keepalive=False:

    await response.end(b'Hello, World!', keepalive=False)

And that option was already available a long before, but useless. Now it is useful.

@sonarqubecloud
Copy link
Copy Markdown

@nggit nggit merged commit 3da9f54 into main Nov 1, 2025
25 checks passed
@nggit nggit deleted the tweak-autoclose branch November 1, 2025 10:27
@nggit nggit mentioned this pull request Nov 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant