Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wv55K-0007Rc-48 for pgsql-odbc@arkaria.postgresql.org; Thu, 12 Jun 2014 13:30:30 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Wv55J-0006NB-HR for pgsql-odbc@arkaria.postgresql.org; Thu, 12 Jun 2014 13:30:29 +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 1Wv55I-0006Mm-6F for pgsql-odbc@postgresql.org; Thu, 12 Jun 2014 13:30:28 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1Wv558-0007ep-E4 for pgsql-odbc@postgresql.org; Thu, 12 Jun 2014 13:30:24 +0000 Received: from sc9-mailhost1.vmware.com (sc9-mailhost1.vmware.com [10.113.161.71]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id 24DB69818D; Thu, 12 Jun 2014 06:30:16 -0700 (PDT) Received: from zcs-prod-ext-mta-1.vmware.com (zcs-prod-ext-mta-1.vmware.com [10.113.62.223]) by sc9-mailhost1.vmware.com (Postfix) with ESMTP id 1853FF4B32; Thu, 12 Jun 2014 06:30:16 -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 CA86C12002A; Thu, 12 Jun 2014 06:30:28 -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, 12 Jun 2014 06:30:27 -0700 (PDT) Message-ID: <5399AB65.5040608@vmware.com> Date: Thu, 12 Jun 2014 16:30:13 +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: In-Reply-To: Content-Type: multipart/mixed; boundary="------------010101090900060104060307" X-Pg-Spam-Score: -7.6 (-------) 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. --------------010101090900060104060307 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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 --------------010101090900060104060307 Content-Type: text/x-csrc; name="cancel-race.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="cancel-race.c" #include #include #include #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; } --------------010101090900060104060307 Content-Type: text/x-csrc; name="common.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="common.c" #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); } --------------010101090900060104060307 Content-Type: text/x-chdr; name="common.h" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="common.h" #include #include #include #ifdef WIN32 #include #endif #include #include #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); --------------010101090900060104060307 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 --------------010101090900060104060307--