-
Notifications
You must be signed in to change notification settings - Fork 2k
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 race in StreamFramer and truncation in api/AllocFS.Logs #4234
Merged
+237
−81
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a8e1f35
tests: test logs from client<->api package
schmichael 41f05dc
api: never return EOF from Logs error chan
schmichael a7c71c1
client: reset encoders between uses
schmichael 564854d
client: give pipe conns meaningful names
schmichael 63dad44
client: ensure cancel is always called when func exits
schmichael cafcb89
client: don't spin on read errors
schmichael aad596b
client: squelch errors on cleanly closed pipes
schmichael 8776cf8
client: use a bytes.Reader for reading a []byte
schmichael 361db26
framer: fix race and remove unused error var
schmichael 6858c52
framer: fix early exit/truncation in framer
schmichael File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -294,6 +294,7 @@ OUTER: | |
streamErr = err | ||
break OUTER | ||
} | ||
encoder.Reset(conn) | ||
case <-ctx.Done(): | ||
break OUTER | ||
} | ||
|
@@ -405,8 +406,6 @@ func (f *FileSystem) logs(conn io.ReadWriteCloser) { | |
|
||
frames := make(chan *sframer.StreamFrame, streamFramesBuffer) | ||
errCh := make(chan error) | ||
var buf bytes.Buffer | ||
frameCodec := codec.NewEncoder(&buf, structs.JsonHandle) | ||
|
||
// Start streaming | ||
go func() { | ||
|
@@ -423,20 +422,23 @@ func (f *FileSystem) logs(conn io.ReadWriteCloser) { | |
go func() { | ||
for { | ||
if _, err := conn.Read(nil); err != nil { | ||
if err == io.EOF { | ||
if err == io.EOF || err == io.ErrClosedPipe { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done here 51440e2 |
||
// One end of the pipe was explicitly closed, exit cleanly | ||
cancel() | ||
return | ||
} | ||
select { | ||
case errCh <- err: | ||
case <-ctx.Done(): | ||
return | ||
} | ||
return | ||
} | ||
} | ||
}() | ||
|
||
var streamErr error | ||
buf := new(bytes.Buffer) | ||
frameCodec := codec.NewEncoder(buf, structs.JsonHandle) | ||
OUTER: | ||
for { | ||
select { | ||
|
@@ -455,6 +457,7 @@ OUTER: | |
streamErr = err | ||
break OUTER | ||
} | ||
frameCodec.Reset(buf) | ||
|
||
resp.Payload = buf.Bytes() | ||
buf.Reset() | ||
|
@@ -464,6 +467,7 @@ OUTER: | |
streamErr = err | ||
break OUTER | ||
} | ||
encoder.Reset(conn) | ||
} | ||
} | ||
|
||
|
@@ -576,12 +580,7 @@ func (f *FileSystem) logsImpl(ctx context.Context, follow, plain bool, offset in | |
// #3342 | ||
select { | ||
case <-framer.ExitCh(): | ||
err := parseFramerErr(framer.Err()) | ||
if err == syscall.EPIPE { | ||
// EPIPE just means the connection was closed | ||
return nil | ||
} | ||
return err | ||
return nil | ||
default: | ||
} | ||
|
||
|
@@ -705,7 +704,7 @@ OUTER: | |
lastEvent = truncateEvent | ||
continue OUTER | ||
case <-framer.ExitCh(): | ||
return parseFramerErr(framer.Err()) | ||
return nil | ||
case <-ctx.Done(): | ||
return nil | ||
case err, ok := <-eofCancelCh: | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't makeClient already do this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't! This
makeClient
helper is very strange and all of the tests inapi/
that expect a client node seem to be commented out! For example: https://github.com/hashicorp/nomad/blob/master/api/allocations_test.go#L33-L59I'm honestly not sure what the right approach is here. Either we need to improve this test client or we need to remove it and use one of the other test client helpers we have.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That test client just serializes a config and starts the agent with it. By default it only creates the server. You may want to consider just using a dev agent.