From f597c36f426865fc5f265c827c9fdd32d178e1a8 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 aa595e21..ab25bc0a 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,7 @@ What's new in psycopg 2.5.3 Adam Petrovich for the bug report and diagnosis. - Don't segfault using poorly defined cursor subclasses which forgot to call the superclass init (:ticket:`#195`). +- 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 b3d4aa63..96919a72 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 d1e1243ba851641feb2920148a44de3b92542997 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 ab25bc0a..4e63e0fd 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,8 @@ What's new in psycopg 2.5.3 Adam Petrovich for the bug report and diagnosis. - 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`). - 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 f6d8f8bf..1a701524 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 221d0d66de5439f76dac1e7c70a787ac732927ef 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 f0c38f0b37bbd202375f33d4a215d332ff4dccd3 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 823a1b76..c0a4724d 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 e5ab0b39875b0cfd6e1cea77b3122e3f18ff520e 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 1a701524..d33915c8 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 e62e0d85..a7781e68 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 12c2fafa865a039d7944844eff72c4255d44045f 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 4e63e0fd..99419bf6 100644 --- a/NEWS +++ b/NEWS @@ -12,6 +12,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()`. - 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 d33915c8..5cdb38dc 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 75b98b561b3cc3be6cb1482b18e5dd2f44dabed4 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 22f1f066..121705f0 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