xmpp/xmpp.go
changeset 176 52c100897e9d
parent 175 fc8702a8572e
parent 170 d496de556c9a
child 178 ccfebbd9f49b
--- a/xmpp/xmpp.go	Wed Nov 06 20:38:03 2013 -0700
+++ b/xmpp/xmpp.go	Wed Nov 06 20:39:36 2013 -0700
@@ -13,6 +13,7 @@
 	"io"
 	"net"
 	"reflect"
+	"sync"
 )
 
 const (
@@ -66,7 +67,8 @@
 	// set up the XMPP stream will not appear here.
 	Recv <-chan Stanza
 	// Outgoing XMPP stanzas to the server should be sent to this
-	// channel.
+	// channel. The application should not close this channel;
+	// rather, call Close().
 	Send    chan<- Stanza
 	sendRaw chan<- interface{}
 	statmgr *statmgr
@@ -80,6 +82,7 @@
 	tlsConfig                    tls.Config
 	layer1                       *layer1
 	error                        chan error
+	shutdownOnce                 sync.Once
 }
 
 // Creates an XMPP client identified by the given JID, authenticating
@@ -256,8 +259,9 @@
 func (cl *Client) Close() {
 	// Shuts down the receivers:
 	cl.setStatus(StatusShutdown)
+
 	// Shuts down the senders:
-	close(cl.Send)
+	cl.shutdownOnce.Do(func() { close(cl.Send) })
 }
 
 // If there's a buffered error in the channel, return it. Otherwise,