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

fix: use read-only lock for event callbacks #388

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var globalEventHandlers map[string][]any

// globalEventHandlersLock is a lock that provides thread synchronization when accessing globalEventHandlers. This
// helps in avoiding concurrent access panics.
var globalEventHandlersLock sync.Mutex
var globalEventHandlersLock sync.RWMutex

// SubscribeAny adds an EventHandler to the list of global EventHandler objects for this a given event data type.
// When an event is published, the callback will be triggered with the event data.
Expand Down Expand Up @@ -76,9 +76,9 @@ func (e *EventEmitter[T]) Publish(event T) error {
// If we have any handlers, invoke them.
if globalEventHandlers != nil {
// Acquire a thread lock when fetching our event handlers to avoid concurrent access panics.
globalEventHandlersLock.Lock()
globalEventHandlersLock.RLock()
callbacks := globalEventHandlers[eventType.String()]
globalEventHandlersLock.Unlock()
globalEventHandlersLock.RUnlock()

// Call all relevant event handlers.
for i := 0; i < len(callbacks); i++ {
Expand Down