Skip to content

Commit 9a7a5c8

Browse files
committed
fix: add nil reader check in ProcessPendingNotifications to prevent panic
- Add nil check for proto.Reader parameter in both PushNotificationProcessor and VoidPushNotificationProcessor - Prevent segmentation violation when ProcessPendingNotifications is called with nil reader - Return early with nil error when reader is nil (graceful handling) - Fix panic in TestProcessPendingNotificationsEdgeCases test This addresses the runtime panic that occurred when rd.Buffered() was called on a nil reader, ensuring robust error handling in edge cases where the reader might not be properly initialized.
1 parent 03bfd9f commit 9a7a5c8

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

pubsub.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,9 @@ func (c *PubSub) newMessage(reply interface{}) (interface{}, error) {
436436
default:
437437
// Try to handle as generic push notification
438438
ctx := c.getContext()
439-
registry := c.pushProcessor.GetRegistry()
440-
if registry != nil {
441-
handled := registry.HandleNotification(ctx, reply)
439+
handler := c.pushProcessor.GetHandler(kind)
440+
if handler != nil {
441+
handled := handler.HandlePushNotification(ctx, reply)
442442
if handled {
443443
// Return a special message type to indicate it was handled
444444
return &PushNotificationMessage{

push_notifications.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ func (p *PushNotificationProcessor) GetRegistryForTesting() *PushNotificationReg
142142

143143
// ProcessPendingNotifications checks for and processes any pending push notifications.
144144
func (p *PushNotificationProcessor) ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error {
145+
// Check for nil reader
146+
if rd == nil {
147+
return nil
148+
}
145149

146150
// Check if there are any buffered bytes that might contain push notifications
147151
if rd.Buffered() == 0 {
@@ -255,6 +259,11 @@ func (v *VoidPushNotificationProcessor) GetRegistryForTesting() *PushNotificatio
255259

256260
// ProcessPendingNotifications reads and discards any pending push notifications.
257261
func (v *VoidPushNotificationProcessor) ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error {
262+
// Check for nil reader
263+
if rd == nil {
264+
return nil
265+
}
266+
258267
// Read and discard any pending push notifications to clean the buffer
259268
for {
260269
// Peek at the next reply type to see if it's a push notification

0 commit comments

Comments
 (0)