pg.ddx.io pgsql-odbc@postgresql.org mailing list archivehelp / color / mirror / Atom feed
Problem in SQLFreeHandle (Statement) 7+ messages / 3 participants [nested] [flat]
* Problem in SQLFreeHandle (Statement) @ 2014-06-12 02:30 Jade Koskela <jkoskela0@gmail.com> 0 siblings, 1 reply; 7+ messages in thread From: Jade Koskela @ 2014-06-12 02:30 UTC (permalink / raw) To: pgsql-odbc Hello, I am using psql-odbc 9.3 on OSX. I have one thread running a query, and another thread cancels the query using SQLCancel. The thread running the query successfully cancels and exits the driver code. When we try to free the statement handle, it blocks on a mutex doing ENTER_CONN_CS, or something similar in SQLFreeHandle. Does anyone have any input on this problem? I don't see who is holding the mutex, the thread running the query is done. Thanks! ^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Problem in SQLFreeHandle (Statement) @ 2014-06-12 13:30 Heikki Linnakangas <hlinnakangas@vmware.com> parent: Jade Koskela <jkoskela0@gmail.com> 0 siblings, 2 replies; 7+ messages in thread From: Heikki Linnakangas @ 2014-06-12 13:30 UTC (permalink / raw) To: Jade Koskela <jkoskela0@gmail.com>; pgsql-odbc On 06/12/2014 05:30 AM, Jade Koskela wrote: > Hello, > I am using psql-odbc 9.3 on OSX. Which version, exactly? There were some changes to locking in SQLFreeHandle in 09.03.0200. Not sure if those changes might've fixed this, or caused it. Can easily you test on another platform, or with different version of unixODBC? > I have one thread running a query, and > another thread cancels the query using SQLCancel. The thread running the > query successfully cancels and exits the driver code. When we try to free > the statement handle, it blocks on a mutex doing ENTER_CONN_CS, or > something similar in SQLFreeHandle. > > Does anyone have any input on this problem? I don't see who is holding the > mutex, the thread running the query is done. I tried to reproduce this with the attached little test program. It works fine. Can you post the test program you're using, or modify the attached so that it exhibits the behavior you're seeing, please? - Heikki -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc Attachments:cancel-race.c (text/x-csrc, 1.7K) download
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include "common.h" struct thread_data { SQLHDBC conn; }; SQLHSTMT hstmt; /* this thread runs a long query */ void *thread1_main(void *d) { SQLRETURN rc; printf("1: calling SQLExecute\n"); rc = SQLExecute(hstmt); CHECK_STMT_RESULT(rc, "1: SQLExecute failed", hstmt); print_result(hstmt); rc = SQLFreeStmt(hstmt, SQL_CLOSE); CHECK_STMT_RESULT(rc, "1:SQLFreeStmt failed", hstmt); } /* this thread cancels the first thread's query */ void *thread2_main(void *d) { SQLRETURN rc; sleep(2); printf("2: canceling first query\n"); rc = SQLCancel(hstmt); if (rc == SQL_SUCCESS) printf("2: canceled\n"); else if (rc == SQL_ERROR) printf("2: SQLCancel failed\n"); else printf("2: unexpected return code from SQLCancel: %d\n", rc); } int main(int argc, char **argv) { SQLRETURN rc; char *param1; SQLLEN cbParam1; int i; #define NTHREADS 2 char errstr[100]; pthread_t thread1; pthread_t thread2; void *retval; test_connect(); rc = SQLAllocStmt(conn, &hstmt); if (!SQL_SUCCEEDED(rc)) { print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn); exit(1); } rc = SQLPrepare(hstmt, (SQLCHAR *) "SELECT pg_sleep(1000)", SQL_NTS); CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt); if (pthread_create(&thread1, NULL, thread1_main, NULL)) { printf("error creating thread\n"); exit(1); } if (pthread_create(&thread2, NULL, thread2_main, NULL)) { printf("error creating thread\n"); exit(1); } if (pthread_join(thread1, &retval) != 0) { printf("error joining thread\n"); exit(1); } if (pthread_join(thread2, &retval) != 0) { printf("error joining thread\n"); exit(1); } return 0; }common.c (text/x-csrc, 5.5K) download
#include "common.h" SQLHENV env; SQLHDBC conn; void print_diag(char *msg, SQLSMALLINT htype, SQLHANDLE handle) { char sqlstate[32]; char message[1000]; SQLINTEGER nativeerror; SQLSMALLINT textlen; SQLRETURN ret; if (msg) printf("%s\n", msg); ret = SQLGetDiagRec(htype, handle, 1, sqlstate, &nativeerror, message, 256, &textlen); if (ret == SQL_INVALID_HANDLE) printf("Invalid handle\n"); else if (ret != SQL_ERROR) printf("%s=%s\n", (CHAR *)sqlstate, (CHAR *)message); } void test_connect_ext(char *extraparams) { SQLRETURN ret; SQLCHAR str[1024]; SQLSMALLINT strl; SQLCHAR dsn[1024]; snprintf(dsn, sizeof(dsn), "DSN=psqlodbc_test_dsn;%s", extraparams ? extraparams : ""); SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env); SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0); SQLAllocHandle(SQL_HANDLE_DBC, env, &conn); ret = SQLDriverConnect(conn, NULL, dsn, SQL_NTS, str, sizeof(str), &strl, SQL_DRIVER_COMPLETE); if (SQL_SUCCEEDED(ret)) { printf("connected\n"); } else { print_diag("SQLDriverConnect failed.", SQL_HANDLE_DBC, conn); exit(1); } } void test_connect(void) { test_connect_ext(NULL); } void test_disconnect(void) { SQLRETURN rc; printf("disconnecting\n"); rc = SQLDisconnect(conn); if (!SQL_SUCCEEDED(rc)) { print_diag("SQLDisconnect failed", SQL_HANDLE_DBC, conn); exit(1); } rc = SQLFreeHandle(SQL_HANDLE_DBC, conn); if (!SQL_SUCCEEDED(rc)) { print_diag("SQLFreeHandle failed", SQL_HANDLE_DBC, conn); exit(1); } conn = NULL; rc = SQLFreeHandle(SQL_HANDLE_ENV, env); if (!SQL_SUCCEEDED(rc)) { print_diag("SQLFreeHandle failed", SQL_HANDLE_ENV, env); exit(1); } env = NULL; } const char * datatype_str(SQLSMALLINT datatype) { static char buf[100]; switch (datatype) { case SQL_CHAR: return "CHAR"; case SQL_VARCHAR: return "VARCHAR"; case SQL_LONGVARCHAR: return "LONGVARCHAR"; case SQL_WCHAR: return "WCHAR"; case SQL_WVARCHAR: return "WVARCHAR"; case SQL_WLONGVARCHAR: return "WLONGVARCHAR"; case SQL_DECIMAL: return "DECIMAL"; case SQL_NUMERIC: return "NUMERIC"; case SQL_SMALLINT: return "SMALLINT"; case SQL_INTEGER: return "INTEGER"; case SQL_REAL: return "REAL"; case SQL_FLOAT: return "FLOAT"; case SQL_DOUBLE: return "DOUBLE"; case SQL_BIT: return "BIT"; case SQL_TINYINT: return "TINYINT"; case SQL_BIGINT: return "BIGINT"; case SQL_BINARY: return "BINARY"; case SQL_VARBINARY: return "VARBINARY"; case SQL_LONGVARBINARY: return "LONGVARBINARY"; case SQL_TYPE_DATE: return "TYPE_DATE"; case SQL_TYPE_TIME: return "TYPE_TIME"; case SQL_TYPE_TIMESTAMP: return "TYPE_TIMESTAMP"; case SQL_GUID: return "GUID"; default: snprintf(buf, sizeof(buf), "unknown sql type %d", datatype); return buf; } } const char *nullable_str(SQLSMALLINT nullable) { static char buf[100]; switch(nullable) { case SQL_NO_NULLS: return "not nullable"; case SQL_NULLABLE: return "nullable"; case SQL_NULLABLE_UNKNOWN: return "nullable_unknown"; default: snprintf(buf, sizeof(buf), "unknown nullable value %d", nullable); return buf; } } void print_result_meta_series(HSTMT hstmt, SQLSMALLINT *colids, SQLSMALLINT numcols) { int i; printf("Result set metadata:\n"); for (i = 0; i < numcols; i++) { SQLRETURN rc; SQLCHAR colname[50]; SQLSMALLINT colnamelen; SQLSMALLINT datatype; SQLULEN colsize; SQLSMALLINT decdigits; SQLSMALLINT nullable; rc = SQLDescribeCol(hstmt, colids[i], colname, sizeof(colname), &colnamelen, &datatype, &colsize, &decdigits, &nullable); if (!SQL_SUCCEEDED(rc)) { print_diag("SQLDescribeCol failed", SQL_HANDLE_STMT, hstmt); return; } printf("%s: %s(%u) digits: %d, %s\n", colname, datatype_str(datatype), (unsigned int) colsize, decdigits, nullable_str(nullable)); } } void print_result_meta(HSTMT hstmt) { SQLRETURN rc; SQLSMALLINT numcols, i; SQLSMALLINT *colids; rc = SQLNumResultCols(hstmt, &numcols); if (!SQL_SUCCEEDED(rc)) { print_diag("SQLNumResultCols failed", SQL_HANDLE_STMT, hstmt); return; } colids = (SQLSMALLINT *) malloc(numcols * sizeof(SQLSMALLINT)); for (i = 0; i < numcols; i++) colids[i] = i + 1; print_result_meta_series(hstmt, colids, numcols); free(colids); } /* * Print result only for the selected columns. */ void print_result_series(HSTMT hstmt, SQLSMALLINT *colids, SQLSMALLINT numcols) { SQLRETURN rc; printf("Result set:\n"); while(1) { rc = SQLFetch(hstmt); if (rc == SQL_NO_DATA) break; if (rc == SQL_SUCCESS) { char buf[40]; int i; SQLLEN ind; for (i = 0; i < numcols; i++) { rc = SQLGetData(hstmt, colids[i], SQL_C_CHAR, buf, sizeof(buf), &ind); if (!SQL_SUCCEEDED(rc)) { print_diag("SQLGetData failed", SQL_HANDLE_STMT, hstmt); return; } if (ind == SQL_NULL_DATA) strcpy(buf, "NULL"); printf("%s%s", (i > 0) ? "\t" : "", buf); } printf("\n"); } else { print_diag("SQLFetch failed", SQL_HANDLE_STMT, hstmt); exit(1); } } } /* * Print result on all the columns */ void print_result(HSTMT hstmt) { SQLRETURN rc; SQLSMALLINT numcols, i; SQLSMALLINT *colids; rc = SQLNumResultCols(hstmt, &numcols); if (!SQL_SUCCEEDED(rc)) { print_diag("SQLNumResultCols failed", SQL_HANDLE_STMT, hstmt); return; } colids = (SQLSMALLINT *) malloc(numcols * sizeof(SQLSMALLINT)); for (i = 0; i < numcols; i++) colids[i] = i + 1; print_result_series(hstmt, colids, numcols); free(colids); }^ permalink raw reply [nested|flat] 7+ messages in threadcommon.h (text/x-chdr, 976B) download
#include <stdio.h> #include <string.h> #include <stdlib.h> #ifdef WIN32 #include <windows.h> #endif #include <sql.h> #include <sqlext.h> #ifdef WIN32 #define snprintf _snprintf #endif extern SQLHENV env; extern SQLHDBC conn; #define CHECK_STMT_RESULT(rc, msg, hstmt) \ if (!SQL_SUCCEEDED(rc)) \ { \ print_diag(msg, SQL_HANDLE_STMT, hstmt); \ exit(1); \ } extern void print_diag(char *msg, SQLSMALLINT htype, SQLHANDLE handle); extern void test_connect_ext(char *extraparams); extern void test_connect(void); extern void test_disconnect(void); extern void print_result_meta_series(HSTMT hstmt, SQLSMALLINT *colids, SQLSMALLINT numcols); extern void print_result_series(HSTMT hstmt, SQLSMALLINT *colids, SQLSMALLINT numcols); extern void print_result_meta(HSTMT hstmt); extern void print_result(HSTMT hstmt); extern const char *datatype_str(SQLSMALLINT datatype); extern const char *nullable_str(SQLSMALLINT nullable);
* Re: Problem in SQLFreeHandle (Statement) @ 2014-06-17 19:07 Jade Koskela <jkoskela0@gmail.com> parent: Heikki Linnakangas <hlinnakangas@vmware.com> 1 sibling, 0 replies; 7+ messages in thread From: Jade Koskela @ 2014-06-17 19:07 UTC (permalink / raw) To: Heikki Linnakangas <hlinnakangas@vmware.com>; +Cc: pgsql-odbc 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 [4466282496]*** LEAVE_CONN_CS [140210608634616] *** <- count=1 [4466282496]*** LEAVE_CONN_CS [140210608634616] *** <- count=0 [4466282496]*** LEAVE_CONN_CS [140210608634616] *** <- count=0 [140735257568016][[SQLFreeHandle]] [140735257568016]*** ENTER_CONN_CS [140210608634616] *** <-thread blocks on mutex here, should be unlocked? On Thu, Jun 12, 2014 at 6:30 AM, Heikki Linnakangas <hlinnakangas@vmware.com > wrote: > On 06/12/2014 05:30 AM, Jade Koskela wrote: > >> Hello, >> I am using psql-odbc 9.3 on OSX. >> > > Which version, exactly? There were some changes to locking in > SQLFreeHandle in 09.03.0200. Not sure if those changes might've fixed this, > or caused it. > > Can easily you test on another platform, or with different version of > unixODBC? > > > I have one thread running a query, and >> another thread cancels the query using SQLCancel. The thread running the >> query successfully cancels and exits the driver code. When we try to free >> the statement handle, it blocks on a mutex doing ENTER_CONN_CS, or >> something similar in SQLFreeHandle. >> >> Does anyone have any input on this problem? I don't see who is holding the >> mutex, the thread running the query is done. >> > > I tried to reproduce this with the attached little test program. It works > fine. Can you post the test program you're using, or modify the attached so > that it exhibits the behavior you're seeing, please? > > - Heikki > > -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc Attachments: mylog_jkoskela50004.log (application/octet-stream, 2.9M) download ^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Problem in SQLFreeHandle (Statement) @ 2014-06-17 20:06 Jade Koskela <jkoskela0@gmail.com> parent: Heikki Linnakangas <hlinnakangas@vmware.com> 1 sibling, 2 replies; 7+ messages in thread From: Jade Koskela @ 2014-06-17 20:06 UTC (permalink / raw) To: Heikki Linnakangas <hlinnakangas@vmware.com>; pgsql-odbc 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 [4466282496]*** LEAVE_CONN_CS [140210608634616] *** <- count=1 [4466282496]*** LEAVE_CONN_CS [140210608634616] *** <- count=0 [4466282496]*** LEAVE_CONN_CS [140210608634616] *** <- count=0 [140735257568016][[SQLFreeHandle]] [140735257568016]*** ENTER_CONN_CS [140210608634616] *** <-thread blocks on mutex here, should be unlocked? On Thu, Jun 12, 2014 at 6:30 AM, Heikki Linnakangas <hlinnakangas@vmware.com > wrote: > On 06/12/2014 05:30 AM, Jade Koskela wrote: > >> Hello, >> I am using psql-odbc 9.3 on OSX. >> > > Which version, exactly? There were some changes to locking in > SQLFreeHandle in 09.03.0200. Not sure if those changes might've fixed this, > or caused it. > > Can easily you test on another platform, or with different version of > unixODBC? > > > I have one thread running a query, and >> another thread cancels the query using SQLCancel. The thread running the >> query successfully cancels and exits the driver code. When we try to free >> the statement handle, it blocks on a mutex doing ENTER_CONN_CS, or >> something similar in SQLFreeHandle. >> >> Does anyone have any input on this problem? I don't see who is holding the >> mutex, the thread running the query is done. >> > > I tried to reproduce this with the attached little test program. It works > fine. Can you post the test program you're using, or modify the attached so > that it exhibits the behavior you're seeing, please? > > - Heikki > > -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc Attachments: mylog_tail.log (application/octet-stream, 55.9K) download ^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Problem in SQLFreeHandle (Statement) @ 2014-06-17 20:13 Barry Bell <Barry.Bell@hartehanks.com> parent: Jade Koskela <jkoskela0@gmail.com> 1 sibling, 0 replies; 7+ messages in thread From: Barry Bell @ 2014-06-17 20:13 UTC (permalink / raw) To: Jade Koskela <jkoskela0@gmail.com>; Heikki Linnakangas <hlinnakangas@vmware.com>; pgsql-odbc I was able to login with the olddominion login and download the file (This is normal FTP , this is NOT SFTP) Using port 21 Which port are you using? Barry Bell Senior Developer/Analyst Logistics Harte Hanks 1525 NW 3rd ST Deerfield Beach FL, 33442 954-429-3771 Ext 267 office 954-281-1464 fax [https://4b2685446389bc779b46-5f66fbb59518cc4fcae8900db28267f5.ssl.cf2.rackcdn.com/email-sig-hh.png]<http://www.hartehanks.com/; hartehanks.com<http://www.hartehanks.com; / linkedin<http://www.linkedin.com/company/hartehanks; / twitter<http://www.twitter.com/HarteHanks; / facebook<http://www.facebook.com/HarteHanks; From: pgsql-odbc-owner@postgresql.org [mailto:pgsql-odbc-owner@postgresql.org] On Behalf Of Jade Koskela Sent: Tuesday, June 17, 2014 4:06 PM To: Heikki Linnakangas; pgsql-odbc@postgresql.org Subject: Re: [ODBC] Problem in SQLFreeHandle (Statement) 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 [4466282496]*** LEAVE_CONN_CS [140210608634616] *** <- count=1 [4466282496]*** LEAVE_CONN_CS [140210608634616] *** <- count=0 [4466282496]*** LEAVE_CONN_CS [140210608634616] *** <- count=0 [140735257568016][[SQLFreeHandle]] [140735257568016]*** ENTER_CONN_CS [140210608634616] *** <-thread blocks on mutex here, should be unlocked? On Thu, Jun 12, 2014 at 6:30 AM, Heikki Linnakangas <hlinnakangas@vmware.com<mailto:hlinnakangas@vmware.com>> wrote: On 06/12/2014 05:30 AM, Jade Koskela wrote: Hello, I am using psql-odbc 9.3 on OSX. Which version, exactly? There were some changes to locking in SQLFreeHandle in 09.03.0200. Not sure if those changes might've fixed this, or caused it. Can easily you test on another platform, or with different version of unixODBC? I have one thread running a query, and another thread cancels the query using SQLCancel. The thread running the query successfully cancels and exits the driver code. When we try to free the statement handle, it blocks on a mutex doing ENTER_CONN_CS, or something similar in SQLFreeHandle. Does anyone have any input on this problem? I don't see who is holding the mutex, the thread running the query is done. I tried to reproduce this with the attached little test program. It works fine. Can you post the test program you're using, or modify the attached so that it exhibits the behavior you're seeing, please? - Heikki Attachments:^ permalink raw reply [nested|flat] 7+ messages in threadimage001.png (image/png, 7.9K) download
view image
* Re: Problem in SQLFreeHandle (Statement) @ 2014-06-19 12:33 Heikki Linnakangas <hlinnakangas@vmware.com> parent: Jade Koskela <jkoskela0@gmail.com> 1 sibling, 1 reply; 7+ messages in thread From: Heikki Linnakangas @ 2014-06-19 12:33 UTC (permalink / raw) To: Jade Koskela <jkoskela0@gmail.com>; pgsql-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-odbc Attachments: 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.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^ permalink raw reply [nested|flat] 7+ messages in thread0002-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.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
* Re: Problem in SQLFreeHandle (Statement) @ 2014-06-19 22:26 Jade Koskela <jkoskela0@gmail.com> parent: Heikki Linnakangas <hlinnakangas@vmware.com> 0 siblings, 0 replies; 7+ messages in thread From: Jade Koskela @ 2014-06-19 22:26 UTC (permalink / raw) To: Heikki Linnakangas <hlinnakangas@vmware.com>; pgsql-odbc Thanks Heikki ! This has solved the problem. I'm still curious as to the root cause. According to the SUSv3, which OSX/Darwin is supposed to conform to, recursive mutexes should return error codes if unlocked from a different thread, but it is not undefined behavior (it is only undefined for normal mutexes). I tried to test this by doing a bunch of locking/unlocking of the same mutex from different threads and couldn't see any problems. However, if I compile without support for recursive mutexes then the problem disappears. Maybe we'll never know. Anyways back to other work, thanks again. Jade Koskela Tableau Software On Thu, Jun 19, 2014 at 5:33 AM, Heikki Linnakangas <hlinnakangas@vmware.com > wrote: > 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 > > ^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2014-06-19 22:26 UTC | newest] Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2014-06-12 02:30 Problem in SQLFreeHandle (Statement) Jade Koskela <jkoskela0@gmail.com> 2014-06-12 13:30 ` Heikki Linnakangas <hlinnakangas@vmware.com> 2014-06-17 19:07 ` Jade Koskela <jkoskela0@gmail.com> 2014-06-17 20:06 ` Jade Koskela <jkoskela0@gmail.com> 2014-06-17 20:13 ` Barry Bell <Barry.Bell@hartehanks.com> 2014-06-19 12:33 ` Heikki Linnakangas <hlinnakangas@vmware.com> 2014-06-19 22:26 ` Jade Koskela <jkoskela0@gmail.com>
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