From 773525e1e7f28a3a00d819ec0173345f3daa8a32 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Thu, 10 Sep 2026 10:32:25 -0400 Subject: [PATCH] Add a timeout for the WebSocket upgrade response 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}`. --- lib/slipstream/configuration.ex | 12 ++++++ lib/slipstream/connection/pipeline.ex | 37 ++++++++++++++++- lib/slipstream/connection/state.ex | 1 + test/slipstream/integration_test.exs | 58 +++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 2 deletions(-) diff --git a/lib/slipstream/configuration.ex b/lib/slipstream/configuration.ex index 4148293..7ccfbd0 100644 --- a/lib/slipstream/configuration.ex +++ b/lib/slipstream/configuration.ex @@ -18,6 +18,17 @@ defmodule Slipstream.Configuration do type: :non_neg_integer, default: 30_000 ], + upgrade_timeout_msec: [ + doc: """ + The time to wait for the server to respond to the WebSocket upgrade + request once the connection has been opened. If the server does not + respond within this time, the connection is closed and + `c:Slipstream.handle_disconnect/2` is invoked with + `{:error, :upgrade_timeout}`. A value of `0` will disable the timeout. + """, + type: :non_neg_integer, + default: 10_000 + ], headers: [ doc: """ A set of headers to merge with the request headers when GETing the @@ -139,6 +150,7 @@ defmodule Slipstream.Configuration do @type t :: %__MODULE__{ uri: %URI{}, heartbeat_interval_msec: non_neg_integer(), + upgrade_timeout_msec: non_neg_integer(), headers: [{String.t(), String.t()}], json_parser: module(), serializer: module(), diff --git a/lib/slipstream/connection/pipeline.ex b/lib/slipstream/connection/pipeline.ex index 403a187..6dce4fe 100644 --- a/lib/slipstream/connection/pipeline.ex +++ b/lib/slipstream/connection/pipeline.ex @@ -49,6 +49,24 @@ defmodule Slipstream.Connection.Pipeline do put_message(p, :connect) end + defp decode_message( + %{raw_message: :upgrade_timeout, state: %State{status: :opened}} = p + ) do + put_message( + p, + event(%Events.ChannelConnectFailed{reason: :upgrade_timeout}) + ) + end + + # the upgrade timer fired just as the connection was established, so it + # could not be cancelled + # coveralls-ignore-start + defp decode_message(%{raw_message: :upgrade_timeout} = p) do + put_message(p, event(%Events.NoOp{})) + end + + # coveralls-ignore-stop + defp decode_message( %{ raw_message: {:DOWN, ref, :process, _pid, reason}, @@ -184,7 +202,14 @@ defmodule Slipstream.Connection.Pipeline do ) do with {:ok, conn} <- Impl.http_connect(config), {:ok, conn, ref} <- Impl.websocket_upgrade(conn, config) do - put_state(p, %{state | conn: conn, request_ref: ref}) + state = %{ + state + | conn: conn, + request_ref: ref, + upgrade_timer: start_upgrade_timer(config) + } + + put_state(p, state) else # coveralls-ignore-start {:error, conn, reason} -> @@ -408,6 +433,8 @@ defmodule Slipstream.Connection.Pipeline do state: %State{} = state } = p ) do + if state.upgrade_timer, do: Process.cancel_timer(state.upgrade_timer) + timer = if state.config.heartbeat_interval_msec != 0 do {:ok, tref} = @@ -420,7 +447,7 @@ defmodule Slipstream.Connection.Pipeline do end state = - %{state | status: :connected, heartbeat_timer: timer} + %{state | status: :connected, upgrade_timer: nil, heartbeat_timer: timer} |> State.reset_heartbeat() route_event state, event @@ -473,6 +500,12 @@ defmodule Slipstream.Connection.Pipeline do # coveralls-ignore-stop + defp start_upgrade_timer(%{upgrade_timeout_msec: 0}), do: nil + + defp start_upgrade_timer(%{upgrade_timeout_msec: timeout}) do + Process.send_after(self(), :upgrade_timeout, timeout) + end + @spec default_return(t()) :: t() defp default_return(%{state: state, return: nil} = p) do put_return(p, {:noreply, state}) diff --git a/lib/slipstream/connection/state.ex b/lib/slipstream/connection/state.ex index a505b4b..4f5c827 100644 --- a/lib/slipstream/connection/state.ex +++ b/lib/slipstream/connection/state.ex @@ -21,6 +21,7 @@ defmodule Slipstream.Connection.State do :websocket, :request_ref, :join_params, + :upgrade_timer, :heartbeat_timer, :heartbeat_ref, :metadata, diff --git a/test/slipstream/integration_test.exs b/test/slipstream/integration_test.exs index 494aa6a..7237b51 100644 --- a/test/slipstream/integration_test.exs +++ b/test/slipstream/integration_test.exs @@ -28,6 +28,64 @@ defmodule Slipstream.IntegrationTest do end end + describe "given the server never responds to the WebSocket upgrade request" do + setup do + {:ok, listen} = :gen_tcp.listen(0, [:binary, active: false]) + {:ok, port} = :inet.port(listen) + test_pid = self() + + spawn_link(fn -> + {:ok, socket} = :gen_tcp.accept(listen) + {:ok, _upgrade_request} = :gen_tcp.recv(socket, 0) + # never respond, but report when the client closes the connection + send(test_pid, {:server_recv, :gen_tcp.recv(socket, 0)}) + end) + + [ + config: [ + uri: "ws://localhost:#{port}/socket/websocket", + upgrade_timeout_msec: 100 + ] + ] + end + + test "the socket is disconnected with :upgrade_timeout reason", c do + import Slipstream + + assert {:error, :upgrade_timeout} = + c.config + |> connect!() + |> await_connect(15_000) + + assert_receive {:server_recv, {:error, :closed}} + end + end + + describe "given the server responds to the WebSocket upgrade request" do + setup do + [ + config: [ + uri: "ws://localhost:4001/socket/websocket", + upgrade_timeout_msec: 500 + ] + ] + end + + test "the upgrade timeout does not close the connected socket", c do + import Slipstream + + socket = + c.config + |> connect!() + |> await_connect!(15_000) + + Process.sleep(1_000) + + assert Process.alive?(socket.channel_pid) + refute_received _ + end + end + defmodule EtfSerializer do alias Slipstream.Message