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: fixup gRPC buffer overflow bug with unit test. #3376

Merged
merged 3 commits into from
Feb 21, 2025

Conversation

aaron-kasten
Copy link
Contributor

@aaron-kasten aaron-kasten commented Feb 21, 2025

Change Overview

gRPC has a over-the-wire buffer limit of 4MB. gRPC buffer overflow errors can occur if this figure is use as a before the wire buffer measure as gRPC adds overhead (JSON overhead) when sending messages from gRPC API clients.

This fix just divides the gRPC buffer limit in half (2MB) which is much smaller than the theoretical max (theoretical_max = 4MB - protocol overhead) but intuitively large enough to hold enough data.

This also includes a fix for using bytes.Buffer as a circular buffer, which its not very good at. Instead a []byte buffer is used. If performance is a concern then the buffer copy should be split across 2 threads in a bucket brigade.

Pull request type

Please check the type of change your PR introduces:

  • 🐛 Bugfix

Issues

There is no github issue for this fix.

Test Plan

  • ⚡ Unit test

@@ -35,7 +35,7 @@ func tmpDir(c *C) string {
// limit the size of the address while retaining some of the properties
// of the original directory name
hs := adler32.Checksum([]byte(c.TestName()))
d, err := os.MkdirTemp("", fmt.Sprintf("%08x%.20s", hs, c.TestName()))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change to make these temp files easier to find. Having the filename hash first can be really confusing when using ls and an alphabetical sort.

t := time.NewTicker(s.tailTickDuration)
for {
n, err := buf.ReadFrom(fh)
Copy link
Contributor Author

@aaron-kasten aaron-kasten Feb 21, 2025

Choose a reason for hiding this comment

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

Fixup misuse of bytes.Buffer: bytes.Buffer will grow to size of write regardless of backing buffer capacity.

@@ -219,7 +218,7 @@ func (s *processServiceServer) streamOutput(ss sender, p *process, fh *os.File)
<-t.C
continue
}
o := &Output{Output: buf.String()}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Performs an extra allocation for the string object which may add measurable overhead considering how large the buffer is (2MB). May want to convert this code to a 2 thread bucket brigade at some point.

@@ -449,3 +449,37 @@ func (s *KanXSuite) TestCreateProcess_Exit2(c *C) {
c.Assert(p0.GetExitErr(), Equals, "signal: killed")
c.Assert(p0.GetExitCode(), Equals, int64(-1))
}

func (s *KanXSuite) TestCreateProcess_BufferOverflow_1(c *C) {
Copy link
Contributor Author

@aaron-kasten aaron-kasten Feb 21, 2025

Choose a reason for hiding this comment

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

Test that buffer does not overflow.

@aaron-kasten aaron-kasten force-pushed the fixups-and-bugfixes-C branch 2 times, most recently from 5e65328 to 3073c38 Compare February 21, 2025 20:07
@aaron-kasten aaron-kasten force-pushed the fixups-and-bugfixes-C branch 4 times, most recently from c4b07b6 to 91b53a4 Compare February 21, 2025 22:43
Signed-off-by: Aaron Alpar <[email protected]>
Signed-off-by: Aaron Alpar <[email protected]>
@mergify mergify bot merged commit 3a0bba4 into kanisterio:master Feb 21, 2025
21 checks passed
aaron-kasten added a commit to aaron-kasten/kanister that referenced this pull request Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants