websocket: connection attempts on the input connector now honor context cancellation - #483
websocket: connection attempts on the input connector now honor context cancellation#483Leward wants to merge 3 commits into
Conversation
| if client, res, err = dialer.Dial(w.urlStr, headers); err != nil { | ||
| return nil, err | ||
| } | ||
| } else if client, res, err = dialer.Dial(w.urlStr, headers); err != nil { |
There was a problem hiding this comment.
The branching was redundant and has been simplified
| @@ -0,0 +1,72 @@ | |||
| // Copyright 2025 Redpanda Data, Inc. | |||
There was a problem hiding this comment.
This file has been introduced because in a follow-up change the websocket output will need to use those functions.
|
Commits
Review The approach is sound and well-reasoned: honouring the context through the HTTP upgrade phase genuinely requires poking the raw One issue found:
|
bf682a6 to
60ae0e1
Compare
|
Commits Review LGTM |
getConn received a context but discarded it: dialer.Dial always used context.Background, so a peer that accepted the TCP connection and never completed the HTTP upgrade handshake held the input for gorilla's 45s default handshake timeout. AsyncReader has no other way to stop the reader than the context it passes to Connect, so this could outlast the Kubernetes default terminationGracePeriodSeconds (30s) and turn a graceful shutdown into a kill. Add a dialContext helper that keeps a handle on the raw connection via NetDialContext and uses context.AfterFunc to close it once the outer context is done, waking a handshake read that gorilla's own timeout wouldn't catch in time. The resulting error is mapped back to ctx.Err so callers can tell a shutdown from a flaky peer. The helper hides the context's deadline from gorilla so gorilla's own connection deadline doesn't race the watcher. dialContext and its test helpers are free functions with no reader state, so they live in shared websocket.go/websocket_test.go files rather than the input-only ones, ready for the output side to reuse.
Co-authored-by: Joseph Woodward <joseph.woodward@redpanda.com>
Co-authored-by: Joseph Woodward <joseph.woodward@redpanda.com>
c742850 to
f47750c
Compare
|
Commits
Review The change adds a LGTM |
What
Ensures connection attempts on the websocket input connector properly honor context cancellation and deadlines during the HTTP upgrade/handshake phase.
Why
Previously, Gorilla WebSocket only honored context cancellation during TCP/TLS handshakes, causing hangs against unresponsive endpoints for up to the default 45-second handshake timeout and delaying graceful shutdown.
Key Changes:
dialContexthelper usingcontext.AfterFunconnet.Connto interrupt hanging HTTP upgrade exchanges upon context cancellation.ctx.Deadline()viahiddenDeadlineContextto avoid competing socket timers and ensure deterministic context error returns.websocketinput connector and added test cases covering pre-dial cancellation, mid-handshake cancellation, and deadline timeouts.Out of Scope:
ReadMessage()will be handled separatelywebsocket outputwill be handled in a follow-up PRRefs: