Skip to content

Return errors from push/5 instead of exiting the caller - #90

Open
joshk wants to merge 1 commit into
CuatroElixir:mainfrom
joshk:fix/push-errors-dont-exit-caller
Open

joshk wants to merge 1 commit into
CuatroElixir:mainfrom
joshk:fix/push-errors-dont-exit-caller

Conversation

@joshk

@joshk joshk commented Oct 18, 2026

Copy link
Copy Markdown

Problem

push/5 is a GenServer.call/3 to the connection process (lib/slipstream/command_router.ex:54, lib/slipstream/socket.ex:240), and the call isn't guarded:

The comment above the fallback clause in push/5 (lib/slipstream.ex:1094) expected these to come back as errors.

Separately, payloads are encoded inside the connection process. A push or join whose params the serializer can't encode raises there and kills the connection; examples are a map containing a pid, or a topic longer than 255 bytes for a binary push.

Reproduction

# a payload the serializer can't encode crashes the connection process
push(socket, "rooms:lobby", "event", %{pid: self()})

# a push while the connection is busy exits the client process
:sys.suspend(Slipstream.Socket.channel_pid(socket))
push(socket, "rooms:lobby", "event", %{}, 100)
# ** (exit) {:timeout, {GenServer, :call, ...}}

Changes

  • Socket.call/3 catches exits, so push/5 returns {:error, :timeout} or {:error, :not_connected} instead.
  • A send failure during a push replies {:error, {:send_failure, reason}} before the connection stops.
  • A push whose payload can't be encoded returns {:error, %Slipstream.Serializer.EncodeError{}}, and the connection stays up.
  • A join whose params can't be encoded fails with {:failed_to_join, {:encode_error, exception}} through handle_topic_close/3 / await_join/3, and the connection stays up.
  • The possible errors are listed in the push/5 docs.
  • On success the connection still replies with the bare ref, so Slipstream.SocketTest.assert_push/5 is unchanged.

Compatibility

push/5 no longer exits the caller in these cases; it returns an error. On OTP 24+ a late reply after a timeout is dropped (calls use process aliases), so no stray message arrives in the client's mailbox.

Related to #76: in test mode, a push from handle_call/3 while the test process is blocked now returns {:error, :timeout} instead of crashing the client. The deadlock itself remains, as discussed there.

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 the client exit against main while testing the blocked-send case from #85, and wrote this change and its tests.

`push/5` is a `GenServer.call/3` to the connection process, but the call
was not guarded, so:

* a push which was not accepted within `timeout` (e.g. the connection
  process is busy or blocked writing to the socket) exited the client
  process with `{:timeout, {GenServer, :call, _}}`
* a connection process which exited or stopped during the call (e.g. a
  send failure, which stopped the connection without replying) exited the
  client process too

The code comment above the fallback clause expected these to be returned
as errors. `Socket.call/3` now catches the exits and `push/5` returns
`{:error, :timeout}` or `{:error, :not_connected}`, and a send failure
during a push replies `{:error, {:send_failure, reason}}` before the
connection stops.

Payloads are also encoded in the connection process, so a push or join
whose params the serializer could not encode (e.g. a map containing a pid,
or a topic over 255 bytes for a binary push) raised and crashed the
connection. A push now returns `{:error, %Slipstream.Serializer.EncodeError{}}`
and a join fails with `{:failed_to_join, {:encode_error, exception}}`;
the connection stays up.

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