Tests sometimes fail because Repo isn't started

Hello everybody,

Sometimes my test suite fails with errors like this:

** (RuntimeError) could not lookup Manager.Repo because it was not started or it does not exist

On my colleague’s machine it happens a lot, much less on my machine and almost never on our CI server. So I guess it’s some kind of race condition but I don’t know what’s causing it. The ecto repo is the first child of our top-level supervisor but apparently that’s not always enough.

Is there a way to ensure the ecto repo is correctly started before trying to run any test?

Tell us more about your app. Is this umbrella project by any chance and the failing tests come from different app than the one which defined Repo?

3 Likes

I also have this issue from time to time.

I’m sure it started after upgrading libraries but I didn’t bother inspecting it yet. Might do it tomorrow.

Your colleague is not alone.

2 Likes

It is an umbrella app in which only one app has a repo. Failing tests are from the app that has the repo.

The problem occurs both when we run tests from the umbrella top level directory and we run them from the app subdirectory.

That being said, that app which has a repo and failing tests also specifies another app, which also has its own repo, as a :path dependency in its mix.exs deps entry. I don’t see any reference to that other app in the tests errors though.

1 Like

Same problem for me. I have an umbrella app that I’m trying to migrate from Ecto 2.x to Ecto 3.x

Running the tests on the single apps (10+ apps) works as expected.

Running the tests from the umbrella gives several errors on all the apps, like this:

** (RuntimeError) could not lookup SomeApp.Repo because it was not started or it does not exist

Checked configs and settings, it looks like all is correct, after all they all passed (1000+ tests) before migration to ecto 3.x and, as I said, tests are failing only when I run them from the umbrella.

What is most likely happening is that your supervision tree is restarting, temporarily bringing down the ecto repo supervisor. You can verify it by adding this to your repo:

  def init(_type, config) do
    IO.puts "STARTING REPO"
    {:ok, config}
  end
6 Likes

Hi @josevalim thank you for your quick reply.

Sorry for delay, but the next day I went on vacation and I didn’t tried.

Now that I’m back to work, I tried your suggestion and added the puts on all the Repo modules in the apps of the umbrella. What I’ve noted is that the message STARTING X REPO for each app is displayed at the very beginning of the test run (mix test on the umbrella app), but then I get the same errors when tests are executed.

So, your hypothesis is correct, do you have any suggestions on how to fix this?

Thank you in advance.

1 Like

You need to figure out what is causing your supervision tree to restart and change it accordingly so it doesn’t restart all the time. You can enable handle_sasl_reports in Logger to get more complete reports.

3 Likes