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 <github@gone.nl>
Signed-off-by: Laura Brehm <laurabrehm@hey.com>
(cherry picked from commit d5f564adaa)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Laura Brehm 2023-06-30 15:03:19 +01:00 committed by Sebastiaan van Stijn
parent 520e3600ee
commit 2d5f041bde
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 8 additions and 8 deletions

View File

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

View File

@ -6,6 +6,7 @@ package commandconn
import ( import (
"context" "context"
"io" "io"
"io/fs"
"testing" "testing"
"time" "time"
@ -179,7 +180,8 @@ func TestCloseWhileWriting(t *testing.T) {
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid)) assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
writeErr := <-writeErrC writeErr := <-writeErrC
assert.ErrorContains(t, writeErr, "EOF") assert.ErrorContains(t, writeErr, "file already closed")
assert.Check(t, is.ErrorIs(writeErr, fs.ErrClosed))
} }
func TestCloseWhileReading(t *testing.T) { func TestCloseWhileReading(t *testing.T) {
@ -209,5 +211,5 @@ func TestCloseWhileReading(t *testing.T) {
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid)) assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
readErr := <-readErrC readErr := <-readErrC
assert.ErrorContains(t, readErr, "EOF") assert.Check(t, is.ErrorIs(readErr, fs.ErrClosed))
} }