From 696d12355008a2bb733aeafa55614d6ca28b769f Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 6 Mar 2014 18:11:17 +0000 Subject: [PATCH] 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) {