diff --git a/client/connection.go b/client/connection.go index 32ff9d3..892bf8a 100644 --- a/client/connection.go +++ b/client/connection.go @@ -139,6 +139,7 @@ func (conn *Conn) Connect(host string, pass ...string) os.Error { bufio.NewReader(conn.sock), bufio.NewWriter(conn.sock)) conn.sock.SetTimeout(conn.Timeout * 1e9) + conn.connected = true go conn.send() go conn.recv() @@ -247,14 +248,18 @@ func (conn *Conn) rateLimit(chars int64) { } func (conn *Conn) shutdown() { - conn.connected = false - conn.sock.Close() - conn.cSend <- true - conn.cLoop <- true - conn.dispatchEvent(&Line{Cmd: "DISCONNECTED"}) - // reinit datastructures ready for next connection - // do this here rather than after runLoop()'s for due to race - conn.initialise() + // Guard against double-call of shutdown() if we get an error in send() + // as calling sock.Close() will cause recv() to recieve EOF in readstring() + if conn.connected { + conn.connected = false + conn.sock.Close() + conn.cSend <- true + conn.cLoop <- true + conn.dispatchEvent(&Line{Cmd: "DISCONNECTED"}) + // reinit datastructures ready for next connection + // do this here rather than after runLoop()'s for due to race + conn.initialise() + } } // Dumps a load of information about the current state of the connection to a diff --git a/client/handlers.go b/client/handlers.go index 75ca25a..d3f896d 100644 --- a/client/handlers.go +++ b/client/handlers.go @@ -76,7 +76,6 @@ func (conn *Conn) h_PING(line *Line) { // Handler to trigger a "CONNECTED" event on receipt of numeric 001 func (conn *Conn) h_001(line *Line) { // we're connected! - conn.connected = true conn.dispatchEvent(&Line{Cmd: "CONNECTED"}) // and we're being given our hostname (from the server's perspective) t := line.Args[len(line.Args)-1] @@ -103,7 +102,7 @@ func (conn *Conn) h_433(line *Line) { // if this is happening before we're properly connected (i.e. the nick // we sent in the initial NICK command is in use) we will not receive // a NICK message to confirm our change of nick, so ReNick here... - if !conn.connected && line.Args[1] == conn.Me.Nick { + if line.Args[1] == conn.Me.Nick { conn.Me.ReNick(line.Args[1] + "_") } }