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

Fix a potential bug in DropCloserReadHalf::take() #605

Conversation

steedmicro
Copy link
Contributor

@steedmicro steedmicro commented Jan 12, 2024

Description

I fixed a potential bug in DropCloserReadHalf::take() function.
I mainly applied @allada's recommendations.
Though, I kept the previous second_chunk logic because we need to check second_chunk is empty or not in order to return first_chunk without copying stuff.
Detailed context can be found in the issue below.

Fixes #578

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • Updated documentation if needed
  • Tests added/amended
  • bazel test //... passes locally
  • PR is contained in a single commit, using git amend see some docs

This change is Reviewable

Copy link

vercel bot commented Jan 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nativelink-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 12, 2024 1:07pm

Copy link
Member

@allada allada left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 1 LGTMs obtained


nativelink-util/src/buf_channel.rs line 321 at r1 (raw file):

            // 3a. If our `first_chunk` is EOF, we are done.
            // 3b. If our `first_chunk` is exactly `size` we can return our current state.
            if first_chunk.is_empty() || first_chunk.len() >= size {

I don't think this part is correct.

We need to receive the EOF, and if the first chunk returns exactly the number of bytes we won't receive our EOF.

If first_chunk.is_empty() is true this is fine.
If self.partial.is_some() is true, it is ok to go down this path but we should assert first_chunk.len() == size.

Id rewrite this as:

// 3a. If our `first_chunk` is EOF, we are done.
if first_chunk.is_empty() {
  return Ok(first_chunk);
}

// 3b. If our first_chunk has data and it our self.partial was filled it means our stream has more data.
if self.partial.is_some() {
  assert!(first_chunk.len() == size, "Length should be exactly size here");
  return Ok(first_chunk);
}

nativelink-util/src/buf_channel.rs line 364 at r1 (raw file):

            output.put(chunk);

            if output.len() == size {

We can only do this if output.len() == size && self.partial.is_some()


nativelink-util/src/buf_channel.rs line 367 at r1 (raw file):

                return Ok(output.freeze());
            }
            assert!(output.len() < size, "Length should never be larger than size in take()");

nit: can needs to be <=


nativelink-util/src/buf_channel.rs line 369 at r1 (raw file):

            assert!(output.len() < size, "Length should never be larger than size in take()");
            assert!(
                self.partial.is_none(),

nit: We can remove this, it's checked at the beginning of the loop.

@steedmicro steedmicro closed this Jan 12, 2024
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.

2 participants