From 82de0f5ad450f3add2a8e7674cf5e019af609a66 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 3 Sep 2015 16:57:24 -0600 Subject: The comment at the bottom of hackers_watch.go was wrong; fix the race. The actual determinant was this race main worker ------------------- Close() Read() Exit() If Read() returned between when Close() happened and when Exit() happened, then the code ran. It doesn't *really* matter if the code runs, but for predictability, set up a wait group to have Close() block until the worker exits. --- src/nshd/hackers_git/hackers.go | 8 +++++++- src/nshd/hackers_git/hackers_watch.go | 6 +----- 2 files changed, 8 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/nshd/hackers_git/hackers.go b/src/nshd/hackers_git/hackers.go index b1fffc6..ecbda7a 100644 --- a/src/nshd/hackers_git/hackers.go +++ b/src/nshd/hackers_git/hackers.go @@ -23,6 +23,7 @@ type Hackers struct { util.NullBackend cfg Config lock sync.RWMutex + workers sync.WaitGroup users map[int32]user passwords map[int32]string @@ -44,11 +45,16 @@ func NewHackers(config Config) *Hackers { if err != nil { return nil } - go o.worker() + o.workers.Add(1) + go func() { + defer o.workers.Done() + o.worker() + }() return &o } func (o *Hackers) Close() { + defer o.workers.Wait() logger.Info("Closing hackers.git session") o.lock.Lock() defer o.lock.Unlock() diff --git a/src/nshd/hackers_git/hackers_watch.go b/src/nshd/hackers_git/hackers_watch.go index 87c856d..9592dab 100644 --- a/src/nshd/hackers_git/hackers_watch.go +++ b/src/nshd/hackers_git/hackers_watch.go @@ -181,9 +181,5 @@ func (o *Hackers) worker() { } } } - // This happens only sometimes--depending on if the main - // goroutine or this goroutine is killed by os.Exit first; - // this happens if the main goroutine calls inotify.Close() - // before this groutine is killed. - logger.Info("Stopping hackers.git inotify watcher") + logger.Info("Stopped hackers.git inotify watcher") } -- cgit v1.2.3-54-g00ecf