Skip to content

Commit 2d5f041

Browse files
laurazardthaJeztah
andcommitted
commandconn: return original error while closing
Changes the `Read` and `Write` error handling logic to return the original error while closing the connection. We still skip calling `handleEOF` if already closing the connection. Fixes the flaky `TestCloseWhileWriting` and `TestCloseWhileReading` tests. Co-authored-by: Sebastiaan van Stijn <[email protected]> Signed-off-by: Laura Brehm <[email protected]> (cherry picked from commit d5f564a) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 520e360 commit 2d5f041

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

cli/connhelper/commandconn/commandconn.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,8 @@ func (c *commandConn) Read(p []byte) (int, error) {
163163
// Close might get called
164164
if c.closing.Load() {
165165
// If we're currently closing the connection
166-
// we don't want to call onEOF, but we do want
167-
// to return an io.EOF
168-
return 0, io.EOF
166+
// we don't want to call onEOF
167+
return n, err
169168
}
170169

171170
return n, c.handleEOF(err)
@@ -178,9 +177,8 @@ func (c *commandConn) Write(p []byte) (int, error) {
178177
// Close might get called
179178
if c.closing.Load() {
180179
// If we're currently closing the connection
181-
// we don't want to call onEOF, but we do want
182-
// to return an io.EOF
183-
return 0, io.EOF
180+
// we don't want to call onEOF
181+
return n, err
184182
}
185183

186184
return n, c.handleEOF(err)

cli/connhelper/commandconn/commandconn_unix_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package commandconn
66
import (
77
"context"
88
"io"
9+
"io/fs"
910
"testing"
1011
"time"
1112

@@ -179,7 +180,8 @@ func TestCloseWhileWriting(t *testing.T) {
179180
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
180181

181182
writeErr := <-writeErrC
182-
assert.ErrorContains(t, writeErr, "EOF")
183+
assert.ErrorContains(t, writeErr, "file already closed")
184+
assert.Check(t, is.ErrorIs(writeErr, fs.ErrClosed))
183185
}
184186

185187
func TestCloseWhileReading(t *testing.T) {
@@ -209,5 +211,5 @@ func TestCloseWhileReading(t *testing.T) {
209211
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
210212

211213
readErr := <-readErrC
212-
assert.ErrorContains(t, readErr, "EOF")
214+
assert.Check(t, is.ErrorIs(readErr, fs.ErrClosed))
213215
}

0 commit comments

Comments
 (0)