You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2017/08/07 20:14:27 UTC

[mynewt-newtmgr] 05/06: nmxact - Fix conn.Shutdown() race condition.

This is an automated email from the ASF dual-hosted git repository.

ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newtmgr.git

commit a1c846e69d42f2c698fab70e4002412e2e9d9e1d
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Mon Aug 7 12:55:45 2017 -0700

    nmxact - Fix conn.Shutdown() race condition.
---
 newtmgr/newtmgr.go     |  6 ++++--
 nmxact/nmble/conn.go   | 23 ++++++++++++-----------
 nmxact/omp/dispatch.go |  4 +++-
 3 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/newtmgr/newtmgr.go b/newtmgr/newtmgr.go
index 3133a9d..ff945ca 100644
--- a/newtmgr/newtmgr.go
+++ b/newtmgr/newtmgr.go
@@ -88,8 +88,10 @@ func main() {
 			s := <-sigChan
 			switch s {
 			case os.Interrupt, syscall.SIGTERM:
-				cli.SilenceErrors()
-				cli.NmExit(1)
+				go func() {
+					cli.SilenceErrors()
+					cli.NmExit(1)
+				}()
 
 			case syscall.SIGQUIT:
 				util.PrintStacks()
diff --git a/nmxact/nmble/conn.go b/nmxact/nmble/conn.go
index 36a147f..28321ef 100644
--- a/nmxact/nmble/conn.go
+++ b/nmxact/nmble/conn.go
@@ -38,6 +38,7 @@ type Conn struct {
 
 	connHandle     uint16
 	connecting     bool
+	stopped        bool
 	disconnectChan chan error
 	wg             sync.WaitGroup
 
@@ -74,13 +75,12 @@ func (c *Conn) initiateShutdown() bool {
 	c.mtx.Lock()
 	defer c.mtx.Unlock()
 
-	if c.stopChan == nil {
+	if c.stopped {
 		return false
 	}
+	c.stopped = true
 
 	close(c.stopChan)
-	c.stopChan = nil
-
 	return true
 }
 
@@ -100,6 +100,7 @@ func (c *Conn) shutdown(err error) {
 	if !c.initiateShutdown() {
 		return
 	}
+	defer func() { c.stopped = false }()
 
 	c.connecting = false
 	c.connHandle = BLE_CONN_HANDLE_NONE
@@ -697,16 +698,16 @@ func (c *Conn) Stop() error {
 	c.mtx.Lock()
 	defer c.mtx.Unlock()
 
-	var err error
 	if c.connHandle != BLE_CONN_HANDLE_NONE {
-		err = c.terminate()
+		// Terminate the connection.  On success, the conn object will shut
+		// down upon receipt of the disconnect event.  On failure, just force a
+		// shutdown manually.
+		if err := c.terminate(); err != nil {
+			go c.shutdown(err)
+		}
 	} else if c.connecting {
-		err = c.connCancel()
-	}
-
-	if err != nil {
-		// Something went unexpectedly wrong.  Just force a shutdown.
-		go c.shutdown(err)
+		c.connCancel()
+		go c.shutdown(fmt.Errorf("Stopped"))
 	}
 
 	return nil
diff --git a/nmxact/omp/dispatch.go b/nmxact/omp/dispatch.go
index 093efa5..0595e61 100644
--- a/nmxact/omp/dispatch.go
+++ b/nmxact/omp/dispatch.go
@@ -77,7 +77,9 @@ func (d *Dispatcher) addOmpListener() error {
 				}
 
 			case err := <-ol.ErrChan:
-				log.Debugf("OIC error: %s", err.Error())
+				if err != nil {
+					log.Debugf("OIC error: %s", err.Error())
+				}
 
 			case <-d.stopCh:
 				return

-- 
To stop receiving notification emails like this one, please contact
"commits@mynewt.apache.org" <co...@mynewt.apache.org>.