From: Heikki Linnakangas <hlinnakangas@vmware.com>
To: pgsql-odbc@postgresql.org <pgsql-odbc@postgresql.org>
Subject: SQLFreeStmt is missing locking
Date: Wed, 19 Feb 2014 20:25:30 +0200
Message-ID: <5304F71A.7040001@vmware.com> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-odbc>
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
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbcAttachments:add-freestmt-locking-1.patch (text/x-diff, 1.4K) downloaddiff --git a/odbcapi.c b/odbcapi.cindex 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.cindex 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);
SQLFreeStmt-race.c (text/x-csrc, 2.9K) download
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#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;
}
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
Subject: Re: SQLFreeStmt is missing locking
In-Reply-To: <5304F71A.7040001@vmware.com>
References: <5304F71A.7040001@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