Postgres DBConnection.ConnectionError - timed out

i have a process that i want to modify the conection timeout in ecto, but it still wit the same conection time out this is the error:

[error] Postgrex.Protocol (#PID<0.319.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.481.0> timed out because it owned the connection for longer than 15000ms

(DBConnection.ConnectionError) tcp send: closed

1 Like

@CastLouis: Did you checked this article? It’s guide for probably what you want.

i check my configurations and looks like in the example, the problem is that when i run the process it took more time than 15ms, and i modify the conection timeout in test.exs and in all the files and it doesn’t work.

@CastLouis: It looks like another problem with (default?) configuration - ping @josevalim and @fishcakez.

To be sure, please update your dependencies (especially Ecto and Postgrex).

Please also give us some info:

  1. Your OS and source of Erlang and Elixir you use like: Distro Name - Version - default/name of repo
  2. Erlang and Elixir versions
  3. Your dependencies version (again especially Ecto and Postgrex)
  4. Your database version
  5. If possible please share a minimal example to reproduce your problem with timeout

Thanks @Eiji.

Keep in mind that the ownership timeout is controlled by the :ownership_timeout configuration. I think we improve the error message for it in more recent versions as well.

1 Like

Good morning
i was loading a .csv file with a lot of registry, it took a lot of time to complete, while it loading the

#Code of loading the file
defp parse_line_to_string(line) do
<<code::binary-size(2), _::binary-size(1), rest::binary>> = line
{code, rest}
end

the code to insert into the database:

defp parse_line_to_string(line) do
<<code::binary-size(2), _::binary-size(1), rest::binary>> = line
{code, rest}
end

#the configuration from ecto:
config :na_db,
ecto_repos: [NA.DB.Repo]

config :na_db, NA.DB.Repo,
adapter: Ecto.Adapters.Postgres,
database: “nadb”,
username: “dadmin”,
password: “d@dm!n”,
hostname: “127.0.0.1”,
pool: Ecto.Adapters.SQL.Sandbox,
port: “5432”,
timeout: 450000

i was changing and use :ownership_timeout and set more time and it doesn’t work.

I tried to simulate similar work (collecting data for more than 15s) and I got same error, however Supervisor for MyApp.Repo saves tests from not passing and I checked also database and all works! Elixir is just awesome!

@josevalim: Thanks! I added it to configuration and it still works same, but without errors like in this topic. I agree that we need a better error message, because I also through about other timeout configuration variables.
I have a question that is related to this topic. Is it safe to set much more bigger timeouts in configuration than default one or we need to accept some consequences? Imagine new blog will probably be rarely visited and that surprise of Elixir admin that it have a error every 15s :smiley:

@CastLouis: Make sure that you are changing it in right configuration file and be sure to follow all steps in tutorial I previously linked. As I already wrote I got same error in example application and solved it using both tutorial and José tip.

i was modifing the postgrex.ex, the property @timeout with more time, but the problem still,

@CastLouis: What you mean by postgrex.ex? You need to change configuration *.exs file in /config directory. Depends on environment that you are working on you should apply configuration change in appropriate configuration file like: test.exs.
Example config/test.exs file:

use Mix.Config

config :my_app, MyApp.Repo,
  adapter: Ecto.Adapters.Postgres,
  database: "my_app_test",
  hostname: "localhost",
  ownership_timeout: 60_000, # <- add this line
  password: "postgres",
  pool: Ecto.Adapters.SQL.Sandbox,
  username: "postgres"

i was modify postgrex @timeout property for chech, i was modifing the test.exs with ownership_timeout, pool_timeout and timeout too, but the problem persist and i dont know how can i find more information about it, i think the reset the machine if ecto see the changes let me.

@CastLouis: You missed something. Checked again Ecto tutorial step by step and be sure to add :ownership_timeout to test.exs. If you can publish your code or invite me to repository then I can fix it for you - I will create a PR. I have accounts on Github, BitBucket and GitLab

This are the modules that are in the tutorial, the database is create before
the mix.exs:
defmodule NA.DB.Mixfile do use Mix.Project
def project do [app: :na_db, version: “0.1.0”, build_path: “…/…/_build”, config_path: “…/…/config/config.exs”, deps_path: “…/…/deps”, lockfile: “…/…/mix.lock”, elixir: “~> 1.4”, build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, deps: deps()] end
def application do [ extra_applications: [:logger], mod: {NA.DB, []} ] end
defp deps do [ {:postgrex, “~> 0.13”}, {:ecto, “~> 2.1”}, ] endend

the repo.ex:
defmodule NA.DB.Repo do use Ecto.Repo, otp_app: :na_dbend

the na_db.ex start function:
def start(_type, _args) do import Supervisor.Spec, warn: false
children = [ supervisor(NA.DB.Repo, [get_opts()]), ]
opts = [strategy: :one_for_one, name: NA.DB.Supervisor]
Supervisor.start_link(children, opts) end

The test.config:
config :na_db, NA.DB.Repo, adapter: Ecto.Adapters.Postgres, database: “nadb”, username: “dadmin”, password: “d@dm!n”, ownership_timeout: 60_000, hostname: “127.0.0.1”, pool: Ecto.Adapters.SQL.Sandbox, port: “5432”, timeout: 450000

The config/config.exs
import_config “#{Mix.env}.exs”

after that in the tutorial is on creating the tables. could you please help me, it all the information that i have it.

@CastLouis: so it’s a sub application in umbrella project. Make sure that other application does not override it’s configuration, for example:
File: umbralla_app/apps/a:

config :my_app, :something, true

could be overwritten by umbreall_app/apss/b:

config :my_app, :something, false

Please also update your post with proper markdown:

Please use this syntax:
```elixir
# your code goes here …
```

1 Like

@CastLouis: ahh, I see what you have missed:

The config/config.exs
import_config “#{Mix.env}.exs”

after that in the tutorial is on creating the tables. could you please help me, it all the information that i have it.

After that yes it’s about create example table and work on it, but after it there is chapter called: Tests and functionality! Please read whole article and not only copy and paste code. :smiley:

Jaja, no i am not just copy i try to understand this error, i follow your instructions and i add this line of code in my config/config.exs and it looks like this:

config :na_db, :ecto_repos, [NA.DB.Repo]
import_config “#{Mix.env}.exs”

and the problem still, now it throws this information:

test read my claim (NA.Claims.ClaimHistoryLoaderTest)
test/na_claims/claim_history_loader_test.exs:3
** (exit) exited in: GenServer.call(#PID<0.489.0>, {:checkout, #Reference<0.0.2.28900>, true, 450000}, 5000)
** (EXIT) shutdown: “owner #PID<0.483.0> timed out because it owned the connection for longer than 15000ms”

450000 and 5000 i don’t know where are those values, one question after doing the change i need to restart something?

@CastLouis: you do not need to restart anything - just run tests by mix test one more time after you change configuration - or in case you are changing for example ports then you need to be sure that 3rd party application is already runned on that port and if not reconfigure and restart it, but simple change any timeout variable do not need to restart any 3rd party applications.

Starting from my previous comment you answered that you modified only: config/config.exs - please make sure that you read whole article. Last chapter is focusing on tests - there are more changes to do than modify only one file.

And you still do not using special markup :slight_smile:

Sometimes helpful practise is to create new project and start from 0, so:

  1. create new elixir project
  2. setup credo
  3. merge your code part by part and test it
    It’s possible to make a typo like: :timaout which is not easy to notice :slight_smile:

Excuse me

config :na_db, :ecto_repos, [NA.DB.Repo]
import_config "#{Mix.env}.exs"

StackTrace:

1) test read my claim (NA.Claims.ClaimHistoryLoaderTest)
     test/na_claims/claim_history_loader_test.exs:3
     ** (exit) exited in: GenServer.call(#PID<0.489.0>, {:checkout, #Reference<0.0.2.33413>, true, 450000}, 5000)
         ** (EXIT) shutdown: "owner #PID<0.483.0> timed out because it owned the connection for longer than 15000ms"
       (elixir) lib/enum.ex:1768: Enum."-each/2-anonymous-3-"/3
       (elixir) lib/stream.ex:1385: anonymous fn/3 in Enumerable.Stream.reduce/3
       (elixir) lib/stream.ex:1259: Stream.do_resource/5
       (elixir) lib/stream.ex:1405: Enumerable.Stream.do_each/4
       (elixir) lib/enum.ex:1767: Enum.each/2
       test/na_claims/claim_history_loader_test.exs:4: (test)

Loader:

`defp store_claim(pid) do    
NA.DB.Repo.Claims.insert_claim(make_claim(pid), NA.Claims.ClaimAgent.get_claim_fields(pid)                                                     |> Enum.map(fn(x) -> struct(NA.DB.Schema.ClaimField, Map.from_struct(x)) end))  end`

@CastLouis: You have still a problem with 2nd code, but not I see better your problem (syntax highlighting is really helpful :slight_smile:
Look at docs - it have more description. Hope you will better understand your error.

I have now a similiar DBConnection.ConnectionError with PostgreSQL but was after the computer fronzen the system and complains an error System on my Mac OS.
So I was reading here and what I do and now worked for me was Update the PostgreSQL.
So check for the Updates and try.