From a8e7cbce2ba84230eab0579be1874bdff8cd1235 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 | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/session.go b/session.go index 0912bce..38ffc7d 100644 --- a/session.go +++ b/session.go @@ -59,12 +59,21 @@ 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 + if r := recover(); r != nil { + err = fmt.Errorf("Attempt to write after Close()") + } + }() 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 }