8000 fix: Avoid panic in `ServerSentEventSender` by keeping handler alive by mafredri · Pull Request #4821 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

fix: Avoid panic in ServerSentEventSender by keeping handler alive #4821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 1, 2022
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
0