Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 423
ChrisPhoenix Avatar asked Jun 05 '26 01:06

ChrisPhoenix


1 Answers

You can:

  • Either use Sequel pooling by connecting only once and maintaining db_connect value between your Sidekiq tasks executions
  • Or you can connect every time, but then you have to disconnect manually by calling the disconnect method (http://sequel.jeremyevans.net/rdoc/classes/Sequel/Database.html#method-i-disconnect).

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.

like image 72
SkyWriter Avatar answered Jun 06 '26 16:06

SkyWriter