Skip to content

Commit 93ac7a2

Browse files
committed
Fixed leaking pipes in Discovery command creation
Moved the process creation at the very last moment, since StdInPipe and StdOutPipe methods actually creates a OS pipe even if the process is not started later.
1 parent ed394ee commit 93ac7a2

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

arduino/discovery/discovery.go

+24-20
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const (
4747
// with the boards.
4848
type PluggableDiscovery struct {
4949
id string
50+
processArgs []string
5051
process *executils.Process
5152
outgoingCommandsPipe io.Writer
5253
incomingMessagesChan <-chan *discoveryMessage
@@ -126,28 +127,12 @@ type Event struct {
126127

127128
// New create and connect to the given pluggable discovery
128129
func New(id string, args ...string) (*PluggableDiscovery, error) {
129-
proc, err := executils.NewProcess(args...)
130-
if err != nil {
131-
return nil, err
132-
}
133-
stdout, err := proc.StdoutPipe()
134-
if err != nil {
135-
return nil, err
136-
}
137-
stdin, err := proc.StdinPipe()
138-
if err != nil {
139-
return nil, err
140-
}
141-
messageChan := make(chan *discoveryMessage)
142130
disc := &PluggableDiscovery{
143-
id: id,
144-
process: proc,
145-
incomingMessagesChan: messageChan,
146-
outgoingCommandsPipe: stdin,
147-
state: Dead,
148-
cachedPorts: map[string]*Port{},
131+
id: id,
132+
processArgs: args,
133+
state: Dead,
134+
cachedPorts: map[string]*Port{},
149135
}
150-
go disc.jsonDecodeLoop(stdout, messageChan)
151136
return disc, nil
152137
}
153138

@@ -249,6 +234,25 @@ func (disc *PluggableDiscovery) sendCommand(command string) error {
249234

250235
func (disc *PluggableDiscovery) runProcess() error {
251236
logrus.Infof("starting discovery %s process", disc.id)
237+
proc, err := executils.NewProcess(disc.processArgs...)
238+
if err != nil {
239+
return err
240+
}
241+
stdout, err := proc.StdoutPipe()
242+
if err != nil {
243+
return err
244+
}
245+
stdin, err := proc.StdinPipe()
246+
if err != nil {
247+
return err
248+
}
249+
disc.outgoingCommandsPipe = stdin
250+
disc.process = proc
251+
252+
messageChan := make(chan *discoveryMessage)
253+
disc.incomingMessagesChan = messageChan
254+
go disc.jsonDecodeLoop(stdout, messageChan)
255+
252256
if err := disc.process.Start(); err != nil {
253257
return err
254258
}

0 commit comments

Comments
 (0)