From a31c1a1722d26d4748ca2c32b4207df865f6967b Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 6 Mar 2014 17:49:24 +0000 Subject: [PATCH 1/7] Allow get_transaction_status on closed connections It's a local operation and the libpq functions has a NULL guard. --- NEWS | 1 + psycopg/connection_type.c | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/NEWS b/NEWS index d236eee6..b229795d 100644 --- a/NEWS +++ b/NEWS @@ -28,6 +28,7 @@ What's new in psycopg 2.5.3 - Don't segfault using poorly defined cursor subclasses which forgot to call the superclass init (:ticket:`#195`). - Fixed possible segfault in named cursors creation. +- It is now possible to call `get_transaction_status()` on closed connections. - Fixed debug build on Windows, thanks to James Emerton. diff --git a/psycopg/connection_type.c b/psycopg/connection_type.c index 2de1d24d..cb47b662 100644 --- a/psycopg/connection_type.c +++ b/psycopg/connection_type.c @@ -700,8 +700,6 @@ psyco_conn_set_client_encoding(connectionObject *self, PyObject *args) static PyObject * psyco_conn_get_transaction_status(connectionObject *self) { - EXC_IF_CONN_CLOSED(self); - return PyInt_FromLong((long)PQtransactionStatus(self->pgconn)); } From 696d12355008a2bb733aeafa55614d6ca28b769f Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 6 Mar 2014 18:11:17 +0000 Subject: [PATCH 2/7] Close a connection if PQexec returned NULL This happens for Socket connections, not for TCP ones, where a result containing an error is returned and correctly handled by pq_raise() Closes ticket #196 but not #192: poll() still doesn't change the connection closed. --- NEWS | 2 ++ psycopg/pqpath.c | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index b229795d..e0dd56e8 100644 --- a/NEWS +++ b/NEWS @@ -27,6 +27,8 @@ What's new in psycopg 2.5.3 (:ticket:`#203`). - Don't segfault using poorly defined cursor subclasses which forgot to call the superclass init (:ticket:`#195`). +- Mark the connection closed when a Socket connection is broken, as it + happens for TCP connections instead (:ticket:`#196`). - Fixed possible segfault in named cursors creation. - It is now possible to call `get_transaction_status()` on closed connections. - Fixed debug build on Windows, thanks to James Emerton. diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 86709657..0e9481a5 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -417,10 +417,18 @@ pq_complete_error(connectionObject *conn, PGresult **pgres, char **error) pq_raise(conn, NULL, pgres); /* now *pgres is null */ } - else if (*error != NULL) { - PyErr_SetString(OperationalError, *error); - } else { - PyErr_SetString(OperationalError, "unknown error"); + else { + if (*error != NULL) { + PyErr_SetString(OperationalError, *error); + } else { + PyErr_SetString(OperationalError, "unknown error"); + } + /* Trivia: with a broken socket connection PQexec returns NULL, so we + * end up here. With a TCP connection we get a pgres with an error + * instead, and the connection gets closed in the pq_raise call above + * (see ticket #196) + */ + conn->closed = 2; } if (*error) { From 2e55b35d5db7a4773be22909371ea285e0e09398 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 5 Apr 2014 15:10:15 +0100 Subject: [PATCH 3/7] Don't set an exception witout GIL closing lobjects with a bad conn We ended up in this branch only for an excessively aggressive closing of the transaction that now I'm going to fix. --- psycopg/lobject_int.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/psycopg/lobject_int.c b/psycopg/lobject_int.c index 2f78e90b..43f2b179 100644 --- a/psycopg/lobject_int.c +++ b/psycopg/lobject_int.c @@ -253,7 +253,7 @@ lobject_close_locked(lobjectObject *self, char **error) return 0; break; default: - PyErr_SetString(OperationalError, "the connection is broken"); + *error = strdup("the connection is broken"); return -1; break; } From 3752880b7b59979cf0eaad6e0190d46e29df524e Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 5 Apr 2014 15:11:43 +0100 Subject: [PATCH 4/7] Fixed attempt of closing an already closed lobject on dealloc This results in a "null without exception set" in the corrent state, which is caused by the connection being unexpectedly closed anyway. --- psycopg/lobject_type.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/psycopg/lobject_type.c b/psycopg/lobject_type.c index 1af352e3..068923c9 100644 --- a/psycopg/lobject_type.c +++ b/psycopg/lobject_type.c @@ -355,7 +355,7 @@ lobject_dealloc(PyObject* obj) { lobjectObject *self = (lobjectObject *)obj; - if (self->conn) { /* if not, init failed */ + if (self->conn && self->fd != -1) { if (lobject_close(self) < 0) PyErr_Print(); Py_XDECREF((PyObject*)self->conn); From 1654687d1b626c07dd74a154c16570fe1899a6d1 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 5 Apr 2014 15:14:00 +0100 Subject: [PATCH 5/7] Check the connection is really bad on exception before closing it We end up here without a pgres sometimes (e.g. from lobject errors) --- psycopg/pqpath.c | 4 +++- tests/test_lobject.py | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 0e9481a5..16b733f4 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -428,7 +428,9 @@ pq_complete_error(connectionObject *conn, PGresult **pgres, char **error) * instead, and the connection gets closed in the pq_raise call above * (see ticket #196) */ - conn->closed = 2; + if (CONNECTION_BAD == PQstatus(conn->pgconn)) { + conn->closed = 2; + } } if (*error) { diff --git a/tests/test_lobject.py b/tests/test_lobject.py index beb277b2..66a3d8a1 100755 --- a/tests/test_lobject.py +++ b/tests/test_lobject.py @@ -130,6 +130,7 @@ class LargeObjectTests(LargeObjectTestCase): self.assertRaises(psycopg2.OperationalError, self.conn.lobject, 0, "w", lo.oid) + self.assert_(not self.conn.closed) def test_import(self): self.tmpdir = tempfile.mkdtemp() From 489658cfc5753fadc83b75dee844cd45d37940f6 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 5 Apr 2014 15:32:16 +0100 Subject: [PATCH 6/7] Close the connection if discovered bad on poll() --- NEWS | 1 + psycopg/pqpath.c | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/NEWS b/NEWS index e0dd56e8..54a9e6e6 100644 --- a/NEWS +++ b/NEWS @@ -29,6 +29,7 @@ What's new in psycopg 2.5.3 the superclass init (:ticket:`#195`). - Mark the connection closed when a Socket connection is broken, as it happens for TCP connections instead (:ticket:`#196`). +- Mark the connection closed if found broken on `poll()`. - Fixed possible segfault in named cursors creation. - It is now possible to call `get_transaction_status()` on closed connections. - Fixed debug build on Windows, thanks to James Emerton. diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 16b733f4..a19c05a8 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -807,6 +807,12 @@ pq_is_busy(connectionObject *conn) Dprintf("pq_is_busy: PQconsumeInput() failed"); pthread_mutex_unlock(&(conn->lock)); Py_BLOCK_THREADS; + + /* if the libpq says pgconn is lost, close the py conn */ + if (CONNECTION_BAD == PQstatus(conn->pgconn)) { + conn->closed = 2; + } + PyErr_SetString(OperationalError, PQerrorMessage(conn->pgconn)); return -1; } @@ -836,6 +842,12 @@ pq_is_busy_locked(connectionObject *conn) if (PQconsumeInput(conn->pgconn) == 0) { Dprintf("pq_is_busy_locked: PQconsumeInput() failed"); + + /* if the libpq says pgconn is lost, close the py conn */ + if (CONNECTION_BAD == PQstatus(conn->pgconn)) { + conn->closed = 2; + } + PyErr_SetString(OperationalError, PQerrorMessage(conn->pgconn)); return -1; } From 09fb131f1a06b874e0fc737962589d49ebc5fe9f Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 5 Apr 2014 13:37:42 +0100 Subject: [PATCH 7/7] Don't specify 0 or 1 in closed docs There's also 2 which means broken. But I prefer to leave that as implementation detail. --- doc/src/connection.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/src/connection.rst b/doc/src/connection.rst index eac2f001..2d483920 100644 --- a/doc/src/connection.rst +++ b/doc/src/connection.rst @@ -295,8 +295,8 @@ The ``connection`` class .. attribute:: closed - Read-only attribute reporting whether the database connection is open - (0) or closed (1). + Read-only integer attribute: 0 if the connection is open, nonzero if + it is closed or broken. .. method:: cancel