Skip to content

Commit ce30d9a

Browse files
authored
Merge pull request #26 from coder/spike/proc-close
Fix proc.Close() when there are no errors
2 parents 07fb561 + dde61b3 commit ce30d9a

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,18 @@ func (r remoteProcess) Close() error {
266266

267267
func joinErrs(errs ...error) error {
268268
var str string
269+
foundErr := false
269270
for _, e := range errs {
270271
if e != nil {
272+
foundErr = true
271273
if str != "" {
272274
str += ", "
273275
}
274276
str += e.Error()
275277
}
276278
}
277-
return xerrors.New(str)
279+
if foundErr {
280+
return xerrors.New(str)
281+
}
282+
return nil
278283
}

client_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"cdr.dev/slog/sloggers/slogtest/assert"
1616
"cdr.dev/wsep/internal/proto"
1717
"github.com/google/go-cmp/cmp"
18+
"golang.org/x/xerrors"
1819
"nhooyr.io/websocket"
1920
)
2021

@@ -84,6 +85,46 @@ func TestRemoteExec(t *testing.T) {
8485
testExecer(ctx, t, execer)
8586
}
8687

88+
func TestRemoveClose(t *testing.T) {
89+
t.Parallel()
90+
91+
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
92+
defer cancel()
93+
94+
ws, server := mockConn(ctx, t, nil)
95+
defer server.Close()
96+
97+
execer := RemoteExecer(ws)
98+
cmd := Command{
99+
Command: "/bin/bash",
100+
TTY: true,
101+
Stdin: true,
102+
Env: []string{"TERM=linux"},
103+
}
104+
105+
proc, err := execer.Start(ctx, cmd)
106+
assert.Success(t, "execer Start", err)
107+
108+
i := proc.Stdin()
109+
o := proc.Stdout()
110+
buf := make([]byte, 2048)
111+
_, err = i.Write([]byte("echo foo"))
112+
assert.Success(t, "echo", err)
113+
bldr := strings.Builder{}
114+
for !strings.Contains(bldr.String(), "foo") {
115+
n, err := o.Read(buf)
116+
if xerrors.Is(err, io.EOF) {
117+
break
118+
}
119+
assert.Success(t, "read", err)
120+
_, err = bldr.Write(buf[:n])
121+
assert.Success(t, "write to builder", err)
122+
}
123+
err = proc.Close()
124+
assert.Success(t, "close proc", err)
125+
// note that proc.Close() also closes the websocket.
126+
}
127+
87128
func TestRemoteExecFail(t *testing.T) {
88129
t.Parallel()
89130
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)

0 commit comments

Comments
 (0)