From 958fb1a760956318bf41132de30c93b375b9d3e0 Mon Sep 17 00:00:00 2001 From: Nedyalko Dyakov Date: Fri, 27 Jun 2025 00:22:44 +0300 Subject: [PATCH] fix: resolve data race in PushNotificationProcessor - Add sync.RWMutex to PushNotificationProcessor struct - Protect enabled field access with read/write locks in IsEnabled() and SetEnabled() - Use thread-safe IsEnabled() method in ProcessPendingNotifications() - Fix concurrent access to enabled field that was causing data races This resolves the race condition between goroutines calling IsEnabled() and SetEnabled() concurrently, ensuring thread-safe access to the enabled field. --- push_notifications.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/push_notifications.go b/push_notifications.go index c88647ce..b1c89ca3 100644 --- a/push_notifications.go +++ b/push_notifications.go @@ -97,6 +97,7 @@ func (r *PushNotificationRegistry) HasHandlers() bool { type PushNotificationProcessor struct { registry *PushNotificationRegistry enabled bool + mu sync.RWMutex // Protects enabled field } // NewPushNotificationProcessor creates a new push notification processor. @@ -109,11 +110,15 @@ func NewPushNotificationProcessor(enabled bool) *PushNotificationProcessor { // IsEnabled returns whether push notification processing is enabled. func (p *PushNotificationProcessor) IsEnabled() bool { + p.mu.RLock() + defer p.mu.RUnlock() return p.enabled } // SetEnabled enables or disables push notification processing. func (p *PushNotificationProcessor) SetEnabled(enabled bool) { + p.mu.Lock() + defer p.mu.Unlock() p.enabled = enabled } @@ -124,7 +129,7 @@ func (p *PushNotificationProcessor) GetRegistry() *PushNotificationRegistry { // ProcessPendingNotifications checks for and processes any pending push notifications. func (p *PushNotificationProcessor) ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error { - if !p.enabled || !p.registry.HasHandlers() { + if !p.IsEnabled() || !p.registry.HasHandlers() { return nil }