From 329a62d7d90841a3c3ac1197abd940afb81f7060 Mon Sep 17 00:00:00 2001 From: Alex Bramley Date: Sun, 18 Dec 2016 14:22:12 +0000 Subject: [PATCH] Don't hold conn.mu during REGISTER. Fixes #94. --- client/connection.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/client/connection.go b/client/connection.go index 344926f..f84621a 100644 --- a/client/connection.go +++ b/client/connection.go @@ -295,6 +295,18 @@ func (conn *Conn) ConnectTo(host string, pass ...string) error { // handler for the CONNECTED event is used to perform any initial client work // like joining channels and sending messages. func (conn *Conn) Connect() error { + // We don't want to hold conn.mu while firing the REGISTER event, + // and it's much easier and less error prone to defer the unlock, + // so the connect mechanics have been delegated to internalConnect. + err := conn.internalConnect() + if err == nil { + conn.dispatch(&Line{Cmd: REGISTER, Time: time.Now()}) + } + return err +} + +// internalConnect handles the work of actually connecting to the server. +func (conn *Conn) internalConnect() error { conn.mu.Lock() defer conn.mu.Unlock() conn.initialise() @@ -350,7 +362,6 @@ func (conn *Conn) Connect() error { conn.postConnect(true) conn.connected = true - conn.dispatch(&Line{Cmd: REGISTER, Time: time.Now()}) return nil }