From 4a66771c396d4a55a0bee8ccd469e015448126d9 Mon Sep 17 00:00:00 2001 From: Daniel Oaks Date: Tue, 18 Apr 2017 16:43:24 +1000 Subject: [PATCH] socket: Attempt to close sockets better --- irc/socket.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/irc/socket.go b/irc/socket.go index fbb748be..4c441872 100644 --- a/irc/socket.go +++ b/irc/socket.go @@ -49,10 +49,13 @@ func NewSocket(conn net.Conn, maxSendQBytes uint64) Socket { // Close stops a Socket from being able to send/receive any more data. func (socket *Socket) Close() { + if socket.Closed { + return + } socket.Closed = true // force close loop to happen - go socket.fillLineToSendExists() + go socket.fillLineToSendExists(true) } // CertFP returns the fingerprint of the certificate provided by the client. @@ -116,14 +119,16 @@ func (socket *Socket) Write(data string) error { socket.linesToSendMutex.Lock() socket.linesToSend = append(socket.linesToSend, data) socket.linesToSendMutex.Unlock() - go socket.fillLineToSendExists() + go socket.fillLineToSendExists(false) return nil } // fillLineToSendExists only exists because you can't goroutine single statements. -func (socket *Socket) fillLineToSendExists() { - socket.lineToSendExists <- true +func (socket *Socket) fillLineToSendExists(force bool) { + if force || !socket.Closed { + socket.lineToSendExists <- true + } } // RunSocketWriter starts writing messages to the outgoing socket. @@ -185,15 +190,16 @@ func (socket *Socket) RunSocketWriter() { break } } - // empty the lineToSendExists channel - for 0 < len(socket.lineToSendExists) { - <-socket.lineToSendExists - } //TODO(dan): empty socket.lineToSendExists queue socket.conn.Close() if !socket.Closed { socket.Closed = true } + + // empty the lineToSendExists channel + for 0 < len(socket.lineToSendExists) { + <-socket.lineToSendExists + } } // WriteLine writes the given line out of Socket.