Skip to content

Handle ping, pong and invalid frames from the server without crashing - #93

Open
joshk wants to merge 1 commit into
CuatroElixir:mainfrom
joshk:fix/server-frames-crash-connection
Open

joshk wants to merge 1 commit into
CuatroElixir:mainfrom
joshk:fix/server-frames-crash-connection

Conversation

@joshk

@joshk joshk commented Oct 18, 2026

Copy link
Copy Markdown

Problem

Several frames a server can legitimately send crash the Slipstream.Connection process with a FunctionClauseError or MatchError:

  • Ping and pong frames. Mint.WebSocket decodes these as {:ping, data} / {:pong, data}, but Impl.decode_message/2 only matches the bare atoms :ping / :pong (lib/slipstream/connection/impl.ex:125). The existing PingReceived / PongReceived handling, including the automatic pong reply, is therefore unreachable.
  • Invalid frames, e.g. a text frame that isn't valid UTF-8. Mint.WebSocket puts {:error, reason} entries in the decoded frame list, and nothing matches them.
  • Replies with a status other than "ok" / "error". Phoenix allows any atom status ({:reply, :accepted, socket}, {:reply, {:queued, %{}}, socket}), but ReplyReceived.to_reply/1 only matches those two (lib/slipstream/events/reply_received.ex:11).
  • A phx_error whose ref isn't a tracked join ref hits true = State.join_ref?(state, ref) (lib/slipstream/events.ex:121).

The client process doesn't monitor the connection process, so after any of these crashes handle_disconnect/2 is never called and the client never reconnects.

Reproduction

A Phoenix channel that replies with a custom status:

def handle_in("work", _params, socket), do: {:reply, :accepted, socket}

Pushing "work" from a Slipstream client kills the connection process:

** (FunctionClauseError) no function clause matching in Slipstream.Events.ReplyReceived.to_reply/1
    [%{"response" => %{}, "status" => "accepted"}]

A server that sends a WebSocket ping fails the same way in Slipstream.Connection.Impl.decode_message/2 with {:ping, "..."}.

Changes

  • Ping and pong frames are handled; pings are answered with a pong, using the code that was already there.
  • Invalid frames fail the connection, as RFC 6455 requires (e.g. section 8.1 for invalid UTF-8). The client gets handle_disconnect/2 with {:invalid_frame, reason}.
  • Statuses other than ok/error reach handle_reply/3 as strings, "accepted" or {"queued", response}, so no atoms are created from server input. The reply() type and its docs are updated.
  • A phx_error for an unknown join ref is ignored.

Compatibility

  • The reply() type is wider. Replies with other statuses used to crash the connection, so no working code depends on the old behaviour.
  • An invalid frame now closes the connection instead of crashing it.

How this was found, and AI use

I found this during a review of Slipstream's memory and timeout behaviour that I did with Claude Code. Claude reproduced these crashes against main with the fake server above and wrote this change and its tests.

Several frames a remote server may legitimately send crashed the
Slipstream.Connection process with a FunctionClauseError:

* Ping and pong frames. Mint.WebSocket decodes these as `{:ping, data}`
  and `{:pong, data}`, but `Impl.decode_message/2` only matched the bare
  `:ping`/`:pong` atoms, so the existing PingReceived/PongReceived
  handling (and the automatic pong reply) was unreachable.
* Invalid frames, e.g. a text frame which is not valid UTF-8. Mint.WebSocket
  includes `{:error, reason}` entries in the decoded frame list. These now
  fail the connection as RFC 6455 requires, with a ChannelClosed reason of
  `{:invalid_frame, reason}`.
* Replies with a status other than "ok" or "error". Phoenix allows any atom
  status (e.g. `{:reply, {:accepted, %{}}, socket}`). These are now given to
  `c:Slipstream.handle_reply/3` with the status as a string, so no atoms are
  created from server input.
* A `phx_error` whose ref is not a tracked join ref hit a
  `true = State.join_ref?(...)` assertion. It is now ignored.

Because the client process does not monitor the connection process, each
of these crashes left the client believing it was still connected with no
`handle_disconnect/2` and no reconnect.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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

Adding String.t() and {String.t(), json_serializable()} to the reply() type makes sense to me 👍. I would prefer that over coercing them to atoms.

This line of PRs looks pretty good but there is a CI failure from a compiler warning on Elixir 1.20 as mentioned in #89 which needs a fix. I'll give these PRs a closer look when I get a chance

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.

2 participants