Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WGBq0-0006pJ-Rq for pgsql-odbc@arkaria.postgresql.org; Wed, 19 Feb 2014 18:25:41 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WGBq0-00015u-6g for pgsql-odbc@arkaria.postgresql.org; Wed, 19 Feb 2014 18:25:40 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WGBpz-00013u-82 for pgsql-odbc@postgresql.org; Wed, 19 Feb 2014 18:25:39 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WGBpu-0006Di-H9 for pgsql-odbc@postgresql.org; Wed, 19 Feb 2014 18:25:37 +0000 Received: from sc9-mailhost3.vmware.com (sc9-mailhost3.vmware.com [10.113.161.73]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id 224602B546 for ; Wed, 19 Feb 2014 10:25:33 -0800 (PST) Received: from zcs-prod-ext-mta-1.vmware.com (zcs-prod-ext-mta-1.vmware.com [10.113.62.223]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id 1CAFC401DB for ; Wed, 19 Feb 2014 10:25:33 -0800 (PST) Received: from zcs-prod-ext-mta-1.vmware.com (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-1.vmware.com (Postfix) with ESMTP id 7AEEC120007 for ; Wed, 19 Feb 2014 10:25:32 -0800 (PST) Received: from [192.168.1.90] (zimbra-prod-ext-proxy-vip.vmware.com [10.113.63.87]) by zcs-prod-ext-mta-1.vmware.com (Postfix) with ESMTPSA for ; Wed, 19 Feb 2014 10:25:31 -0800 (PST) Message-ID: <5304F71A.7040001@vmware.com> Date: Wed, 19 Feb 2014 20:25:30 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131103 Icedove/17.0.10 MIME-Version: 1.0 To: "pgsql-odbc@postgresql.org" Subject: SQLFreeStmt is missing locking Content-Type: multipart/mixed; boundary="------------090108010003010408090800" X-Pg-Spam-Score: -7.5 (-------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-odbc Precedence: bulk Sender: pgsql-odbc-owner@postgresql.org This is a multi-part message in MIME format. --------------090108010003010408090800 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hi, Today I debugged an application that sometimes gave an error like this, after several hours of stress testing: ERROR: prepared statement "_PLAN000000000EA64DF0" already exists The application is multi-threaded with a built-in connection pool. It turned out to be a race condition between SQLFreeStmt and using the same connection for another query. SQLFreeStmt calls SC_Destructor, which calls SC_set_prepared(stmt, FALSE). That is supposed to free the prepared statement in the backend, by issuing a DEALLOCATE. Before that, however, it checks the status of the connection; there's no point sending a DEALLOCATE if the connection is dead already, or within an aborted transaction. However, it's possible that the connection is in CONN_EXECUTING state, if another thread is executing a different query using the same connection. SC_set_prepared treats that the same as a dead connection, and skips the DEALLOCATE. That leaks the prepared statement. Later, when a new statement object is allocated to the same address, on the same connection, and prepare a new statement using that handle, it will clash with the leaked prepared statement in the backend, and give the "prepared statement XXX already exists" error. The fix is pretty straightforward: SQLFreeStmt (and SQLFreeHandle, which is equivalent), needs to use a critical section like most of the API functions do. This particular problem only happens when dropping the statement altogether with SQL_DROP, but I think SQL_CLOSE, SQL_RESET_PARAMS and SQL_UNBIND also needs to be protected by a lock. Otherwise another thread might be concurrently modifying the statement's data structures. SQL_DROP will free the statement object, so that needs to be protected by the connection's critical section rather than the statement's. Also, the statement's critical section wouldn't be enough to stop other threads from using the connection at the same time, so we'd still have the same problem in SC_set_prepared. So long story short, I'm going to apply the attached patch, which adds locking to SQLFreeStmt and SQLFreeHandle. Attached is also a test program to demonstrate the issue, using pthreads. I'm not going to add this to the regression suite, though, because it would add a dependency to pthreads, and it seems unlikely for this exact same bug to reappear anyway. Anyone see a problem with that? PS. There are a few other API functions that are not protected by a critical section: SQLCopyDesc, SQLGetDescField, SQLGetDiagField, SQLGetDiagRec, SQLSetDescField. I'm not motivated enough to go fix them, but I think at least some of them also have race conditions. - Heikki --------------090108010003010408090800 Content-Type: text/x-diff; name="add-freestmt-locking-1.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="add-freestmt-locking-1.patch" diff --git a/odbcapi.c b/odbcapi.c index fc124d5..efcd480 100644 --- a/odbcapi.c +++ b/odbcapi.c @@ -455,9 +455,36 @@ SQLFreeStmt(HSTMT StatementHandle, SQLUSMALLINT Option) { RETCODE ret; + StatementClass *stmt = (StatementClass *) StatementHandle; + ConnectionClass *conn = NULL; mylog("[SQLFreeStmt]"); + + if (stmt) + { + if (Option == SQL_DROP) + { + conn = stmt->hdbc; + if (conn) + ENTER_CONN_CS(conn); + } + else + ENTER_STMT_CS(stmt); + } + ret = PGAPI_FreeStmt(StatementHandle, Option); + + if (stmt) + { + if (Option == SQL_DROP) + { + if (conn) + LEAVE_CONN_CS(conn); + } + else + LEAVE_STMT_CS(stmt); + } + return ret; } diff --git a/odbcapi30.c b/odbcapi30.c index 16288bb..4a99a4e 100644 --- a/odbcapi30.c +++ b/odbcapi30.c @@ -239,7 +239,11 @@ SQLFreeHandle(SQLSMALLINT HandleType, SQLHANDLE Handle) { CSTR func = "SQLFreeHandle"; RETCODE ret; + StatementClass *stmt; + ConnectionClass *conn = NULL; + mylog("[[%s]]", func); + switch (HandleType) { case SQL_HANDLE_ENV: @@ -249,7 +253,20 @@ SQLFreeHandle(SQLSMALLINT HandleType, SQLHANDLE Handle) ret = PGAPI_FreeConnect(Handle); break; case SQL_HANDLE_STMT: + stmt = (StatementClass *) Handle; + + if (stmt) + { + conn = stmt->hdbc; + if (conn) + ENTER_CONN_CS(conn); + } + ret = PGAPI_FreeStmt(Handle, SQL_DROP); + + if (conn) + LEAVE_CONN_CS(conn); + break; case SQL_HANDLE_DESC: ret = PGAPI_FreeDesc(Handle); --------------090108010003010408090800 Content-Type: text/x-csrc; name="SQLFreeStmt-race.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="SQLFreeStmt-race.c" #include #include #include #include "common.h" struct stmt_freeing_thread_data { SQLHDBC conn; HSTMT hstmt; }; void *stmt_freeing_thread_main(void *hstmt) { struct stmt_freeing_thread_data *data = (struct stmt_freeing_thread_data *) hstmt; SQLRETURN rc; sleep(2); rc = SQLFreeStmt(data->hstmt, SQL_DROP); if (!SQL_SUCCEEDED(rc)) { print_diag("SQLFreeStmt failed", SQL_HANDLE_DBC, data->conn); exit(1); } } int main(int argc, char **argv) { SQLRETURN rc; HSTMT hstmt = SQL_NULL_HSTMT; HSTMT hstmt2 = SQL_NULL_HSTMT; char *param1; SQLLEN cbParam1; int i; char errstr[100]; pthread_t stmt_freeing_thread; struct stmt_freeing_thread_data thread_data; test_connect(); rc=SQLSetConnectAttr( conn,SQL_ATTR_AUTOCOMMIT, (void*) SQL_AUTOCOMMIT_OFF, SQL_NTS); if (!SQL_SUCCEEDED(rc)) { print_diag("SQLSetConnectAttr failed", SQL_HANDLE_STMT, hstmt); exit(1); } rc = SQLAllocStmt(conn, &hstmt2); if (!SQL_SUCCEEDED(rc)) { print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn); exit(1); } rc = SQLAllocStmt(conn, &hstmt); if (!SQL_SUCCEEDED(rc)) { print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn); exit(1); } /* Prepare a statement */ rc = SQLPrepare(hstmt, (SQLCHAR *) "SELECT 'foo'", SQL_NTS); CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt); /* Execute */ rc = SQLExecute(hstmt); if (rc != SQL_NO_DATA) CHECK_STMT_RESULT(rc, "SQLExecute failed", hstmt); rc = SQLEndTran( SQL_HANDLE_DBC, conn, SQL_COMMIT); if (!SQL_SUCCEEDED(rc)) { print_diag("SQLEndTran failed", SQL_HANDLE_DBC, conn); exit(1); } /* * Launch a new thread that will free the statement after two seconds, * while we're busy executing another statement */ thread_data.conn = conn; thread_data.hstmt = hstmt; if (pthread_create(&stmt_freeing_thread, NULL, stmt_freeing_thread_main, &thread_data)) { printf("error creating thread\n"); exit(1); } /* * Before the other thread free's the first statement, begin a new query * using a the second statement on the same connection. */ rc = SQLExecDirect(hstmt2, (SQLCHAR *) "SELECT pg_sleep(5)", SQL_NTS); CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt); rc = SQLFreeStmt(hstmt2, SQL_CLOSE); CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt2); /* the thread freeing the first statmenet should be finished by now */ if (pthread_join(stmt_freeing_thread, NULL)) { printf("error joining thread\n"); exit(1); } /* Verify that the prepared statement was deallocated */ printf("Querying pg_prepared_statement to check if all prepared statements\n"); printf("were DEALLOCATEd correctly. The result set should be empty.\n"); rc = SQLExecDirect(hstmt2, (SQLCHAR *) "SELECT * FROM pg_prepared_statements", SQL_NTS); CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt2); print_result(hstmt2); printf("\n"); /* Clean up */ test_disconnect(); return 0; } --------------090108010003010408090800 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc --------------090108010003010408090800--