From 2e8a98a925b322ad81f716bcb46c185e07a8dc93 Mon Sep 17 00:00:00 2001 From: Euan Kemp Date: Thu, 13 Jul 2017 23:45:17 -0700 Subject: [PATCH] irc: fix timer memleak This is a gotcha called out in the `time.After` docs. `time.After` will leak the underlying channel if nothing ever receives on it. --- irc/socket.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/irc/socket.go b/irc/socket.go index 0e99f622..1da4a1b3 100644 --- a/irc/socket.go +++ b/irc/socket.go @@ -133,10 +133,12 @@ func (socket *Socket) Write(data string) error { // timedFillLineToSendExists either sends the note or times out. func (socket *Socket) timedFillLineToSendExists(duration time.Duration) { + lineToSendTimeout := time.NewTimer(duration) + defer lineToSendTimeout.Stop() select { case socket.lineToSendExists <- true: // passed data successfully - case <-time.After(duration): + case <-lineToSendTimeout.C: // timed out send } }