From 71eeb9086a36ca98814c4292c10dd89244677e90 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 5 Apr 2014 15:32:16 +0100 Subject: [PATCH] Close the connection if discovered bad on poll() Conflicts: NEWS --- NEWS | 1 + psycopg/pqpath.c | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/NEWS b/NEWS index 52cbe225..4e0c029d 100644 --- a/NEWS +++ b/NEWS @@ -26,6 +26,7 @@ What's new in psycopg 2.4.7 happens for TCP connections instead (:ticket:`#196`). - Fixed overflow opening a lobject with an oid not fitting in a signed int (:ticket:`#203`). + - Mark the connection closed if found broken on `poll()`. - Fixed possible segfault in named cursors creation. - Fixed debug build on Windows, thanks to James Emerton. diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 65b226e1..17f7a972 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -774,6 +774,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; } @@ -803,6 +809,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; }