From: Heikki Linnakangas <hlinnakangas@vmware.com>
To: Jade Koskela <jkoskela0@gmail.com>
To: pgsql-odbc@postgresql.org
Subject: Re: Problem in SQLFreeHandle (Statement)
Date: Thu, 19 Jun 2014 15:33:06 +0300
Message-ID: <53A2D882.1000106@vmware.com> (raw)
In-Reply-To: <CAN5ZvqxmLiUzZjbEdNjmhYWZ9sHkf7Y8RJi=DxRR8-3Jj56_Qg@mail.gmail.com>
References: <CAN5Zvqz3WRSWFzNAZ0J3ftEib+u85g1GBqb=GMve3tHFz0jkLA@mail.gmail.com>
<5399AB65.5040608@vmware.com>
<CAN5ZvqxmLiUzZjbEdNjmhYWZ9sHkf7Y8RJi=DxRR8-3Jj56_Qg@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-odbc>
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
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbcAttachments:download all (2 files, .tar.gz)0001-Fix-bug-in-SQLCancel.patch (text/x-diff, 1.7K) download
From 68c47befd4bc27b5691f457af48fa5028cf52286 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
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.cindex 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
0002-Refactor-SQLCancel-and-add-comments-for-readability.patch (text/x-diff, 5.6K) download
From d255ab39268de3ac1d4a374bfb24fe40aec299aa Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
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.cindex 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-odbc@postgresql.org
Cc: hlinnakangas@vmware.com, jkoskela0@gmail.com
Subject: Re: Problem in SQLFreeHandle (Statement)
In-Reply-To: <53A2D882.1000106@vmware.com>
References: <CAN5Zvqz3WRSWFzNAZ0J3ftEib+u85g1GBqb=GMve3tHFz0jkLA@mail.gmail.com> <5399AB65.5040608@vmware.com> <CAN5ZvqxmLiUzZjbEdNjmhYWZ9sHkf7Y8RJi=DxRR8-3Jj56_Qg@mail.gmail.com> <53A2D882.1000106@vmware.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox