Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve cancellation in sendEvent
  • Loading branch information
mafredri committed Nov 1, 2022
commit 87a10aac1ac0354921fc945241ba5ef7da7e8c8f
16 changes: 13 additions & 3 deletions coderd/httpapi/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,28 @@ func ServerSentEventSender(rw http.ResponseWriter, r *http.Request) (sendEvent f

event := sseEvent{
payload: buf.Bytes(),
errC: make(chan error, 1),
errC: make(chan error, 1), // Buffered to prevent deadlock.
}

select {
case <-r.Context().Done():
return r.Context().Err()
case <-ctx.Done():
return ctx.Err()
case eventC <- event:
return <-event.errC
case <-closed:
return xerrors.New("server sent event sender closed")
case eventC <- event:
// Re-check closure signals after sending the event to allow
// for early exit. We don't check closed here because it
// can't happen while processing the event.
select {
case <-r.Context().Done():
return r.Context().Err()
case <-ctx.Done():
return ctx.Err()
case err := <-event.errC:
return err
}
}
}

Expand Down