From 7632e1ae46d0f65b6302f02d0b6a5c236b84c0fe Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 6 Oct 2012 01:45:24 +0100 Subject: [PATCH] Get the result from the connection after the green panic Otherwise the connection won't be usable in case we manage to put it back on track (libpq reports "another command is already in progress") --- psycopg/green.c | 7 +++++++ sandbox/test_green_error.py | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/psycopg/green.c b/psycopg/green.c index 8e5ec1be..2f35d9f6 100644 --- a/psycopg/green.c +++ b/psycopg/green.c @@ -202,6 +202,7 @@ end: static void psyco_panic_cancel(connectionObject *conn) { + PGresult *res; PyObject *etype, *evalue, *etb; char errbuf[256]; @@ -229,6 +230,12 @@ psyco_panic_cancel(connectionObject *conn) goto exit; } + /* we must clear the result or we get "another command is already in + * progress" */ + if (NULL != (res = pq_get_last_result(conn))) { + PQclear(res); + } + exit: /* restore the exception. If no exception was set at function begin, don't * clobber one that may have been set here. */ diff --git a/sandbox/test_green_error.py b/sandbox/test_green_error.py index 23a247b5..7d17cf74 100644 --- a/sandbox/test_green_error.py +++ b/sandbox/test_green_error.py @@ -42,7 +42,15 @@ signal.signal(signal.SIGHUP, handler) conn = psycopg2.connect(DSN) curs = conn.cursor() print "PID", os.getpid() -curs.execute("select pg_sleep(1000)") +try: + curs.execute("select pg_sleep(1000)") +except BaseException, e: + print "got exception:", e.__class__.__name__, e + +conn.rollback() +curs.execute("select 1") +print curs.fetchone() + # You can unplug the network cable etc. here. # Kill -HUP will raise an exception in the callback.