prevent potential deadlock in server-invoked disconnect

This commit is contained in:
Liam Stanley 2017-08-17 07:18:24 -04:00
parent 6e1bd23a28
commit a1c6ebb737
3 changed files with 4 additions and 5 deletions

@ -274,8 +274,6 @@ func (c *Client) String() string {
// safe to call multiple times. See Connect()'s documentation on how
// handlers and goroutines are handled when disconnected from the server.
func (c *Client) Close() {
c.RunHandlers(&Event{Command: STOPPED, Trailing: c.Server()})
c.mu.RLock()
if c.stop != nil {
c.debug.Print("requesting client to stop")

@ -187,9 +187,6 @@ func TestClientClose(t *testing.T) {
})
c.Handlers.AddBg(INITIALIZED, func(c *Client, e Event) {
c.Close()
// Speed up the disconnect process.
server.Close()
conn.Close()
})
go func() {

@ -313,6 +313,7 @@ func (c *Client) internalConnect(mock net.Conn) error {
select {
case <-ctx.Done():
c.debug.Print("received request to close, beginning clean up")
c.RunHandlers(&Event{Command: STOPPED, Trailing: c.Server()})
case err := <-errs:
c.debug.Print("received error, beginning clean up")
result = err
@ -320,6 +321,9 @@ func (c *Client) internalConnect(mock net.Conn) error {
// Make sure that the connection is closed if not already.
c.mu.RLock()
if c.stop != nil {
c.stop()
}
c.conn.mu.Lock()
c.conn.connected = false
_ = c.conn.Close()