Skip to content

Commit 03379d0

Browse files
authored
fix(pkg/board): consume zombie process in adb (#81)
* fix(pkg/board): wait for zombie process in adb * make linter happy * user a better way to handler wait on close
1 parent 4d105dd commit 03379d0

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

pkg/board/remote/adb/adb.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ func (a *ADBConnection) List(path string) ([]remote.FileInfo, error) {
120120
if err := cmd.Start(); err != nil {
121121
return nil, err
122122
}
123+
defer func() { _ = cmd.Wait() }()
123124

124125
r := bufio.NewReader(output)
125126
_, err = r.ReadBytes('\n') // Skip the first line
@@ -167,6 +168,7 @@ func (a *ADBConnection) Stats(p string) (remote.FileInfo, error) {
167168
if err := cmd.Start(); err != nil {
168169
return remote.FileInfo{}, err
169170
}
171+
defer func() { _ = cmd.Wait() }()
170172

171173
r := bufio.NewReader(output)
172174
line, err := r.ReadBytes('\n')

pkg/board/remote/adb/adb_nowindows.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
package adb
1919

2020
import (
21+
"cmp"
2122
"context"
2223
"fmt"
2324
"io"
2425

2526
"github.com/arduino/go-paths-helper"
27+
28+
"github.com/arduino/arduino-app-cli/pkg/board/remote"
2629
)
2730

2831
func adbReadFile(a *ADBConnection, path string) (io.ReadCloser, error) {
@@ -37,7 +40,14 @@ func adbReadFile(a *ADBConnection, path string) (io.ReadCloser, error) {
3740
if err := cmd.Start(); err != nil {
3841
return nil, err
3942
}
40-
return output, nil
43+
return remote.WithCloser{
44+
Reader: output,
45+
CloseFun: func() error {
46+
err1 := output.Close()
47+
err2 := cmd.Wait()
48+
return cmp.Or(err1, err2)
49+
},
50+
}, nil
4151
}
4252

4353
func adbWriteFile(a *ADBConnection, r io.Reader, pathStr string) error {

pkg/board/remote/adb/adb_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
"github.com/arduino/go-paths-helper"
2828

29-
"github.com/arduino/arduino-app-cli/pkg/board/remote/ssh"
29+
"github.com/arduino/arduino-app-cli/pkg/board/remote"
3030
)
3131

3232
func adbReadFile(a *ADBConnection, path string) (io.ReadCloser, error) {
@@ -44,7 +44,7 @@ func adbReadFile(a *ADBConnection, path string) (io.ReadCloser, error) {
4444
return nil, err
4545
}
4646

47-
return ssh.WithCloser{
47+
return remote.WithCloser{
4848
Reader: decoded,
4949
CloseFun: func() error {
5050
err1 := output.Close()

pkg/board/remote/remote.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,17 @@ type Cmder interface {
5959
Output(ctx context.Context) ([]byte, error)
6060
Interactive() (io.WriteCloser, io.Reader, io.Reader, Closer, error)
6161
}
62+
63+
// WithCloser is a helper to create an io.ReadCloser from an io.Reader
64+
// and a close function.
65+
type WithCloser struct {
66+
io.Reader
67+
CloseFun func() error
68+
}
69+
70+
func (w WithCloser) Close() error {
71+
if w.CloseFun != nil {
72+
return w.CloseFun()
73+
}
74+
return nil
75+
}

pkg/board/remote/ssh/ssh.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,6 @@ func (a *SSHConnection) WriteFile(r io.Reader, path string) error {
219219
return nil
220220
}
221221

222-
type WithCloser struct {
223-
io.Reader
224-
CloseFun func() error
225-
}
226-
227-
func (w WithCloser) Close() error {
228-
if w.CloseFun != nil {
229-
return w.CloseFun()
230-
}
231-
return nil
232-
}
233-
234222
func (a *SSHConnection) ReadFile(path string) (io.ReadCloser, error) {
235223
session, err := a.client.NewSession()
236224
if err != nil {
@@ -247,7 +235,7 @@ func (a *SSHConnection) ReadFile(path string) (io.ReadCloser, error) {
247235
return nil, fmt.Errorf("failed to start command: %w", err)
248236
}
249237

250-
return WithCloser{
238+
return remote.WithCloser{
251239
Reader: output,
252240
CloseFun: session.Close,
253241
}, nil

0 commit comments

Comments
 (0)