Skip to content

Stop writing a 500 on top of a response that already started - #3715

Merged
benoitc merged 2 commits into
benoitc:masterfrom
gyanu2507:no-500-on-started-response
Aug 31, 2026
Merged

Stop writing a 500 on top of a response that already started#3715
benoitc merged 2 commits into
benoitc:masterfrom
gyanu2507:no-500-on-started-response

Conversation

@gyanu2507

Copy link
Copy Markdown
Contributor

Worker timeout fires SIGABRT → sys.exit(1). That's a BaseException, so the except Exception guard that checks headers_sent never ran. handle_error then wrote a 500 onto the same socket, which is how wget ended up with ISE HTML concatenated onto a partial download.

Once headers are out we just close, same as a regular exception.

Fixes #3410

Worker timeout raises SystemExit, which skipped the headers_sent guard.
@benoitc

benoitc commented Aug 31, 2026

Copy link
Copy Markdown
Owner

Thanks for tracking this down, the fix is right: once headers are out we should close, not write a 500.

One thing on the catch itself: except BaseException is too broad. The abort handler does sys.exit(1), so the only thing reaching here beyond a normal Exception is SystemExit. Could you narrow it to:

except (Exception, SystemExit):

in all three workers possibly.

@benoitc
benoitc self-requested a review August 31, 2026 00:12
@benoitc benoitc self-assigned this Aug 31, 2026

@benoitc benoitc left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

lPlease look at my comment about the exception. Once handled , the code is OK to merge.

@gyanu2507

Copy link
Copy Markdown
Contributor Author

Narrowed the headers-sent catch in sync, gthread, and the async worker to except (Exception, SystemExit).

@gyanu2507
gyanu2507 requested a review from benoitc August 31, 2026 05:15
@benoitc
benoitc merged commit 1597b05 into benoitc:master Aug 31, 2026
16 checks passed
@benoitc

benoitc commented Aug 31, 2026

Copy link
Copy Markdown
Owner

Thanks @gyanu2507, merged.

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.

Gunicorn injects "HTTP/1.1 500 Internal Server Error" into body when worker is terminated while streaming response

2 participants