PGconn.connect_start blocks the whole ruby process

Issue #165 resolved
Masashi Miyazaki created an issue

When trying to connect to the some specific unresolved hostname(ex:, PG::Connection.connect_start blocks the whole ruby process until timeout.

I'm using 'pg' gem to connect to an Amazon Redshift Cluster, and it happens when trying to connect to a terminated cluster.

You can reproduce it with the attachment file. The result is as following.

$ ruby test_ruby_db_drivers_block_in_connect2.rb pg
....................[pg: connecting to unreachable database (]
test_ruby_db_drivers_block_in_connect3.rb:31:in `connect_start': could not translate host name "" to address: nodename nor servname provided, or not known (PG::Error)
     from test_ruby_db_drivers_block_in_connect3.rb:31:in `<main>'

Comments (6)

  1. Lars Kanis

    Thanks for reporting this! The PostgreSQL documentation has some notes about the conditions for non blocking behavior: This makes sure that connect_start returns immediately and so will also not block other ruby threads. In your case libpq must do a DNS query and will block therefore.

    Nevertheless, the connection functions in ruby-pg should not not block other ruby threads in any case. So I'll change them to release the GVL accordingly.

  2. Masashi Miyazaki reporter

    Thank you for your quick comments and fixing! I understood the problem. The workaround that you suggested works for me.

  3. Log in to comment