diff -r 7ce61108ed86 -r f4ccc2f29777 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