From 001aac80391cc76b02c2c3c32a925b168a1b7a60 Mon Sep 17 00:00:00 2001 From: "T. Jameson Little" Date: Mon, 16 Jan 2017 16:00:55 -0700 Subject: [PATCH] Recover from send on closed channel --- session.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/session.go b/session.go index 0912bce..da7b875 100644 --- a/session.go +++ b/session.go @@ -59,12 +59,19 @@ func (s *localPeer) Receive() <-chan Message { return s.incoming } -func (s *localPeer) Send(msg Message) error { +func (s *localPeer) Send(msg Message) (err error) { + defer func() { + // just in case Close is called before Send + err = recover() + }() s.outgoing <- msg - return nil + return } func (s *localPeer) Close() error { - close(s.outgoing) + if s.outgoing != nil { + close(s.outgoing) + s.outgoing = nil + } return nil }