Make compression reader/writer Close() idempotent#1013
Open
veeceey wants to merge 1 commit intogorilla:mainfrom
Open
Make compression reader/writer Close() idempotent#1013veeceey wants to merge 1 commit intogorilla:mainfrom
veeceey wants to merge 1 commit intogorilla:mainfrom
Conversation
The flateReadWrapper.Read() preemptively calls Close() when it encounters EOF, returning the flate reader to the pool early. When NextReader() is subsequently called, it calls Close() again on the same wrapper. Previously, this second Close() returned io.ErrClosedPipe, which caused spurious errors when compression was enabled (e.g. "discarding reader close error: io: read/write on closed pipe"). Per RFC 6455 and standard Go conventions, closing an already-closed resource should be a no-op. This change makes both flateReadWrapper and flateWriteWrapper Close() methods idempotent by returning nil instead of an error when the underlying reader/writer is already nil. Fixes gorilla#859 Signed-off-by: Varun Chawla <varun_6april@hotmail.com>
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.
Fixes #859
flateReadWrapper.Read()preemptively callsClose()when it encounters EOF to return the flate reader to the pool early. WhenNextReader()is called next, it callsClose()on the same wrapper again. This secondClose()was returningio.ErrClosedPipe, causing spurious errors when compression was enabled.The fix makes
Close()on bothflateReadWrapperandflateWriteWrapperidempotent — returningnilon an already-closed wrapper instead of an error. This follows Go conventions where closing an already-closed resource should be a safe no-op, and avoids the error path that was causing the log spam reported in #859.The
Read()andWrite()methods still return their respective errors (io.ErrClosedPipe/errWriteClosed) when called on a closed wrapper, since those represent actual misuse.Per RFC 6455 Section 7.1.1, a close frame should only be sent once. The underlying issue was that the error from the redundant
Close()call was being surfaced or logged, making it appear something was wrong when it was actually expected behavior.Tests added for double-close scenarios on both read and write wrappers.