From 3a8dec76a039201c656442c25d4731c26296fac1 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 24 Feb 2014 11:39:43 +0000 Subject: [PATCH 1/3] Set the connection async earlier in green mode The moment it is called shouldn't have really changed, but it's more explicit when it happens. Previously it was sort of obfuscated behind a roundtrip through the green callback and poll. --- psycopg/connection_int.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c index dcfeb0bc..425835df 100644 --- a/psycopg/connection_int.c +++ b/psycopg/connection_int.c @@ -506,10 +506,6 @@ conn_setup(connectionObject *self, PGconn *pgconn) pthread_mutex_lock(&self->lock); Py_BLOCK_THREADS; - if (psyco_green() && (0 > pq_set_non_blocking(self, 1))) { - return -1; - } - if (!conn_is_datestyle_ok(self->pgconn)) { int res; Py_UNBLOCK_THREADS; @@ -573,6 +569,9 @@ _conn_sync_connect(connectionObject *self) /* if the connection is green, wait to finish connection */ if (green) { + if (0 > pq_set_non_blocking(self, 1)) { + return -1; + } if (0 != psyco_wait(self)) { return -1; } From 33db3ecfc3eff7a7d064eafb3d592d6012127609 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 24 Feb 2014 12:12:16 +0000 Subject: [PATCH 2/3] Set the connection async before polling for connection It should fix ticket #194 --- psycopg/connection_int.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c index 425835df..ef29566a 100644 --- a/psycopg/connection_int.c +++ b/psycopg/connection_int.c @@ -613,6 +613,11 @@ _conn_async_connect(connectionObject *self) PQsetNoticeProcessor(pgconn, conn_notice_callback, (void*)self); + /* Set the connection to nonblocking now. */ + if (pq_set_non_blocking(self, 1) != 0) { + return -1; + } + /* The connection will be completed banging on poll(): * First with _conn_poll_connecting() that will finish connection, * then with _conn_poll_setup_async() that will do the same job @@ -787,11 +792,6 @@ _conn_poll_setup_async(connectionObject *self) switch (self->status) { case CONN_STATUS_CONNECTING: - /* Set the connection to nonblocking now. */ - if (pq_set_non_blocking(self, 1) != 0) { - break; - } - self->equote = conn_get_standard_conforming_strings(self->pgconn); self->protocol = conn_get_protocol_version(self->pgconn); self->server_version = conn_get_server_version(self->pgconn); From 956560325ede2fad8e1725915b4323d7938482d1 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 24 Feb 2014 15:15:54 +0000 Subject: [PATCH 3/3] Bug #194 confirmed fixed: newsfile updated. Conflicts: NEWS --- NEWS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS b/NEWS index cc1633bc..d57fefd9 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,8 @@ What's new in psycopg 2.4.7 Alexey Borzenkov (:ticket:`#173`). - Manually creating `lobject` with the wrong parameter doesn't segfault (:ticket:`#187`). + - Avoid blocking async connections on connect (:ticket:`#194`). Thanks to + Adam Petrovich for the bug report and diagnosis. - Fixed debug build on Windows, thanks to James Emerton.