Don't call Close() twice.
authorChris Jones <chris@cjones.org>
Wed, 16 Oct 2013 20:29:29 -0600
changeset 168 f4ccc2f29777
parent 167 7ce61108ed86
child 169 f59b79d032da
Don't call Close() twice.
xmpp/xmpp.go
--- a/xmpp/xmpp.go	Wed Oct 16 20:05:02 2013 -0600
+++ b/xmpp/xmpp.go	Wed Oct 16 20:29:29 2013 -0600
@@ -13,6 +13,7 @@
 	"io"
 	"net"
 	"reflect"
+	"sync"
 )
 
 const (
@@ -80,6 +81,7 @@
 	tlsConfig                    tls.Config
 	layer1                       *layer1
 	error                        chan error
+	shutdownOnce                 sync.Once
 }
 
 // Creates an XMPP client identified by the given JID, authenticating
@@ -249,8 +251,11 @@
 // there's already an error in the channel, discard the newer one in
 // favor of the older.
 func (cl *Client) setError(err error) {
-	defer cl.Close()
-	defer cl.setStatus(StatusError)
+	shutdown := func() {
+		cl.setStatus(StatusError)
+		cl.Close()
+	}
+	defer cl.shutdownOnce.Do(shutdown)
 
 	if len(cl.error) > 0 {
 		return