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.