chuckpreslar/emission

Emit an event which doesn't have listener will result deadlock

lsm opened this issue · 2 comments

lsm commented

Hi Chuck,

This lib is very handy (like the one in node.js). But I can't fire an event without a listener. What do you think to handle this case silently as sometime the event handler doesn't exist when the event is triggered.

fatal error: all goroutines are asleep - deadlock!
goroutine 1 [semacquire]:
sync.runtime_Semacquire(0x2101fa01c)
    /usr/local/Cellar/go/1.2/libexec/src/pkg/runtime/sema.goc:199 +0x30
sync.(*Mutex).Lock(0x2101fa018)
    /usr/local/Cellar/go/1.2/libexec/src/pkg/sync/mutex.go:66 +0xd6
github.com/chuckpreslar/emission.(*Emitter).Emit(0x21023b000, 0x83520, 0x2102041b0, 0x221036bf18, 0x1, ...)
...
chuckpreslar/emission/emitter.go:150 +0x82

The following code will reproduce the above fatal error:

package main

import (
  "fmt"
)

import (
  "github.com/chuckpreslar/emission"
)

func main() {
  emitter := emission.NewEmitter()

  hello := func(to string) {
    fmt.Printf("Hello %s!\n", to)
  }

  count := func(count int) {
    for i := 0; i < count; i++ {
      fmt.Println(i)
    }
  }

  emitter.On("hello", hello).
    On("count", count).
    Emit("other event", "world").
    Emit("count", 5)
}

Senmiao

Good catch! If my commit failed to resolve your issue, please reopen.

lsm commented

The problem is fixed. Thank you.
I also sent a pull request in which a test was added for this issue.