Skip to content

Commit

Permalink
Recover from send on closed channel
Browse files Browse the repository at this point in the history
  • Loading branch information
beatgammit committed Jan 16, 2017
1 parent 4a4b738 commit a8e7cbc
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit a8e7cbc

Please sign in to comment.