Skip to content

Commit c73bbfc

Browse files
committed
Drop incoming messages with stale join refs
1 parent f16aa8f commit c73bbfc

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

lib/phoenix/socket.ex

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,9 +765,19 @@ defmodule Phoenix.Socket do
765765
end
766766
end
767767

768-
defp handle_in({pid, _ref, _status}, message, state, socket) do
769-
send(pid, message)
770-
{:ok, {state, socket}}
768+
defp handle_in({pid, _ref, _status}, msg, state, socket) do
769+
%{topic: topic, join_ref: join_ref} = msg
770+
771+
case state.channels_inverse do
772+
# we need to match on nil to handle v1 protocol
773+
%{^pid => {^topic, existing_join_ref}} when existing_join_ref in [join_ref, nil] ->
774+
send(pid, msg)
775+
{:ok, {state, socket}}
776+
777+
# the client has sent a stale message to a previous join_ref, ignore
778+
%{^pid => {^topic, _old_join_ref}} ->
779+
{:ok, {state, socket}}
780+
end
771781
end
772782

773783
defp handle_in(

test/phoenix/integration/long_poll_channels_test.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,14 +436,16 @@ defmodule Phoenix.Integration.LongPollChannelsTest do
436436

437437
test "#{@mode}: publishing events" do
438438
Phoenix.PubSub.subscribe(__MODULE__, "room:lobby")
439-
session = join("/ws", "room:lobby", @vsn, "1", @mode)
439+
join_ref = "1"
440+
session = join("/ws", "room:lobby", @vsn, join_ref, @mode)
440441

441442
# Publish successfully
442443
resp =
443444
poll(:post, "/ws", @vsn, session, %{
444445
"topic" => "room:lobby",
445446
"event" => "new_msg",
446447
"ref" => "1",
448+
"join_ref" => join_ref,
447449
"payload" => %{"body" => "hi!"}
448450
})
449451

0 commit comments

Comments
 (0)