Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WxbWu-0005kK-6V for pgsql-odbc@arkaria.postgresql.org; Thu, 19 Jun 2014 12:33:24 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WxbWs-0005Qv-Jf for pgsql-odbc@arkaria.postgresql.org; Thu, 19 Jun 2014 12:33:22 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1WxbWr-0005Qo-AD for pgsql-odbc@postgresql.org; Thu, 19 Jun 2014 12:33:21 +0000 Received: from smtp-outbound-1.vmware.com ([208.91.2.12]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1WxbWh-0004aM-7F for pgsql-odbc@postgresql.org; Thu, 19 Jun 2014 12:33:18 +0000 Received: from sc9-mailhost2.vmware.com (sc9-mailhost2.vmware.com [10.113.161.72]) by smtp-outbound-1.vmware.com (Postfix) with ESMTP id 0213E9A762; Thu, 19 Jun 2014 05:33:09 -0700 (PDT) Received: from zcs-prod-ext-mta-1.vmware.com (zcs-prod-ext-mta-1.vmware.com [10.113.62.223]) by sc9-mailhost2.vmware.com (Postfix) with ESMTP id E6B45B0684; Thu, 19 Jun 2014 05:33:08 -0700 (PDT) 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 6826E12002A; Thu, 19 Jun 2014 05:33:23 -0700 (PDT) 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; Thu, 19 Jun 2014 05:33:22 -0700 (PDT) Message-ID: <53A2D882.1000106@vmware.com> Date: Thu, 19 Jun 2014 15:33:06 +0300 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Icedove/24.5.0 MIME-Version: 1.0 To: Jade Koskela , pgsql-odbc@postgresql.org Subject: Re: Problem in SQLFreeHandle (Statement) References: <5399AB65.5040608@vmware.com> In-Reply-To: Content-Type: multipart/mixed; boundary="------------050001050500030800020007" X-Pg-Spam-Score: -6.9 (------) 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. --------------050001050500030800020007 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 06/17/2014 11:06 PM, Jade Koskela wrote: > I couldn't reproduce the behavior using the test program, or a similar one. > I recompiled the driver to enable extra logging. > Here is the grep on ENTER_CONN_CS, the second address is the address of the > mutex (pthread recursive). > This was compiled on OSX with Clang, and then also with GCC with the same > result. Behavior doesn't reproduce on Windows. > > > [140735257568016][[SQLFreeHandle]] > > [140735257568016]*** ENTER_CONN_CS [140210608634616] *** > > [140735257568016]*** ENTER_CONN_CS [140210608634616] *** > > [140735257568016]*** LEAVE_CONN_CS [140210608634616] *** > > [140735257568016]*** LEAVE_CONN_CS [140210608634616] *** > > [140735257568016]*** LEAVE_CONN_CS [140210608634616] *** > > [140735257568016][SQLGetConnectAttrW] > > [140735257568016]*** ENTER_CONN_CS [140210608634616] *** > > [140735257568016]*** LEAVE_CONN_CS [140210608634616] *** > > [140735257568016][SQLGetConnectAttrW] > > [140735257568016]*** ENTER_CONN_CS [140210608634616] *** > > [140735257568016]*** LEAVE_CONN_CS [140210608634616] *** > > [140735257568016][SQLGetConnectAttrW] > > [140735257568016]*** ENTER_CONN_CS [140210608634616] *** > > [140735257568016]*** LEAVE_CONN_CS [140210608634616] *** > > [4466282496][[SQLAllocHandle]] > > [4466282496]*** ENTER_CONN_CS [140210608634616] *** <- new thread > grabbed mutex, count=1 > > [4466282496]**** PGAPI_AllocStmt: hdbc = 0x7f8553857000, stmt = > 0x7f8552d17100 > > [4466282496]*** LEAVE_CONN_CS [140210608634616] *** <- count=0 > > [4466282496]**** PGAPI_Prepare: STMT_ALLOCATED, copy > > [4466282496]*** ENTER_CONN_CS [140210608634616] *** <- count=1 > > [4466282496]*** ENTER_CONN_CS [140210608634616] *** <- count=2 > > [140735257568016]*** LEAVE_CONN_CS [140210608634616] *** <- this thread > doesn't own, count=2 A-ha. The problem is that after issuing the query cancel, the SQLCancel() function calls DiscardStatementSvp(), to discard any savepoints associated with the statement. SetStatementSvp() and DiscardStatementSvp() maintain a counter of how many times the CONN_CS has been acquired, and DiscardStatementSvp() tries to completely release it by releasing it as many times as the counter says it's been locked. However, unlocking a pthread mutex from a different thread than the one that locked it is undefined behavior. Apparently it happens to work on other platforms by accident, but OS X is somehow more picky about it. We never actually check the return value of pthread_mutex_lock() / pthread_mutex_unlock(), so it's possible that they are reporting errors even on other platforms, and we just don't pay attention to it. That ought to be fixed too, at least in debug builds. Also for debugging purposes, I think we should add a thread ID field next to the lock_CC_for_rb field to remember which thread is holding the locks, as a sanity check. Attached are two patches. The first one is a narrow fix, which should fix the bug. It simply removes the DiscardStatementSvp() call. AFAICS it's never needed: if the statement was busy executing in a different thread (this is the case you hit), we send a query cancel request to the server, and it's not cool to do anything else with the statement. And in the codepaths where the statement was not busy executing, PGAPI_Cancel() already called DiscardStatementSvp(), with the appropriate lock held. The second patch refactors PGAPI_Cancel() and adds comments, to make it more readable. It's not absolutely required, but I found PGAPI_Cancel() very difficult to debug as it is. Can you test these patches, to confirm that it fixes the bug you're seeing? - Heikki --------------050001050500030800020007 Content-Type: text/x-diff; name="0001-Fix-bug-in-SQLCancel.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-Fix-bug-in-SQLCancel.patch" From 68c47befd4bc27b5691f457af48fa5028cf52286 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Thu, 19 Jun 2014 15:24:50 +0300 Subject: [PATCH 1/2] Fix bug in SQLCancel(). If one thread was busy executing a savepoint statement, while another thread calls SQLCancel() on the statement, SQLCancel() would try to release the locks held by the other thread. That's extremely racy; if the other thread tries to increment/decrement the lock_CC_for_rb counter at the same time, we could end up releasing the lock to many times or too few times. Also, releasing a pthread mutex from a different thread than the one who acquired it is undefined behavior. AFAICS the DiscardStatementSvp() call in SQLCancel() can be safely removed. It's outright wrong and dangerous if the statement is busy executing in a different thread, and the other codepaths in PGAPI_Cancel() already call DiscardStatementSvp(). Bug report by Jade Koskela. --- odbcapi.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/odbcapi.c b/odbcapi.c index b5b5aac..9210cc5 100644 --- a/odbcapi.c +++ b/odbcapi.c @@ -103,14 +103,10 @@ SQLBindCol(HSTMT StatementHandle, RETCODE SQL_API SQLCancel(HSTMT StatementHandle) { - RETCODE ret; - StatementClass *stmt = (StatementClass *) StatementHandle; - mylog("[SQLCancel]"); /* Not that neither ENTER_STMT_CS nor StartRollbackState is called */ /* SC_clear_error((StatementClass *) StatementHandle); maybe this neither */ - ret = PGAPI_Cancel(StatementHandle); - return DiscardStatementSvp(stmt, ret, FALSE); + return PGAPI_Cancel(StatementHandle); } static BOOL theResultIsEmpty(const StatementClass *stmt) -- 2.0.0 --------------050001050500030800020007 Content-Type: text/x-diff; name="0002-Refactor-SQLCancel-and-add-comments-for-readability.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0002-Refactor-SQLCancel-and-add-comments-for-readability.pat"; filename*1="ch" From d255ab39268de3ac1d4a374bfb24fe40aec299aa Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Thu, 19 Jun 2014 15:29:41 +0300 Subject: [PATCH 2/2] Refactor SQLCancel() and add comments, for readability. --- execute.c | 153 ++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 84 insertions(+), 69 deletions(-) diff --git a/execute.c b/execute.c index 7931fe8..41e7c59 100644 --- a/execute.c +++ b/execute.c @@ -1234,7 +1234,6 @@ PGAPI_Cancel(HSTMT hstmt) /* Statement to cancel. */ StatementClass *stmt = (StatementClass *) hstmt, *estmt; ConnectionClass *conn; RETCODE ret = SQL_SUCCESS; - BOOL entered_cs = FALSE; mylog("%s: entering...\n", func); @@ -1246,92 +1245,108 @@ PGAPI_Cancel(HSTMT hstmt) /* Statement to cancel. */ } conn = SC_get_conn(stmt); -#define return DONT_CALL_RETURN_FROM_HERE??? - /* StartRollbackState(stmt); */ - if (stmt->execute_delegate) estmt = stmt->execute_delegate; else estmt = stmt; + /* - * Not in the middle of SQLParamData/SQLPutData so cancel like a - * close. + * SQLCancel works differently depending on what the statement is + * currently doing: + * + * 1. In the middle of SQLParamData / SQLPutData + * -> cancel the statement + * + * 2. Running a query asynchronously. (asynchronous mode is not supported + * by psqlODBC) + * + * 3. Busy running a function in another thread. + * -> Send a query cancel request to the server + * + * 4. Not doing anything. + * -> in ODBC version 2.0, same as SQLFreeStmt(SQL_CLOSE). In version + * 3.5, it has no effect. + * + * XXX: Checking for these conditions is racy. For example, we might + * see that the statement is waiting for SQLParamdata/SQLPutData, but + * before we acquire the lock on the statement, another thread has + * supplied the data and started executing. In that case, we'll block + * on the lock until the execution finishes. */ - if (estmt->data_at_exec < 0) + if (estmt->data_at_exec >= 0) { + /* Waiting for more data from SQLParamData/SQLPutData. Cancel that. */ /* - * Tell the Backend that we're cancelling this request + * Note, any previous data-at-exec buffers will be freed in the + * recycle */ - if (estmt->status == STMT_EXECUTING) - { - if (!CC_send_cancel_request(conn)) - { - ret = SQL_ERROR; - } - goto cleanup; - } + /* if they call SQLExecDirect or SQLExecute again. */ + + ENTER_STMT_CS(stmt); + SC_clear_error(stmt); + estmt->data_at_exec = -1; + estmt->current_exec_param = -1; + estmt->put_data = FALSE; + cancelNeedDataState(estmt); + if (stmt->internal) + ret = DiscardStatementSvp(stmt, ret, FALSE); + LEAVE_STMT_CS(stmt); + return ret; + } + else if (estmt->status == STMT_EXECUTING) + { /* - * MAJOR HACK for Windows to reset the driver manager's cursor - * state: Because of what seems like a bug in the Odbc driver - * manager, SQLCancel does not act like a SQLFreeStmt(CLOSE), as - * many applications depend on this behavior. So, this brute - * force method calls the driver manager's function on behalf of - * the application. + * Busy executing in a different thread. Send a cancel request to + * the server. */ - - if (conn->driver_version < 0x0350) + if (!CC_send_cancel_request(conn)) + return SQL_ERROR; + else + return SQL_SUCCESS; + } + else + { + /* + * The statement is not executing, and it's not waiting for params + * either. Looks like it's not doing anything. + */ + if (conn->driver_version >= 0x0350) + return SQL_SUCCESS; + else { + /* + * MAJOR HACK for Windows to reset the driver manager's cursor + * state: Because of what seems like a bug in the Odbc driver + * manager, SQLCancel does not act like a SQLFreeStmt(CLOSE), as + * many applications depend on this behavior. So, this brute + * force method calls the driver manager's function on behalf of + * the application. + */ #ifdef WIN32 - ConnInfo *ci = &(conn->connInfo); - - if (ci->drivers.cancel_as_freestmt) - { + if (conn->connInfo->drivers.cancel_as_freestmt) + { typedef SQLRETURN (SQL_API *SQLAPIPROC)(); - HMODULE hmodule; - SQLAPIPROC addr; + HMODULE hmodule; + SQLAPIPROC addr; - hmodule = GetModuleHandle("ODBC32"); - addr = (SQLAPIPROC) GetProcAddress(hmodule, "SQLFreeStmt"); - ret = addr((char *) (stmt->phstmt) - 96, SQL_CLOSE); - } - else + hmodule = GetModuleHandle("ODBC32"); + addr = (SQLAPIPROC) GetProcAddress(hmodule, "SQLFreeStmt"); + ret = addr((char *) (stmt->phstmt) - 96, SQL_CLOSE); + } + else #endif /* WIN32 */ - { - ENTER_STMT_CS(stmt); - entered_cs = TRUE; - SC_clear_error(hstmt); - ret = PGAPI_FreeStmt(hstmt, SQL_CLOSE); - } - - mylog("PGAPI_Cancel: PGAPI_FreeStmt returned %d\n", ret); + { + ENTER_STMT_CS(stmt); + SC_clear_error(hstmt); + ret = PGAPI_FreeStmt(hstmt, SQL_CLOSE); + if (stmt->internal) + ret = DiscardStatementSvp(stmt, ret, FALSE); + LEAVE_STMT_CS(stmt); + } + mylog("PGAPI_Cancel: PGAPI_FreeStmt returned %d\n", ret); + return ret; } - goto cleanup; } - - /* In the middle of SQLParamData/SQLPutData, so cancel that. */ - /* - * Note, any previous data-at-exec buffers will be freed in the - * recycle - */ - /* if they call SQLExecDirect or SQLExecute again. */ - - ENTER_STMT_CS(stmt); - entered_cs = TRUE; - SC_clear_error(stmt); - estmt->data_at_exec = -1; - estmt->current_exec_param = -1; - estmt->put_data = FALSE; - cancelNeedDataState(estmt); - -cleanup: -#undef return - if (entered_cs) - { - if (stmt->internal) - ret = DiscardStatementSvp(stmt, ret, FALSE); - LEAVE_STMT_CS(stmt); - } - return ret; } -- 2.0.0 --------------050001050500030800020007 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 --------------050001050500030800020007--