From 2aff8709a534cb76c17b9cb28d98b4c2b0882a0a Mon Sep 17 00:00:00 2001 From: James Shubin Date: Sun, 5 Feb 2017 19:44:48 -0500 Subject: [PATCH] gapi: Unblock from a waiting send on GAPI close There was a race condition that would sometimes occur in that if we stopped reading from the gapiChan (on shutdown) but then a new message was available before we managed to close the GAPI, then we would wait forever to finish the close because the channel never sent, and the WaitGroup wouldn't let us exit. This fixes this horrible, horrible race. --- examples/lib/libmgmt1.go | 6 +++++- examples/lib/libmgmt2.go | 6 +++++- examples/lib/libmgmt3.go | 6 +++++- puppet/gapi.go | 7 ++++++- yamlgraph/gapi.go | 9 +++++++-- 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/examples/lib/libmgmt1.go b/examples/lib/libmgmt1.go index f0320ca9..001d5d3c 100644 --- a/examples/lib/libmgmt1.go +++ b/examples/lib/libmgmt1.go @@ -107,7 +107,11 @@ func (obj *MyGAPI) Next() chan error { select { case <-ticker.C: log.Printf("libmgmt: Generating new graph...") - ch <- nil // trigger a run + select { + case ch <- nil: // trigger a run + case <-obj.closeChan: + return + } case <-obj.closeChan: return } diff --git a/examples/lib/libmgmt2.go b/examples/lib/libmgmt2.go index c4ec2903..f40831e6 100644 --- a/examples/lib/libmgmt2.go +++ b/examples/lib/libmgmt2.go @@ -100,7 +100,11 @@ func (obj *MyGAPI) Next() chan error { select { case <-ticker.C: log.Printf("libmgmt: Generating new graph...") - ch <- nil // trigger a run + select { + case ch <- nil: // trigger a run + case <-obj.closeChan: + return + } case <-obj.closeChan: return } diff --git a/examples/lib/libmgmt3.go b/examples/lib/libmgmt3.go index 7b28aa28..f4a6e862 100644 --- a/examples/lib/libmgmt3.go +++ b/examples/lib/libmgmt3.go @@ -154,7 +154,11 @@ func (obj *MyGAPI) Next() chan error { select { case <-ticker.C: log.Printf("libmgmt: Generating new graph...") - ch <- nil // trigger a run + select { + case ch <- nil: // trigger a run + case <-obj.closeChan: + return + } case <-obj.closeChan: return } diff --git a/puppet/gapi.go b/puppet/gapi.go index 305756e2..a278e9ac 100644 --- a/puppet/gapi.go +++ b/puppet/gapi.go @@ -100,7 +100,12 @@ func (obj *GAPI) Next() chan error { } log.Printf("Puppet: Generating new graph...") pChan = puppetChan() // TODO: okay to update interval in case it changed? - ch <- nil // trigger a run + select { + case ch <- nil: // trigger a run (send a msg) + // unblock if we exit while waiting to send! + case <-obj.closeChan: + return + } case <-obj.closeChan: return } diff --git a/yamlgraph/gapi.go b/yamlgraph/gapi.go index 2dced942..f00bbaee 100644 --- a/yamlgraph/gapi.go +++ b/yamlgraph/gapi.go @@ -97,8 +97,13 @@ func (obj *GAPI) Next() chan error { return } log.Printf("yamlgraph: Generating new graph...") - ch <- err // trigger a run - if err != nil { + select { + case ch <- err: // trigger a run (send a msg) + if err != nil { + return + } + // unblock if we exit while waiting to send! + case <-obj.closeChan: return } case <-obj.closeChan: