Skip to content

Commit 7e5c49c

Browse files
committed
[release/1.4.4] event: fixed subscribtions to stopped event mux
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)
1 parent efcfa22 commit 7e5c49c

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

event/event.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ func (mux *TypeMux) Subscribe(types ...interface{}) Subscription {
6666
mux.mutex.Lock()
6767
defer mux.mutex.Unlock()
6868
if mux.stopped {
69+
// set the status to closed so that calling Unsubscribe after this
70+
// call will short curuit
71+
sub.closed = true
6972
close(sub.postC)
7073
} else {
7174
if mux.subm == nil {

event/event_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ import (
2525

2626
type testEvent int
2727

28+
func TestSubCloseUnsub(t *testing.T) {
29+
// the point of this test is **not** to panic
30+
var mux TypeMux
31+
mux.Stop()
32+
sub := mux.Subscribe(int(0))
33+
sub.Unsubscribe()
34+
}
35+
2836
func TestSub(t *testing.T) {
2937
mux := new(TypeMux)
3038
defer mux.Stop()

0 commit comments

Comments
 (0)