Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid double counting in stats out when writing to nested streams #204

Merged
merged 9 commits into from
Jun 8, 2024

Conversation

nhz2
Copy link
Member

@nhz2 nhz2 commented Apr 6, 2024

Here is an example of the bug this PR fixes:

using Test
using TranscodingStreams
stream = NoopStream(NoopStream(IOBuffer()))
write(stream, b"foobar")
flush(stream)
@test TranscodingStreams.stats(stream).out == 6

Before this PR this would output 12 even though only 6 bytes were written to the underlying stream.

changemode!(stream, :write)
if marginsize(stream.buffer1) == 0 && flushbuffer(stream) == 0
return 0
Copy link
Member Author

@nhz2 nhz2 Apr 6, 2024

Choose a reason for hiding this comment

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

write will throw an error instead of returning 0 on failure.

src/stream.jl Outdated
buffer1 = stream.buffer1
p = input
p_end = p + nbytes
while p < p_end && (marginsize(buffer1) > 0 || flushbuffer(stream) > 0)
while p < p_end
marginsize(buffer1) > 0 || flush_buffer1(stream)
Copy link
Member Author

Choose a reason for hiding this comment

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

flush_buffer1 must empty buffer1 or error, so this loop should make progress.

@nhz2 nhz2 marked this pull request as ready for review April 7, 2024 00:16
@@ -595,9 +595,12 @@ function stats(stream::TranscodingStream)
out = transcoded_out - buffersize(buffer1)
elseif mode === :write
transcoded_in = buffer1.transcoded
transcoded_out = buffer2.transcoded
Copy link
Member Author

Choose a reason for hiding this comment

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

buffer2 is "owned" by the underlying stream if it is shared, so this stream shouldn't rely on buffer2.transcoding being the expected value.

@nhz2 nhz2 marked this pull request as draft April 7, 2024 00:49
@nhz2 nhz2 marked this pull request as ready for review April 9, 2024 20:01
@nhz2 nhz2 requested a review from mkitti April 9, 2024 20:01
@nhz2
Copy link
Member Author

nhz2 commented May 28, 2024

@reallyasi9 Thank you for updating ZipStreams.jl for this fix. I'll merge this PR sometime next week unless there are other concerns.

@nhz2 nhz2 merged commit d92fd8b into master Jun 8, 2024
26 checks passed
@nhz2 nhz2 deleted the nz/write-stats-fixes branch June 8, 2024 21:36
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.

None yet

2 participants