Skip to content

Commit cd6933b

Browse files
committed
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.
1 parent 0c5da0e commit cd6933b

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

push_notifications.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func (r *PushNotificationRegistry) HasHandlers() bool {
9797
type PushNotificationProcessor struct {
9898
registry *PushNotificationRegistry
9999
enabled bool
100+
mu sync.RWMutex // Protects enabled field
100101
}
101102

102103
// NewPushNotificationProcessor creates a new push notification processor.
@@ -109,11 +110,15 @@ func NewPushNotificationProcessor(enabled bool) *PushNotificationProcessor {
109110

110111
// IsEnabled returns whether push notification processing is enabled.
111112
func (p *PushNotificationProcessor) IsEnabled() bool {
113+
p.mu.RLock()
114+
defer p.mu.RUnlock()
112115
return p.enabled
113116
}
114117

115118
// SetEnabled enables or disables push notification processing.
116119
func (p *PushNotificationProcessor) SetEnabled(enabled bool) {
120+
p.mu.Lock()
121+
defer p.mu.Unlock()
117122
p.enabled = enabled
118123
}
119124

@@ -124,7 +129,7 @@ func (p *PushNotificationProcessor) GetRegistry() *PushNotificationRegistry {
124129

125130
// ProcessPendingNotifications checks for and processes any pending push notifications.
126131
func (p *PushNotificationProcessor) ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error {
127-
if !p.enabled || !p.registry.HasHandlers() {
132+
if !p.IsEnabled() || !p.registry.HasHandlers() {
128133
return nil
129134
}
130135

0 commit comments

Comments
 (0)