Skip to content

Commit 204ab4a

Browse files
committed
fix data race caused by webpush-go mutating msg []byte
1 parent 4408090 commit 204ab4a

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

coderd/notifications/push/push.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"io"
99
"net/http"
10+
"slices"
1011
"sync"
1112

1213
"github.com/SherClockHolmes/webpush-go"
@@ -93,8 +94,8 @@ func (n *Notifier) Dispatch(ctx context.Context, userID uuid.UUID, notification
9394
subscription := subscription
9495
eg.Go(func() error {
9596
n.log.Debug(ctx, "dispatching via push", slog.F("subscription", subscription.Endpoint))
96-
97-
resp, err := webpush.SendNotificationWithContext(ctx, notificationJSON, &webpush.Subscription{
97+
cpy := slices.Clone(notificationJSON) // Need to copy as webpush.SendNotificationWithContext modifies the slice.
98+
resp, err := webpush.SendNotificationWithContext(ctx, cpy, &webpush.Subscription{
9899
Endpoint: subscription.Endpoint,
99100
Keys: webpush.Keys{
100101
Auth: subscription.EndpointAuthKey,

coderd/notifications/push/push_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func TestPush(t *testing.T) {
179179
},
180180
}
181181

182-
err = manager.Dispatch(context.Background(), user.ID, notification)
182+
err = manager.Dispatch(ctx, user.ID, notification)
183183
require.NoError(t, err)
184184
assert.True(t, okEndpointCalled, "The valid endpoint should be called")
185185
assert.True(t, goneEndpointCalled, "The expired endpoint should be called")

0 commit comments

Comments
 (0)