Skip to content

Commit f6a7dc9

Browse files
authored
address memory leak (#57)
* wip * rm sleep, cleanup subs * pipes * oops * sigh * rip * piping * oops
1 parent e5c0820 commit f6a7dc9

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

kbchat/kbchat.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"io/ioutil"
1010
"log"
11+
"os"
1112
"os/exec"
1213
"sync"
1314
"time"
@@ -34,12 +35,14 @@ func getUsername(runOpts RunOptions) (username string, err error) {
3435
if err != nil {
3536
return "", err
3637
}
38+
p.ExtraFiles = []*os.File{output.(*os.File)}
3739
if err = p.Start(); err != nil {
3840
return "", err
3941
}
4042

4143
doneCh := make(chan error)
4244
go func() {
45+
defer func() { close(doneCh) }()
4346
statusJSON, err := ioutil.ReadAll(output)
4447
if err != nil {
4548
doneCh <- fmt.Errorf("error reading whoami output: %v", err)
@@ -56,6 +59,10 @@ func getUsername(runOpts RunOptions) (username string, err error) {
5659
} else {
5760
doneCh <- fmt.Errorf("unable to authenticate to keybase service: logged in: %v user: %+v", status.LoggedIn, status.User)
5861
}
62+
// Cleanup the command
63+
if err := p.Wait(); err != nil {
64+
log.Printf("unable to wait for cmd: %v", err)
65+
}
5966
}()
6067

6168
select {
@@ -179,6 +186,7 @@ func (a *API) startPipes() (err error) {
179186
if err != nil {
180187
return err
181188
}
189+
a.apiCmd.ExtraFiles = []*os.File{output.(*os.File)}
182190
if err := a.apiCmd.Start(); err != nil {
183191
return err
184192
}
@@ -363,7 +371,14 @@ func (a *API) Listen(opts ListenOptions) (*NewSubscription, error) {
363371
a.registerSubscription(sub)
364372
pause := 2 * time.Second
365373
readScanner := func(boutput *bufio.Scanner) {
374+
defer func() { done <- struct{}{} }()
366375
for {
376+
select {
377+
case <-shutdownCh:
378+
log.Printf("readScanner: received shutdown")
379+
return
380+
default:
381+
}
367382
boutput.Scan()
368383
t := boutput.Text()
369384
var typeHolder TypeHolder
@@ -416,12 +431,17 @@ func (a *API) Listen(opts ListenOptions) (*NewSubscription, error) {
416431
continue
417432
}
418433
}
419-
done <- struct{}{}
420434
}
421435

422436
attempts := 0
423437
maxAttempts := 1800
424438
go func() {
439+
defer func() {
440+
close(newMsgsCh)
441+
close(newConvsCh)
442+
close(newWalletCh)
443+
close(errorCh)
444+
}()
425445
for {
426446
select {
427447
case <-shutdownCh:
@@ -462,8 +482,10 @@ func (a *API) Listen(opts ListenOptions) (*NewSubscription, error) {
462482
time.Sleep(pause)
463483
continue
464484
}
485+
p.ExtraFiles = []*os.File{stderr.(*os.File), output.(*os.File)}
465486
boutput := bufio.NewScanner(output)
466487
if err := p.Start(); err != nil {
488+
467489
log.Printf("Listen: failed to make listen scanner: %s", err)
468490
time.Sleep(pause)
469491
continue
@@ -511,6 +533,11 @@ func (a *API) Shutdown() error {
511533
for _, sub := range a.subscriptions {
512534
sub.Shutdown()
513535
}
536+
if a.apiCmd != nil {
537+
if err := a.apiCmd.Wait(); err != nil {
538+
return err
539+
}
540+
}
514541

515542
if a.runOpts.Oneshot != nil {
516543
err := a.runOpts.Command("logout", "--force").Run()

0 commit comments

Comments
 (0)