DynamicSupervisor and Ecto.Adapters.SQL.Sandbox problem

When starting to use DynamicSupervisor, the tests started to fail with the message:

09:09:55.169 [error] GenServer {:element_process_registry, "a4d69ba4-6f55-40fa-8c2e-eea9c5cecf27"} terminating
** (DBConnection.OwnershipError) cannot find ownership process for #PID<0.495.0>.

When using ownership, you must manage connections in one of the four ways:

  • By explicitly checking out a connection
  • By explicitly allowing a spawned process
  • By running the pool in shared mode
  • By using :caller option with allowed process
    The first two options require every new process to explicitly check a connection out or be allowed by calling checkout or allow respectively.
    The third option requires a {:shared, pid} mode to be set. If using shared mode in tests, make sure your tests are not async.
    The fourth option requires [caller: pid] to be used when checking out a connection from the pool. The caller process should already be allowed on a connection.
    If you are reading this error, it means you have not done one of the steps above or that the owner process has crashed.
    See Ecto.Adapters.SQL.Sandbox docs for more information.

It seems that the problem is not complicated and known:
Ecto.Adapters.SQL.Sandbox — Ecto SQL v3.8.3 Ecto.Adapters.SQL.Sandbox — Ecto SQL v3.8.3
Problems with Ecto Sandbox · Issue #167 · antonmi/espec · GitHub

Tests are run using espec
The tests take into account recommendations from the documentation
spec_helper.rb


ESpec.configure fn(config) ->
  config.before fn(_tags) ->
    {:ok, _} = Application.ensure_all_started(:ex_machina)
  end

  config.finally fn(_shared) ->
    :ok
  end
end

ESpec.configure fn(config) ->
  config.before fn (shared) ->
    case Ecto.Adapters.SQL.Sandbox.checkout(Krake.Repo) do
      :ok -> nil
      {:already, :owner} -> nil
      error -> IO.puts "Sandbox checkout error: #{ error }"
    end

    unless Map.has_key?(shared, :async) do
      Ecto.Adapters.SQL.Sandbox.mode(Krake.Repo, {:shared, self()})
    end
  end

  config.finally fn(_shared) ->
    Ecto.Adapters.SQL.Sandbox.checkin(Krake.Repo, [])
  end
end

example_test.exs:
use Krake.Web.ConnCase, async: true

Inside the test, the controller call, in the controller, the supervisor process is called::
DynamicSupervisor.start_child(__MODULE__, {ElementProcess, [element_id, ttl]})

Maybe there are some ideas?

A general note: changing the sandbox mode affects what happens when you call checkout, so these should likely be in the opposite order.

Where does the ElementProcess get stopped? These ownership errors usually happen when a GenServer outlives the process that called checkout.

1 Like

If I understand what you’re saying, you are using the code above and also setting async: true. This means the sandbox won’t enter shared mode and any process outside your test process will not be able to share its connection.

1 Like

For shared mode the checkout actually has to happen first. The checkout requests go through a GenServer which will have its state set with the owner’s connection when changing the mode to shared.

Check this blog out: How LiveView got rid of dangling processes in tests – and how we can do the same

The gist is you might try to start the genserver under the test supervisor so it doesn’t outlive the individual test process.