From 07d9fb871858fafec89cb17a7dd3ff35e11289e6 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 2 Jan 2019 13:15:00 +0100 Subject: [PATCH] Don't call CLEARPGRES on the cursor state without holding the gil There is a chance it is executed by two different threads resulting in issue #384. I havent't found any other case that may lead to double free. --- psycopg/pqpath.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 04a34a22..2d4bac87 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -1016,6 +1016,8 @@ pq_execute(cursorObject *curs, const char *query, int async, int no_result, int } Dprintf("pq_execute: pg connection at %p OK", curs->conn->pgconn); + CLEARPGRES(curs->pgres); + Py_BEGIN_ALLOW_THREADS; pthread_mutex_lock(&(curs->conn->lock)); @@ -1027,7 +1029,6 @@ pq_execute(cursorObject *curs, const char *query, int async, int no_result, int } if (async == 0) { - CLEARPGRES(curs->pgres); Dprintf("pq_execute: executing SYNC query: pgconn = %p", curs->conn->pgconn); Dprintf(" %-.200s", query); if (!psyco_green()) { @@ -1069,7 +1070,6 @@ pq_execute(cursorObject *curs, const char *query, int async, int no_result, int Dprintf("pq_execute: executing ASYNC query: pgconn = %p", curs->conn->pgconn); Dprintf(" %-.200s", query); - CLEARPGRES(curs->pgres); if (PQsendQuery(curs->conn->pgconn, query) == 0) { if (CONNECTION_BAD == PQstatus(curs->conn->pgconn)) { curs->conn->closed = 2;