Skip to content
Merged
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
6 changes: 5 additions & 1 deletion lib/phoenix/socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,11 @@ 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
# reconnects behind a closeCode !== 1000 check.
# See https://github.com/mtrudel/bandit/issues/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 reconnects on `closeCode !== 1000`.
# Servers might interpret `{:shutdown, :disconnected}`
# as code 1000, so we pass 1001 explicitly to force a retry.
# See https://github.com/mtrudel/bandit/issues/582.
state = make_ref()
msg = %Broadcast{topic: "t", event: "disconnect", payload: %{}}

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