Skip to content

Commit 59f748d

Browse files
committed
Fixed some resource leaks leading to a "too many open files" error (#1465)
* Fixed leaking pipes in Discovery command creation Moved the process creation at the very last moment, since StdInPipe and StdOutPipe methods actually create an OS pipe even if the process is not started later. * When a discovery is quitted ensure that the process gets Wait-ed Otherwise, the resources allocated by the process will be leaked. (pipes, zombie process, etc.) * fix i18n
1 parent 6b69e27 commit 59f748d

File tree

4 files changed

+63
-58
lines changed

4 files changed

+63
-58
lines changed

Diff for: arduino/discovery/discovery.go

+32-27
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
}
@@ -264,8 +268,15 @@ func (disc *PluggableDiscovery) killProcess() error {
264268
if err := disc.process.Kill(); err != nil {
265269
return err
266270
}
271+
if err := disc.process.Wait(); err != nil {
272+
return err
273+
}
267274
disc.statusMutex.Lock()
268275
defer disc.statusMutex.Unlock()
276+
if disc.eventChan != nil {
277+
close(disc.eventChan)
278+
disc.eventChan = nil
279+
}
269280
disc.state = Dead
270281
logrus.Infof("killed discovery %s process", disc.id)
271282
return nil
@@ -367,13 +378,7 @@ func (disc *PluggableDiscovery) Quit() error {
367378
} else if msg.Message != "OK" || msg.Error {
368379
return errors.Errorf(tr("command failed: %s"), msg.Message)
369380
}
370-
disc.statusMutex.Lock()
371-
defer disc.statusMutex.Unlock()
372-
if disc.eventChan != nil {
373-
close(disc.eventChan)
374-
disc.eventChan = nil
375-
}
376-
disc.state = Dead
381+
disc.killProcess()
377382
return nil
378383
}
379384

Diff for: arduino/discovery/discoverymanager/discoverymanager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (dm *DiscoveryManager) RunAll() []error {
120120
func (dm *DiscoveryManager) StartAll() []error {
121121
return dm.parallelize(func(d *discovery.PluggableDiscovery) error {
122122
state := d.State()
123-
if state != discovery.Idling || state == discovery.Running {
123+
if state != discovery.Idling {
124124
// Already started
125125
return nil
126126
}

Diff for: i18n/data/en.po

+26-26
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ msgstr "%[1]s %[2]s Version: %[3]s Commit: %[4]s Date: %[5]s"
2121
msgid "%[1]s folder is no longer supported! See %[2]s for more information"
2222
msgstr "%[1]s folder is no longer supported! See %[2]s for more information"
2323

24-
#: arduino/discovery/discovery.go:74
24+
#: arduino/discovery/discovery.go:75
2525
msgid "%[1]s, message: %[2]s"
2626
msgstr "%[1]s, message: %[2]s"
2727

28-
#: arduino/discovery/discovery.go:83
28+
#: arduino/discovery/discovery.go:84
2929
msgid "%[1]s, port: %[2]s"
3030
msgstr "%[1]s, port: %[2]s"
3131

32-
#: arduino/discovery/discovery.go:80
32+
#: arduino/discovery/discovery.go:81
3333
msgid "%[1]s, ports: %[2]s"
3434
msgstr "%[1]s, ports: %[2]s"
3535

36-
#: arduino/discovery/discovery.go:77
36+
#: arduino/discovery/discovery.go:78
3737
msgid "%[1]s, protocol version: %[2]d"
3838
msgstr "%[1]s, protocol version: %[2]d"
3939

@@ -2430,12 +2430,12 @@ msgstr "board not found"
24302430
msgid "boardname"
24312431
msgstr "boardname"
24322432

2433-
#: arduino/discovery/discovery.go:300
2434-
#: arduino/discovery/discovery.go:321
2435-
#: arduino/discovery/discovery.go:341
2436-
#: arduino/discovery/discovery.go:364
2437-
#: arduino/discovery/discovery.go:387
2438-
#: arduino/discovery/discovery.go:410
2433+
#: arduino/discovery/discovery.go:311
2434+
#: arduino/discovery/discovery.go:332
2435+
#: arduino/discovery/discovery.go:352
2436+
#: arduino/discovery/discovery.go:375
2437+
#: arduino/discovery/discovery.go:392
2438+
#: arduino/discovery/discovery.go:415
24392439
msgid "calling %[1]s: %[2]w"
24402440
msgstr "calling %[1]s: %[2]w"
24412441

@@ -2477,36 +2477,36 @@ msgstr "cleaning build path"
24772477
msgid "command"
24782478
msgstr "command"
24792479

2480-
#: arduino/discovery/discovery.go:304
2481-
#: arduino/discovery/discovery.go:325
2482-
#: arduino/discovery/discovery.go:345
2483-
#: arduino/discovery/discovery.go:368
2484-
#: arduino/discovery/discovery.go:391
2485-
#: arduino/discovery/discovery.go:414
2480+
#: arduino/discovery/discovery.go:315
2481+
#: arduino/discovery/discovery.go:336
2482+
#: arduino/discovery/discovery.go:356
2483+
#: arduino/discovery/discovery.go:379
2484+
#: arduino/discovery/discovery.go:396
2485+
#: arduino/discovery/discovery.go:419
24862486
msgid "command failed: %s"
24872487
msgstr "command failed: %s"
24882488

2489-
#: arduino/discovery/discovery.go:302
2489+
#: arduino/discovery/discovery.go:313
24902490
msgid "communication out of sync, expected 'hello', received '%s'"
24912491
msgstr "communication out of sync, expected 'hello', received '%s'"
24922492

2493-
#: arduino/discovery/discovery.go:389
2493+
#: arduino/discovery/discovery.go:394
24942494
msgid "communication out of sync, expected 'list', received '%s'"
24952495
msgstr "communication out of sync, expected 'list', received '%s'"
24962496

2497-
#: arduino/discovery/discovery.go:366
2497+
#: arduino/discovery/discovery.go:377
24982498
msgid "communication out of sync, expected 'quit', received '%s'"
24992499
msgstr "communication out of sync, expected 'quit', received '%s'"
25002500

2501-
#: arduino/discovery/discovery.go:323
2501+
#: arduino/discovery/discovery.go:334
25022502
msgid "communication out of sync, expected 'start', received '%s'"
25032503
msgstr "communication out of sync, expected 'start', received '%s'"
25042504

2505-
#: arduino/discovery/discovery.go:412
2505+
#: arduino/discovery/discovery.go:417
25062506
msgid "communication out of sync, expected 'start_sync', received '%s'"
25072507
msgstr "communication out of sync, expected 'start_sync', received '%s'"
25082508

2509-
#: arduino/discovery/discovery.go:343
2509+
#: arduino/discovery/discovery.go:354
25102510
msgid "communication out of sync, expected 'stop', received '%s'"
25112511
msgstr "communication out of sync, expected 'stop', received '%s'"
25122512

@@ -2726,11 +2726,11 @@ msgstr "installing %[1]s tool: %[2]s"
27262726
msgid "installing platform %[1]s: %[2]s"
27272727
msgstr "installing platform %[1]s: %[2]s"
27282728

2729-
#: arduino/discovery/discovery.go:190
2729+
#: arduino/discovery/discovery.go:175
27302730
msgid "invalid 'add' message: missing port"
27312731
msgstr "invalid 'add' message: missing port"
27322732

2733-
#: arduino/discovery/discovery.go:201
2733+
#: arduino/discovery/discovery.go:186
27342734
msgid "invalid 'remove' message: missing port"
27352735
msgstr "invalid 'remove' message: missing port"
27362736

@@ -3065,7 +3065,7 @@ msgstr "port"
30653065
msgid "port not found: %[1]s %[2]s"
30663066
msgstr "port not found: %[1]s %[2]s"
30673067

3068-
#: arduino/discovery/discovery.go:306
3068+
#: arduino/discovery/discovery.go:317
30693069
msgid "protocol version not supported: requested 1, got %d"
30703070
msgstr "protocol version not supported: requested 1, got %d"
30713071

@@ -3250,7 +3250,7 @@ msgstr "the platform has no releases"
32503250
msgid "the server responded with status %s"
32513251
msgstr "the server responded with status %s"
32523252

3253-
#: arduino/discovery/discovery.go:231
3253+
#: arduino/discovery/discovery.go:216
32543254
msgid "timeout waiting for message from %s"
32553255
msgstr "timeout waiting for message from %s"
32563256

Diff for: i18n/rice-box.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)