Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential deadlock due to calling callbacks while holding a lock #35

Open
sergey-shambir opened this issue Dec 3, 2018 · 1 comment · May be fixed by #53
Open

Potential deadlock due to calling callbacks while holding a lock #35

sergey-shambir opened this issue Dec 3, 2018 · 1 comment · May be fixed by #53

Comments

@sergey-shambir
Copy link

sergey-shambir commented Dec 3, 2018

See golang mutexes aren't recursive / re-entrant

Bus.Publish(...) method locks mutex and calls callbacks under lock, so any access to the bus in callback will cause deadlock.

The following example reproduces issue:

package main

import (
	"fmt"

	"github.com/asaskevich/EventBus"
)

var bus EventBus.Bus

func showbug(a int, b int) {
	fmt.Printf("%d\n", a+b)

	if a == 20 {
		bus.Publish("main:calculator", a+1, b)
	}
}

func main() {
	bus = EventBus.New()
	bus.Subscribe("main:calculator", showbug)
	bus.Publish("main:calculator", 20, 40)
	bus.Unsubscribe("main:calculator", showbug)
}

We use another implementation of eventbus (inspired by yours) where this issue was fixed: github.com/ispringteam/goeventbus (see copySubscriptions method and nextID field)

Feel free to adapt our solution or introduce another one ;)

@Snawoot Snawoot linked a pull request Apr 14, 2022 that will close this issue
@alielbashir
Copy link

Thanks for this, i've been struggling with this lib for real time updates with gRPC streaming and faced many bugs related to unsubscribe. Moving to your implementation seems to fix the problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants