Skip to content

Fix synchronization issues (fatal error: concurrent map writes) #2

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

LukaszMoskala
Copy link

Hello and thank you for maintaining this library.

It's possible to trigger concurrent map writes or concurrent map read and write when calling
AddListener and RemoveListener from multiple goroutines. I used sync.Mutex on all functions that use subscribersMap.

I also use mutextes on Emit since removing element from s.subscribers while for loop is running in another goroutine could cause problems.

I have tested and this fixes my problem.

Have a great day!

@@ -12,6 +15,7 @@ type keyedListener[T any] struct {
// It is intended to be used as an abstract base for underlying signal mechanisms.
//
// Example:
//
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about these lines, go fmt keeps adding them back

@linux019
Copy link

linux019 commented Apr 15, 2025

I think IsEmpty() and Len() should be protected with .RLock() too

count += 1
here data race too

@delaneyj
Copy link

Any movement on this? I'm running into the same issues

@linux019
Copy link

I did some fixes of this in https://github.com/linux019/signals

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 this pull request may close these issues.

3 participants