Skip to content

Commit 71ce1a4

Browse files
authored
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 56bfccd commit 71ce1a4

File tree

4 files changed

+66
-61
lines changed

4 files changed

+66
-61
lines changed

arduino/discovery/discovery.go

Lines changed: 32 additions & 27 deletions
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

arduino/discovery/discoverymanager/discoverymanager.go

Lines changed: 1 addition & 1 deletion
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
}

i18n/data/en.po

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ msgstr "%[1]s folder is no longer supported! See %[2]s for more information"
1313
msgid "%[1]s is required but %[2]s is currently installed."
1414
msgstr "%[1]s is required but %[2]s is currently installed."
1515

16-
#: arduino/discovery/discovery.go:74
16+
#: arduino/discovery/discovery.go:75
1717
msgid "%[1]s, message: %[2]s"
1818
msgstr "%[1]s, message: %[2]s"
1919

20-
#: arduino/discovery/discovery.go:83
20+
#: arduino/discovery/discovery.go:84
2121
msgid "%[1]s, port: %[2]s"
2222
msgstr "%[1]s, port: %[2]s"
2323

24-
#: arduino/discovery/discovery.go:80
24+
#: arduino/discovery/discovery.go:81
2525
msgid "%[1]s, ports: %[2]s"
2626
msgstr "%[1]s, ports: %[2]s"
2727

28-
#: arduino/discovery/discovery.go:77
28+
#: arduino/discovery/discovery.go:78
2929
msgid "%[1]s, protocol version: %[2]d"
3030
msgstr "%[1]s, protocol version: %[2]d"
3131

@@ -2388,12 +2388,12 @@ msgstr "board not found"
23882388
msgid "boardname"
23892389
msgstr "boardname"
23902390

2391-
#: arduino/discovery/discovery.go:300
2392-
#: arduino/discovery/discovery.go:321
2393-
#: arduino/discovery/discovery.go:341
2394-
#: arduino/discovery/discovery.go:364
2395-
#: arduino/discovery/discovery.go:387
2396-
#: arduino/discovery/discovery.go:410
2391+
#: arduino/discovery/discovery.go:311
2392+
#: arduino/discovery/discovery.go:332
2393+
#: arduino/discovery/discovery.go:352
2394+
#: arduino/discovery/discovery.go:375
2395+
#: arduino/discovery/discovery.go:392
2396+
#: arduino/discovery/discovery.go:415
23972397
#: arduino/monitor/monitor.go:246
23982398
msgid "calling %[1]s: %[2]w"
23992399
msgstr "calling %[1]s: %[2]w"
@@ -2441,12 +2441,12 @@ msgstr "cleaning build path"
24412441
msgid "command"
24422442
msgstr "command"
24432443

2444-
#: arduino/discovery/discovery.go:304
2445-
#: arduino/discovery/discovery.go:325
2446-
#: arduino/discovery/discovery.go:345
2447-
#: arduino/discovery/discovery.go:368
2448-
#: arduino/discovery/discovery.go:391
2449-
#: arduino/discovery/discovery.go:414
2444+
#: arduino/discovery/discovery.go:315
2445+
#: arduino/discovery/discovery.go:336
2446+
#: arduino/discovery/discovery.go:356
2447+
#: arduino/discovery/discovery.go:379
2448+
#: arduino/discovery/discovery.go:396
2449+
#: arduino/discovery/discovery.go:419
24502450
#: arduino/monitor/monitor.go:250
24512451
#: arduino/monitor/monitor.go:270
24522452
#: arduino/monitor/monitor.go:287
@@ -2467,32 +2467,32 @@ msgstr "communication out of sync, expected 'configure', received '%s'"
24672467
msgid "communication out of sync, expected 'describe', received '%s'"
24682468
msgstr "communication out of sync, expected 'describe', received '%s'"
24692469

2470-
#: arduino/discovery/discovery.go:302
2470+
#: arduino/discovery/discovery.go:313
24712471
#: arduino/monitor/monitor.go:248
24722472
msgid "communication out of sync, expected 'hello', received '%s'"
24732473
msgstr "communication out of sync, expected 'hello', received '%s'"
24742474

2475-
#: arduino/discovery/discovery.go:389
2475+
#: arduino/discovery/discovery.go:394
24762476
msgid "communication out of sync, expected 'list', received '%s'"
24772477
msgstr "communication out of sync, expected 'list', received '%s'"
24782478

24792479
#: arduino/monitor/monitor.go:321
24802480
msgid "communication out of sync, expected 'open', received '%s'"
24812481
msgstr "communication out of sync, expected 'open', received '%s'"
24822482

2483-
#: arduino/discovery/discovery.go:366
2483+
#: arduino/discovery/discovery.go:377
24842484
msgid "communication out of sync, expected 'quit', received '%s'"
24852485
msgstr "communication out of sync, expected 'quit', received '%s'"
24862486

2487-
#: arduino/discovery/discovery.go:323
2487+
#: arduino/discovery/discovery.go:334
24882488
msgid "communication out of sync, expected 'start', received '%s'"
24892489
msgstr "communication out of sync, expected 'start', received '%s'"
24902490

2491-
#: arduino/discovery/discovery.go:412
2491+
#: arduino/discovery/discovery.go:417
24922492
msgid "communication out of sync, expected 'start_sync', received '%s'"
24932493
msgstr "communication out of sync, expected 'start_sync', received '%s'"
24942494

2495-
#: arduino/discovery/discovery.go:343
2495+
#: arduino/discovery/discovery.go:354
24962496
msgid "communication out of sync, expected 'stop', received '%s'"
24972497
msgstr "communication out of sync, expected 'stop', received '%s'"
24982498

@@ -2706,11 +2706,11 @@ msgstr "installing %[1]s tool: %[2]s"
27062706
msgid "installing platform %[1]s: %[2]s"
27072707
msgstr "installing platform %[1]s: %[2]s"
27082708

2709-
#: arduino/discovery/discovery.go:190
2709+
#: arduino/discovery/discovery.go:175
27102710
msgid "invalid 'add' message: missing port"
27112711
msgstr "invalid 'add' message: missing port"
27122712

2713-
#: arduino/discovery/discovery.go:201
2713+
#: arduino/discovery/discovery.go:186
27142714
msgid "invalid 'remove' message: missing port"
27152715
msgstr "invalid 'remove' message: missing port"
27162716

@@ -3050,7 +3050,7 @@ msgstr "port"
30503050
msgid "port not found: %[1]s %[2]s"
30513051
msgstr "port not found: %[1]s %[2]s"
30523052

3053-
#: arduino/discovery/discovery.go:306
3053+
#: arduino/discovery/discovery.go:317
30543054
#: arduino/monitor/monitor.go:252
30553055
msgid "protocol version not supported: requested 1, got %d"
30563056
msgstr "protocol version not supported: requested 1, got %d"
@@ -3233,7 +3233,7 @@ msgstr "the platform has no releases"
32333233
msgid "the server responded with status %s"
32343234
msgstr "the server responded with status %s"
32353235

3236-
#: arduino/discovery/discovery.go:231
3236+
#: arduino/discovery/discovery.go:216
32373237
msgid "timeout waiting for message from %s"
32383238
msgstr "timeout waiting for message from %s"
32393239

i18n/rice-box.go

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)