exact1990

exact1990

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.14.0 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?

Showing Posts 1 to 4

al2o3cr

al2o3cr

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.

joey_the_snake

joey_the_snake

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.

joey_the_snake

joey_the_snake

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.

CherryPoppins

CherryPoppins

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews