1

在我的频道中寻找测试terminate/2 回调的帮助。

测试和设置如下所示:

setup do
  :ok = Ecto.Adapters.SQL.Sandbox.checkout(MyApp.Repo)
  Ecto.Adapters.SQL.Sandbox.mode(MyApp.Repo, {:shared, self()})

  {:ok, socket} = connect(UserSocket, %{token: "some_token"})
  {:ok, %{}, socket} = subscribe_and_join(socket, "some_channel", %{})

  %{socket: socket}
end

test "terminate/2", %{socket: socket} do
  # for avoiding "** (EXIT from #PID<...>) {:shutdown, :closed}"
  Process.unlink(socket.channel_pid)

  assert close(socket) == :ok
  # some additional asserts go here
end

在 terminate/2 方法中,我只是调用了一个辅助模块,我们将其命名为TerminationHandler

def terminate(_reason, _socket) do
  TerminationHandler.call()
end

TerminationHandler 中的 call/0 方法包含一个数据库查询。它可以看起来像这样,即

def call() do
  users = User |> where([u], u.type == "super") |> Repo.all # line where error appears
  # some extra logic goes here
end

这是我定期收到的错误(可能在 10 次运行中出现一次)

14:31:29.312 [error] GenServer #PID<0.1041.0> terminating
** (stop) exited in: GenServer.call(#PID<0.1040.0>, {:checkout, #Reference<0.3713952378.42205187.247763>, true, 60000}, 5000)
    ** (EXIT) shutdown: "owner #PID<0.1039.0> exited with: shutdown"
    (db_connection) lib/db_connection/ownership/proxy.ex:32: DBConnection.Ownership.Proxy.checkout/2
    (db_connection) lib/db_connection.ex:928: DBConnection.checkout/2
    (db_connection) lib/db_connection.ex:750: DBConnection.run/3
    (db_connection) lib/db_connection.ex:644: DBConnection.execute/4
    (ecto) lib/ecto/adapters/postgres/connection.ex:98: Ecto.Adapters.Postgres.Connection.execute/4
    (ecto) lib/ecto/adapters/sql.ex:256: Ecto.Adapters.SQL.sql_call/6
    (ecto) lib/ecto/adapters/sql.ex:436: Ecto.Adapters.SQL.execute_or_reset/7
    (ecto) lib/ecto/repo/queryable.ex:133: Ecto.Repo.Queryable.execute/5
    (ecto) lib/ecto/repo/queryable.ex:37: Ecto.Repo.Queryable.all/4
    (my_app) lib/my_app/helpers/termination_handler.ex:4: MyApp.Helpers.TerminationHandler.call/0
    (stdlib) gen_server.erl:673: :gen_server.try_terminate/3
    (stdlib) gen_server.erl:858: :gen_server.terminate/10
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: {:join, Phoenix.Channel.Server}Last message: {:join, Phoenix.Channel.Server}

将不胜感激任何有关此错误的原因和可能的方法来避免它的回应。

4

2 回答 2

1

如文档中所述GenServer.terminate/2

[...] 主管将发送退出信号:shutdown,并且GenServer将在超时持续时间内终止。如果在此超时持续时间之后进程仍然存在,它将立即被杀死。

这似乎是你的情况。DBConnection.checkout/2似乎正在等待可用连接出现,并且持续时间超过超时。因此,所有者经历了残酷的杀戮。

可能有两种可能的解决方案:

  • 增加关机超时(我会避免)
  • 增加允许的同时数据库连接的数量。

在任何情况下都可能需要后者,因为您的池似乎已满。这样连接将立即被签出,并且它应该在超时间隔内成功返回。

于 2019-10-23T15:40:15.657 回答
0

This might help.

  defmacro leave_channel(socket) do
    quote do
      Process.unlink(unquote(socket).channel_pid)
      mref = Process.monitor(unquote(socket).channel_pid)
      ref = leave(unquote(socket))
      assert_reply ref, :ok
      assert_receive {:DOWN, ^mref, :process, _pid, _reason}
    end
  end


  defmacro close_socket(socket) do
    quote do
      Process.unlink(unquote(socket).channel_pid)
      mref = Process.monitor(unquote(socket).channel_pid)
      close(unquote(socket))
      assert_receive {:DOWN, ^mref, :process, _pid, _reason}
    end
  end
于 2020-07-01T16:52:39.470 回答