From c189278e246445a99ce6c9c1bef647e27f33b842 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Wed, 22 Feb 2017 16:29:01 -0500 Subject: [PATCH] recwatch: Unblock from sending on exit If we receive an exit signal while we are waiting to send a message, we should still be able to shut down. --- recwatch/recwatch.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/recwatch/recwatch.go b/recwatch/recwatch.go index 8029d5ec..0357252e 100644 --- a/recwatch/recwatch.go +++ b/recwatch/recwatch.go @@ -95,7 +95,11 @@ func (obj *RecWatcher) Init() error { // immediately, this can send after closed which panics! obj.mutex.Lock() if !obj.closed { - obj.events <- Event{Error: err} + select { + case obj.events <- Event{Error: err}: + case <-obj.exit: + // pass + } } obj.mutex.Unlock() } @@ -119,10 +123,6 @@ func (obj *RecWatcher) Close() error { if obj.watcher != nil { err = obj.watcher.Close() obj.watcher = nil - // TODO: should we send the close error? - //if err != nil { - // obj.events <- Event{Error: err} - //} } obj.mutex.Lock() // FIXME: I don't think this mutex is needed anymore... obj.closed = true @@ -280,7 +280,12 @@ func (obj *RecWatcher) Watch() error { if send { send = false // only invalid state on certain types of events - obj.events <- Event{Error: nil, Body: &event} + select { + // exit even when we're blocked on event sending + case obj.events <- Event{Error: nil, Body: &event}: + case <-obj.exit: + return fmt.Errorf("pending event not sent") + } } case err := <-obj.watcher.Errors: