Repository navigation
lz4: stop reading the source before a concurrent Reader returns - #292
Merged
Merged
Conversation
With concurrency, Read and WriteTo took any empty block as the end of the stream and returned the error of a later block while the read-ahead was still reading the source; they now wait for the block channel to close. Reset, and WriteTo after a failed write, abandoned the read-ahead without waiting; they now wait until it no longer uses the source, so the caller may reuse it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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 #291. With
ConcurrencyOption> 1,Readercould return while its read-ahead goroutine was still insidesrc.Read, so a caller that then reused, read or closed its source raced with it.(1) Stop reading the source before a concurrent Reader returns (88fa3b4):
ReadandWriteTotreated any empty buffer from the block channel as the end of the stream and returnedErrorR(). A block that decodes to nothing is also empty, so after a later block failed they returned its error while the read loop was still reading. They now checkErrorR()only when the channel is closed, which happens after the read loop has exited; an empty block just contributes nothing.Resetabandoned the read-ahead without waiting, and so didWriteTowhen the write towfailed. A newBlocks.StopRabandons it and waits on astoppedchannel that the read loop closes once it is done with the source.Resetdocuments that it waits for a read of the previous source in progress, which with a stalled source (an idlenet.Conn, say) is as long as that read takes.TestReaderConcurrentStopsReadingSourceinreader_test.goreads from a source that blocks reads past a gate, and checks thatRead,WriteTo,WriteToto a failing writer andResetdon't return while the read-ahead is blocked there. It doesn't need-race. All four cases fail on v4 and pass here, including 200 runs with-race. Removing only the reader.go change fails the first three; removing only the wait failsResetand the failing writer.go test ./...passes with and without-tags noasm, each with and without-race;go vetandgo fix -diffare clean.FuzzReaderwith-race, which failed within seconds on v4, ran for 3 minutes clean, andFuzzReaderandFuzzFrameRoundTripran for 3 minutes each with and without-tags noasm. The public-API repro from the issue passes with-race. No performance change is claimed: the hot path gains only theokfrom the channel receive.🤖 Generated with Claude Code