PG.connect ignores connect_timeout

Issue #188 invalid
Brian Williams
created an issue

We're using ruby-pg 0.17.1 on Ubuntu 12.04 with libpq 9.3.5-1.pgdg12.4+1 to connect to an AWS Redshift instance. We use PG.connect and are passing in the connect_timeout option set to 10. The connect_timeout setting seems to be ignored, as there is never an exception raised and occasionally the process gets blocked on that line, needing a restart to recover.

Comments (8)

  1. Michael Granger repo owner

    Hi Bruce.

    I tried to write a test to see what was going on with this, and realized that we hadn't yet implemented PG::Connection#conninfo, which returns the connection options used by the connection. Once we'd implemented that, the spec that tested for connect_timeout showed that the connection is being passed to libpq by the Ruby binding, so I'm not sure what else I can do beyond that.

    I'm going to push a prerelease of pg 0.18.0 in a few minutes; would you mind installing that somewhere and ensuring that conn.conninfo_hash[:connect_timeout] has the setting you're passing to it after you connect?

    Also: are you expecting the connect_timeout to do something other than cause the initial connection to fail if it takes longer than the value you specify? In my reading of your issue it seems like you might be expecting it to also detect if the connection has hung after the initial connection, which I don't believe connect_timeout will do.

  2. Brian Williams reporter

    Our Ruby process appears to hang on PG.connect(@db_conf) where @db_conf is a hash with the key connect_timeout set to 15. We kill the process whenever we detect it hanging for more than 30 minutes. We expect an error to be raised after 15 seconds.

  3. Michael Granger repo owner

    Thanks Lars! I should have checked that. I was surprised it wasn't already implemented; I guess that explains it.

    And I agree: conndefaults_hash is a great idea.

  4. Michael Granger repo owner


    First of all, sorry for calling you Bruce before; I have a friend named Bruce Williams and I for some reason just read your name as his. I evidently need to stop replying to issues before I've had my morning coffee.

    The connect_timeout setting only applies to the initial connection. If you want to detect a hung connection after the initial connect succeeds, you'll need to use the keepalive options (if your environment supports it), or do something like periodically send a trivial query via the nonblocking API to detect it yourself.

  5. Log in to comment