Skip to content

Notify the client when its connection process exits unexpectedly - #92

Open
joshk wants to merge 1 commit into
CuatroElixir:mainfrom
joshk:fix/client-monitors-connection
Open

joshk wants to merge 1 commit into
CuatroElixir:mainfrom
joshk:fix/client-monitors-connection

Conversation

@joshk

@joshk joshk commented Oct 18, 2026

Copy link
Copy Markdown

Problem

The client process only learns about a disconnect from the ChannelClosed event that the connection process sends just before it stops. If the connection process exits any other way, handle_disconnect/2 is never called. The default reconnect never happens, and the client stays disconnected indefinitely: connected?/1 returns false and nothing reconnects. For a long-lived client, such as a Nerves device in the field, only restarting the client recovers it.

The connection process already monitors the client (lib/slipstream/connection/state.ex:48), but not the other way round.

Reproduction

# in a connected client
Process.exit(Slipstream.Socket.channel_pid(socket), :kill)
# handle_disconnect/2 is never invoked and the client never reconnects

Changes

  • The client monitors the connection process once connected. This happens in Socket.apply_event/2 for ChannelConnected, which runs in the client process for both the callback and the await_connect/2 paths.
  • use Slipstream gains a handle_info/2 clause for that monitor's :DOWN, which is dispatched as a ChannelClosed with the reason {:connection_exited, exit_reason}.
  • On a normal ChannelClosed the monitor is removed and flushed, so a regular disconnect is still reported exactly once.
  • In test mode the "connection" is the test process, so it isn't monitored and Slipstream.SocketTest behaves as before.

Compatibility

  • handle_disconnect/2 can receive a new reason, {:connection_exited, reason}, which is documented.
  • %Slipstream.Socket{} gains a channel_monitor field.
  • A client's own handle_info({:DOWN, ...}, socket) clauses are unaffected: the new clause only matches the socket's own monitor ref.

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. While undergoing an adversarial review, Claude found that it was possible for the client to never notice that the connection had died, and wrote this change and its tests.

The client process never monitored the Slipstream.Connection process. It
only learned about disconnects from the ChannelClosed event which the
connection process sends before stopping. If the connection process exited
any other way (a crash, being killed, a bug), `handle_disconnect/2` was
never invoked, so the default reconnect never happened and the client sat
"disconnected" forever with `connected?/1` returning false.

The client now monitors the connection process once it is connected
(in `Socket.apply_event/2` for ChannelConnected, which runs in the client
process for both the callback and `await_connect/2` paths). A `:DOWN` for
that monitor is dispatched as a ChannelClosed event with the reason
`{:connection_exited, exit_reason}`. The monitor is removed (and flushed)
when a normal ChannelClosed event is applied so a regular disconnect is
still only reported once.

In test mode the "connection" is the test process, so it is not monitored
and `Slipstream.SocketTest` behaves as before.

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

1 participant