From de6f2ac387231fcd57c05031c82471ea57255e2b Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 30 Jun 2011 14:43:21 +0100 Subject: [PATCH] Grab the GIL when checking for errors occurred The problem was causing a segfault on BEGIN if the server is disconnected after the connection is created. --- NEWS | 2 ++ psycopg/pqpath.c | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/NEWS b/NEWS index 39af3a61..1c70c6da 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,8 @@ What's new in psycopg 2.4.3 --------------------------- + - Fixed segfault in case of transaction started with connection lost + (and possibly other events). - Lazy import of the slow uuid module, thanks to Marko Kreen. diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index cee5ce4c..1f0d5da9 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -344,11 +344,13 @@ pq_execute_command_locked(connectionObject *conn, const char *query, } if (*pgres == NULL) { Dprintf("pq_execute_command_locked: PQexec returned NULL"); + PyEval_RestoreThread(*tstate); if (!PyErr_Occurred()) { const char *msg; msg = PQerrorMessage(conn->pgconn); if (msg && *msg) { *error = strdup(msg); } } + *tstate = PyEval_SaveThread(); goto cleanup; } @@ -635,11 +637,13 @@ pq_get_guc_locked( if (*pgres == NULL) { Dprintf("pq_get_guc_locked: PQexec returned NULL"); + PyEval_RestoreThread(*tstate); if (!PyErr_Occurred()) { const char *msg; msg = PQerrorMessage(conn->pgconn); if (msg && *msg) { *error = strdup(msg); } } + *tstate = PyEval_SaveThread(); goto cleanup; } if (PQresultStatus(*pgres) != PGRES_TUPLES_OK) {