Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/phoenix/socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,14 @@ defmodule Phoenix.Socket do
end

def __info__(%Broadcast{event: "disconnect"}, state) do
{:stop, {:shutdown, :disconnected}, state}
# Close code 1001 ("Going Away") signals the client that the connection
# is intentionally closed but a reconnect is expected — phoenix.js gates
# `reconnectTimer.scheduleTimeout()` on `closeCode !== 1000`, so without
# an explicit code here the default mapping of `{:shutdown, :disconnected}`
# (1000 in bandit ≥1.10.4) prevents the LiveSocket from reconnecting
# after a `phx.gen.auth`-style "log out everywhere" disconnect.
# See mtrudel/bandit#582.
{:stop, {:shutdown, :disconnected}, 1001, state}
end

def __info__(:socket_drain, state) do
Expand Down
4 changes: 4 additions & 0 deletions lib/phoenix/transports/long_poll_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ defmodule Phoenix.Transports.LongPoll.Server do
{:stop, reason, handler_state} ->
state = %{state | handler: {handler, handler_state}}
{:stop, reason, state}

{:stop, reason, _code, handler_state} ->
state = %{state | handler: {handler, handler_state}}
{:stop, reason, state}
end
end

Expand Down
16 changes: 16 additions & 0 deletions test/phoenix/socket/socket_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,20 @@ defmodule Phoenix.SocketTest do
assert DrainerSpecSocket.drainer_spec(drainer: false, endpoint: Endpoint) == :ignore
end
end

describe "__info__/2" do
alias Phoenix.Socket.Broadcast

test "disconnect broadcast emits close code 1001 so phoenix.js reconnects" do
# phoenix.js gates `reconnectTimer.scheduleTimeout()` on
# `closeCode !== 1000`. The default `{:shutdown, :disconnected}`
# mapping in bandit ≥1.10.4 is 1000, which suppresses reconnect;
# we pass 1001 ("Going Away") explicitly so the client retries.
state = make_ref()
msg = %Broadcast{topic: "t", event: "disconnect", payload: %{}}

assert {:stop, {:shutdown, :disconnected}, 1001, ^state} =
Phoenix.Socket.__info__(msg, state)
end
end
end