Intermittent DBConnection.ConnectionError in tests

When running my tests, I sometimes see an error that looks like

[error] Postgrex.Protocol (#PID<0.788.0>) disconnected: ** (DBConnection.ConnectionError) client #PID<0.7569.0> exited

Notable details:

  • It doesn’t always happen
  • When it does happen, it’s not always in the same test or test file
  • It doesn’t actually cause any tests to fail; it just shows up as output in the middle of the output like so:

I’ve been ignoring it up to this point because the test suite still passes and it hasn’t caused any development or deployment bottlenecks, but I’d like to figure out why it’s happening and what I might be able to do to stop it.

Version details, in case it matters:

  • Postgres v10.19 via the postgres:10-alpine Docker image
  • postgrex v0.16.1
  • ecto v3.7.1
  • ecto_sql v3.7.2
  • ecto_psql_extras v0.7.4

Any thoughts?

The usual cause of that message is code that’s still waiting for the result of a Repo call when the test process shuts down.

2 Likes

Is there a particular strategy I can use to keep the test process alive until the Repo call completes? I’m not doing anything particularly unusual in any tests, though I think some of my on_exit teardown code might involve some database cleanup. When testing code that relies on DB triggers, I have to run the tests unsandboxed or the triggers won’t fire. So those particular tests get cleaned up manually.

1 Like

I think we’re having the same issue and we haven’t nailed the root cause either. Things I’d look into:

Could it be a Task that gets spawned somewhere?

Did anyone figure out a fix for this? I’m having the same issue with my tests and since the errors appear to be fairly random, I have no idea how to even begin fixing them.

Still haven’t figured it out. Other priorities have prevented me from spending much time on it. I don’t think it’s a Task. It might be a GenServer that gets started by the test setup, but I do have code to stop it in the on_exit. So for now I’m still stumped.