Skip to content

Commit

Permalink
[release/1.4.4] event: fixed subscribtions to stopped event mux
Browse files Browse the repository at this point in the history
This fixes an issue where the following would lead to a panic due to a
channel being closed twice:

* Start mux
* Stop mux
* Sub to mux
* Unsub

This is fixed by setting the subscriptions status to closed resulting in
the Unsubscribe to ignore the request when called.

(cherry picked from commit 7c1f747)
  • Loading branch information
obscuren committed May 12, 2016
1 parent efcfa22 commit 7e5c49c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func (mux *TypeMux) Subscribe(types ...interface{}) Subscription {
mux.mutex.Lock()
defer mux.mutex.Unlock()
if mux.stopped {
// set the status to closed so that calling Unsubscribe after this
// call will short curuit
sub.closed = true
close(sub.postC)
} else {
if mux.subm == nil {
Expand Down
8 changes: 8 additions & 0 deletions event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ import (

type testEvent int

func TestSubCloseUnsub(t *testing.T) {
// the point of this test is **not** to panic
var mux TypeMux
mux.Stop()
sub := mux.Subscribe(int(0))
sub.Unsubscribe()
}

func TestSub(t *testing.T) {
mux := new(TypeMux)
defer mux.Stop()
Expand Down

0 comments on commit 7e5c49c

Please sign in to comment.