Order the gzip hijack error path so failures cannot corrupt the body - #56
Merged
Conversation
…body Closing the stream discarded its error and released the writer before the hijack was attempted, which cost two things. A failing Close means truncated gzip output, and a hijack succeeding right after hid it. That error is returned now. A failing Hijack left hijacked false with the writer already released, so a handler carrying on would take the plain path and append raw bytes to a body already advertised as Content-Encoding: gzip. The writer stays attached on that path instead, and since it is closed a further write fails with "flate: closed writer" rather than corrupting the response. The deferred close still returns it to the pool, and the pool is safe because Reset clears the closed flag. The writer is only released once Hijack has actually taken the connection. Reported by umputun on #55.
Coverage Report for CI Build 32201953393Coverage increased (+0.2%) to 97.186%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
umputun
approved these changes
Aug 19, 2026
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.
Follow-up to #55, addressing the error-path ordering umputun raised there.
hijackclosed the gzip stream, discarded theCloseerror, and released the writer to the pool, all before attempting the hijack:Two things followed from that.
A failing
Closewas invisible. It means truncated gzip output, and a hijack succeeding immediately after hid the fact. The error is returned now, wrapped.A failing
Hijackcould corrupt the response.hijackedstayed false whilew.gzwas already nil, so a handler that carried on writing took thew.ResponseWriter.Writepath and appended raw bytes to a body already carryingContent-Encoding: gzip. On that path the writer now stays attached, and because it is closed a further write fails withflate: closed writerinstead. Verified rather than assumed: a closedgzip.Writerreturns that error and leaks nothing into the output.The writer is released only once
Hijackhas actually taken the connection. On the failure path the deferredclosestill returns it to the pool, which is safe becauseResetclears the closed flag, and a secondCloseon an already-closed writer returns nil.Three cases in the new test, covering the failing close, the failed hijack followed by a write, and the successful hijack releasing the writer. The first two fail on the current code.
Narrow in practice, as noted on #55 —
Hijackfailing after the type assertion succeeded is mostly a double hijack — but the ordering is strictly safer and costs nothing.