浏览代码

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.
Jeffrey Wilcke 9 年之前
父节点
当前提交
7c1f74713e
共有 2 个文件被更改,包括 11 次插入0 次删除
  1. 3 0
      event/event.go
  2. 8 0
      event/event_test.go

+ 3 - 0
event/event.go

@@ -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 {

+ 8 - 0
event/event_test.go

@@ -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()