Skip to content

Commit aa6539d

Browse files
authored
fix: prevent context cancellation for critical write (#1151)
* fix: prevent context cancellation for critical write * fix * cs
1 parent 19b3850 commit aa6539d

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

publish.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ func (h *Hub) PublishHandler(w http.ResponseWriter, r *http.Request) {
105105
Debug: h.debug,
106106
Event: Event{r.PostForm.Get("data"), r.PostForm.Get("id"), r.PostForm.Get("type"), retry},
107107
}
108+
108109
ctx = context.WithValue(ctx, UpdateContextKey, u)
110+
dispatchCtx := context.WithoutCancel(ctx)
109111

110112
// Broadcast the update
111-
if err := h.transport.Dispatch(ctx, u); err != nil {
113+
if err := h.transport.Dispatch(dispatchCtx, u); err != nil {
112114
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
113115

114116
if h.logger.Enabled(ctx, slog.LevelError) {

subscribe.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,12 @@ func (h *Hub) registerSubscriber(ctx context.Context, w http.ResponseWriter, r *
213213

214214
s.SetTopics(topics, privateTopics)
215215

216-
h.dispatchSubscriptionUpdate(ctx, s, true)
216+
addCtx := context.WithoutCancel(ctx)
217+
h.dispatchSubscriptionUpdate(addCtx, s, true)
217218

218-
if err := h.transport.AddSubscriber(ctx, s); err != nil {
219+
if err := h.transport.AddSubscriber(addCtx, s); err != nil {
219220
http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
220-
h.dispatchSubscriptionUpdate(ctx, s, false)
221+
h.dispatchSubscriptionUpdate(addCtx, s, false)
221222

222223
if h.logger.Enabled(ctx, slog.LevelError) {
223224
h.logger.LogAttrs(ctx, slog.LevelError, "Unable to add subscriber", slog.Any("error", err))
@@ -331,6 +332,8 @@ func (h *Hub) shutdown(ctx context.Context, s *LocalSubscriber) {
331332
// Notify that the client is closing the connection
332333
s.Disconnect()
333334

335+
ctx = context.WithoutCancel(ctx)
336+
334337
if err := h.transport.RemoveSubscriber(ctx, s); err != nil && h.logger.Enabled(ctx, slog.LevelError) {
335338
h.logger.LogAttrs(ctx, slog.LevelError, "Failed to remove subscriber on shutdown", slog.Any("error", err))
336339
}

0 commit comments

Comments
 (0)