Skip to content

Add a timeout for the WebSocket upgrade response - #88

Merged
the-mikedavis merged 1 commit into
mainfrom
upgrade-timeout
Oct 26, 2026
Merged

the-mikedavis merged 1 commit into
mainfrom
upgrade-timeout

Conversation

@the-mikedavis

Copy link
Copy Markdown
Collaborator

Previously, if a server accepted the TCP connection but never responded to the WebSocket upgrade request, the connection would wait forever. The heartbeat timer only starts once the upgrade completes. Mint has a connect timeout, but it only covers opening the TCP/TLS connection.

This change introduces an upgrade_timeout_msec option which defaults to ten seconds (0 to turn off). When that timeout elapses, the connection is closed and c:Slipstream.handle_disconnect/2 is invoked with {:error, :upgrade_timeout}.

Fixes #75

Previously, if a server accepted the TCP connection but never responded
to the WebSocket upgrade request, the connection would wait forever. The
heartbeat timer only starts once the upgrade completes. Mint has a
connect timeout, but it only covers opening the TCP/TLS connection.

This change introduces an `upgrade_timeout_msec` option which defaults
to ten seconds (`0` to turn off). When that timeout elapses, the
connection is closed and  `c:Slipstream.handle_disconnect/2` is invoked
with `{:error, :upgrade_timeout}`.
@elielgordondensity

Copy link
Copy Markdown

Just stumbled on this as thousands of our devices on Nerves Hub fell offline after a brief outage and ever came back on, but our MQTT link is still up, which points to NervesHubLink's usage of Slipstream. Would be nice to know where this is at. @the-mikedavis

@joshk

joshk commented Oct 18, 2026

Copy link
Copy Markdown

It would be fantastic to get this merged in. NervesHubLink uses Slipstream, and with one self-hosted platform supporting over 300k devices, covering this gap is important for us.

joshk added a commit to nerves-hub/nerves_hub_link that referenced this pull request Oct 21, 2026
Checking this work against the open Slipstream pull requests turned up
three things it had not actually addressed.

A reply from NervesHub to one of the device's own pushes took the socket
down. Phoenix sends a reply whenever a channel handler returns one, so
any push the device makes can be answered, and `handle_reply/3` had a
single clause for the extensions detach. Everything else raised a
`FunctionClauseError`. This predates the move - the same single clause
was there on Slipstream, which dispatched to it the same way - and was
only latent because NervesHub does not reply to these pushes today.
Verified against a real server before and after.

A reply whose status is neither `ok` nor `error` was reported as an
error, turning a success into a failure. Phoenix lets a channel reply
with any status, so an unknown one is now passed through as it arrived.

A frame Mint cannot read - a reserved opcode, a text frame that is not
valid UTF-8 - arrives as an `{:error, reason}` entry in the frame list
rather than as a decode failure, which is what CuatroElixir/slipstream#93
is about. The generic catch-all was absorbing these; it now has a clause
of its own that says so, and naming it made the catch-all provably
unreachable, so that went rather than gaining a dialyzer ignore.

CuatroElixir/slipstream#88 was implemented but not reachable: the
upgrade timeout was a `Connection` default with nothing passing it from
configuration. It is now `upgrade_timeout_msec`, 10 seconds to match the
upstream default, with `0` to wait forever, and covered end to end
against a listener that accepts the connection and says nothing.

448 tests, credo and dialyzer clean on 1.18.4/OTP 27.3.4 and
1.19.5/OTP 28.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@the-mikedavis

Copy link
Copy Markdown
Collaborator Author

Thanks for the feedback! I'll merge it down and work on cutting the next release

@the-mikedavis
the-mikedavis merged commit c663278 into main Oct 26, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Connection hangs, does not reconnect with unsuccessful Websocket handshake

3 participants