Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only allow update if pid is on current node #184

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions lib/phoenix/tracker/shard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,9 @@ defmodule Phoenix.Tracker.Shard do
"""
end

defp handle_update({pid, _topic, _key, _meta_updater}, state) when node(pid) != node(),
do: {:reply, {:error, :not_owner}, state}

defp handle_update({pid, topic, key, meta_updater}, state) do
case State.get_by_pid(state.presences, pid, topic, key) do
nil ->
Expand Down
17 changes: 17 additions & 0 deletions test/phoenix/tracker/shard_replication_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,23 @@ defmodule Phoenix.Tracker.ShardReplicationTest do
test "updating with no prior presence", %{shard: shard, topic: topic} do
assert {:error, :nopresence} = Shard.update(shard, self(), topic, "u1", %{})
end

test "updating a presence on a different node", %{topic: topic, tracker: tracker} do
pid = self()
subscribe(topic)

# Track presence on node1
{_node1_pid, {:ok, node1_server}} = start_shard(@node1, name: tracker)
track_presence(@node1, node1_server, pid, topic, "u1", %{name: "s1"})

# Bring up node2 and wait for replication
{_node2_pid, {:ok, node2_server}} = start_shard(@node2, name: tracker)
assert_join ^topic, "u1", %{name: "s1"}
assert %{@node1 => %Replica{status: :up}} = replicas(node2_server)

# Attempt to update presence on node2
assert {:error, :not_owner} = Shard.update(node2_server, pid, topic, "u1", %{name: "s2"})
end

test "duplicate tracking", %{shard: shard, topic: topic} do
pid = self()
Expand Down