Search code examples
ruby-on-railsrubypostgresqlsidekiq

Postgres connection not closing after sidekiq Ruby script


It's a small Ruby script running under Sidekiq. It opens a connection with

    db_connect = Sequel.connect(@db_credential, search_path: @namespace)

It never explicitly closes the connection; I think this is not supposed to be necessary?

After the script has has been run many times, and they have all completed, and the Sidekiq web panel shows no tasks running or queued, Postgres shows 60 Sidekiq connections:

postgres=# select count(*) from pg_stat_activity where application_name like '%sidekiq%';
 count 
-------
    60
(1 row)

The database is on localhost, so nothing else is creating these connections.

psql 9.3.6, Sidekiq 3.3.3, Rails 4.0.0, ruby 2.1.1p76, sequel 4.19.0, Ubuntu 14.04.2 LTS.


Solution

  • You can:

    I believe the issue with your current approach is that you're constructing a new connection pool on every Sidekiq tasks execution by calling Sequel.connect, and these connections keep hanging around. It may take a long time before they're actually garbage collected, if ever.