Skip to content

Commit c6270f6

Browse files
committed
Fix Transport heartbeat storing control sockets. Closes phoenixframework#562
Control sockets for heartbeat messages were being tracked by the transport, causing leave events to be dispatched to nowhere.
1 parent 667d662 commit c6270f6

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
## v0.8.0-dev
44
* Enhancements
55
* [Router] `protect_from_forgery` has been added to the Router for CSRF protection. This is automatically plugged in new projects. See [this example](https://github.com/phoenixframework/phoenix/blob/ce5ebf3d9de4412a18e6325cd0d34e1b9699fb5a/priv/template/web/router.ex#L7) for plugging in your existing router pipeline(s).
6+
* Bug Fixes
7+
* [Channel] Fix WebSocket Heartbeat causing unnecessary `%Phoenix.Socket{}`'s to be tracked and leave errors on disconnect.
68
* Backwards incompatible changes
79
* Endpoints should now be explicitly started in your application supervision tree. Just add `worker(YourApp.Endpoint, [])` to your supervision tree in `lib/your_app.ex`
810
* `mix phoenix.start` was renamed to `mix phoenix.server`

lib/phoenix/channel/transport.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ defmodule Phoenix.Channel.Transport do
6868
|> case do
6969
{:ok, socket} ->
7070
{:ok, HashDict.put(sockets, {msg.channel, msg.topic}, socket)}
71+
{:heartbeat, _socket} ->
72+
{:ok, sockets}
7173
{:error, _socket, reason} ->
7274
{:error, sockets, reason}
7375
end
@@ -89,7 +91,7 @@ defmodule Phoenix.Channel.Transport do
8991
msg = %Message{channel: "phoenix", topic: "conn", event: "heartbeat", message: %{}}
9092
send socket.pid, msg
9193

92-
{:ok, socket}
94+
{:heartbeat, socket}
9395
end
9496
def dispatch(socket, channel, "join", msg) do
9597
socket

test/phoenix/channel_test.exs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,13 @@ defmodule Phoenix.Channel.ChannelTest do
216216
end
217217
end
218218

219-
test "phoenix channel returns heartbeat message when received" do
220-
sockets = HashDict.put(HashDict.new, {"phoenix", "conn"}, new_socket)
219+
test "returns heartbeat message when received, and does not store socket" do
220+
sockets = HashDict.new
221221
message = %Message{channel: "phoenix", topic: "conn", event: "heartbeat", message: %{}}
222222

223-
assert match?({:ok, _sockets}, Transport.dispatch(message, sockets, self, Router))
223+
assert {:ok, sockets} = Transport.dispatch(message, sockets, self, Router)
224224
assert_received %Message{channel: "phoenix", topic: "conn", event: "heartbeat", message: %{}}
225+
assert sockets == HashDict.new
225226
end
226227

227228
test "socket state can change when receiving regular process messages" do
@@ -242,4 +243,6 @@ defmodule Phoenix.Channel.ChannelTest do
242243
_socket = MyChannel.event(socket, "get", %{"key" => :val})
243244
assert_received 123
244245
end
246+
247+
245248
end

0 commit comments

Comments
 (0)