Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VoeVp-0004Ky-CP for pgsql-jdbc@arkaria.postgresql.org; Thu, 05 Dec 2013 19:23:01 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VoeVo-00011G-SX for pgsql-jdbc@arkaria.postgresql.org; Thu, 05 Dec 2013 19:23:00 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VoeVo-000113-63 for pgsql-jdbc@postgresql.org; Thu, 05 Dec 2013 19:23:00 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VoeVk-0006Ab-17 for pgsql-jdbc@postgresql.org; Thu, 05 Dec 2013 19:22:59 +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 92F56285B8 for ; Thu, 5 Dec 2013 11:22:52 -0800 (PST) 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 91538183EF for ; Thu, 5 Dec 2013 11:22:52 -0800 (PST) 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 7F4F8120031 for ; Thu, 5 Dec 2013 11:23:18 -0800 (PST) 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 for ; Thu, 5 Dec 2013 11:23:17 -0800 (PST) Message-ID: <52A0D28A.7040902@vmware.com> Date: Thu, 05 Dec 2013 21:22:50 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131005 Icedove/17.0.9 MIME-Version: 1.0 To: "pgsql-jdbc@postgresql.org" Subject: setQueryTimeout woes Content-Type: multipart/mixed; boundary="------------090901010001040001000007" X-Pg-Spam-Score: -6.9 (------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org This is a multi-part message in MIME format. --------------090901010001040001000007 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Our setQueryTimeout() is still a few bricks shy of a load. The fundamental problem is that the cancel timer is started on the setQueryTimeout() call - not when you actually start to execute a query. That leads to a couple of bugs: 1. If you call setQueryTimeout(5), wait 10 seconds, and call execute(), the cancel timer has already expired, and the statement will be allowed to run forever. Likewise, if you call setQueryTimeout(5), wait 4 seconds, and call execute(), the statement will be canceled after only 1 second. 2. If you call setQueryTimeout on a PreparedStatement, and execute the same statement several times, the timeout only takes affect on the first statement. 3. If you call setQueryTimeout on one Statement, but don't execute it, the timer will still fire, possible on an unrelated victim Statement. And one more bug: The run() method in the timer task contains a finally-block that calls killTimer(), but by the time that runs, the PreparedStatement might already have started running a new query, with a new timeout. And the finally-block erroneously cancels the timeout timer on the next query. I was getting spurious failures in the attached testSetQueryTimeoutOnPrepared() test case, which mysteriously went away when I added a simple System.out.println() call between the queries. That was enough of a delay to mask the race condition. The finally-block seems unnecessary to me, the timer has already fired so there's no harm in leaving the object in place until the finally-block in the execute() method removes it. I created a pull request on github about this. Patch also attached here. - Heikki --------------090901010001040001000007 Content-Type: text/x-diff; name="0001-Fix-various-setQueryTimeout-bugs.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-Fix-various-setQueryTimeout-bugs.patch" From 2c6eb23eeb8e5a77ef58d9e9840f5ee6078ec366 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Thu, 5 Dec 2013 20:21:50 +0200 Subject: [PATCH 1/1] Fix various setQueryTimeout bugs. 1. If you call setQueryTimeout(5), wait 10 seconds, and call execute(), the cancel timer has already expired, and the statement will be allowed to run forever. Likewise, if you call setQueryTimeout(5), wait 4 seconds, and call execute(), the statement will be canceled after only 1 second. 2. If you call setQueryTimeout on a PreparedStatement, and execute the same statement several times, the timeout only takes affect on the first statement. 3. If you call setQueryTimeout on one Statement, but don't execute it, the timer will still fire, possible on an unrelated victim Statement. The root cause of all of these bugs was that the timer was started at the setQueryTimeout() call, not on the execute() call. Also, remove the finally-block from the cancellation task's run-method, because that might erroneously cancel the timer for the next query, if a new query is started using the same statement fast enough. --- org/postgresql/jdbc2/AbstractJdbc2Statement.java | 79 +++++++++++++----------- org/postgresql/test/jdbc2/StatementTest.java | 50 +++++++++++++++ 2 files changed, 93 insertions(+), 36 deletions(-) diff --git a/org/postgresql/jdbc2/AbstractJdbc2Statement.java b/org/postgresql/jdbc2/AbstractJdbc2Statement.java index 05a91ca..f458b00 100644 --- a/org/postgresql/jdbc2/AbstractJdbc2Statement.java +++ b/org/postgresql/jdbc2/AbstractJdbc2Statement.java @@ -76,7 +76,7 @@ public abstract class AbstractJdbc2Statement implements BaseStatement /** Number of rows to get in a batch. */ protected int fetchSize = 0; - /** Timeout (in seconds) for a query (not used) */ + /** Timeout (in seconds) for a query */ protected int timeout = 0; protected boolean replaceProcessingEnabled = true; @@ -555,14 +555,13 @@ public abstract class AbstractJdbc2Statement implements BaseStatement result = null; try { - - - connection.getQueryExecutor().execute(queryToExecute, - queryParameters, - handler, - maxrows, - fetchSize, - flags); + startTimer(); + connection.getQueryExecutor().execute(queryToExecute, + queryParameters, + handler, + maxrows, + fetchSize, + flags); } finally { @@ -717,27 +716,6 @@ public abstract class AbstractJdbc2Statement implements BaseStatement if (seconds < 0) throw new PSQLException(GT.tr("Query timeout must be a value greater than or equals to 0."), PSQLState.INVALID_PARAMETER_VALUE); - - if (seconds == 0) { - killTimer(); - return; - } - - cancelTimer = new TimerTask() { - public void run() - { - try { - AbstractJdbc2Statement.this.cancel(); - } catch (SQLException e) { - } - finally - { - killTimer(); - } - } - }; - - Driver.addTimerTask( cancelTimer, seconds * 1000); timeout = seconds; } @@ -2906,12 +2884,17 @@ public abstract class AbstractJdbc2Statement implements BaseStatement handler = new BatchResultHandler(queries, parameterLists, updateCounts, wantsGeneratedKeysAlways); } - connection.getQueryExecutor().execute(queries, - parameterLists, - handler, - maxrows, - fetchSize, - flags); + try { + startTimer(); + connection.getQueryExecutor().execute(queries, + parameterLists, + handler, + maxrows, + fetchSize, + flags); + } finally { + killTimer(); + } if (wantsGeneratedKeysAlways) { generatedKeys = new ResultWrapper(((BatchResultHandler)handler).getGeneratedKeys()); @@ -3424,6 +3407,30 @@ public abstract class AbstractJdbc2Statement implements BaseStatement throw Driver.notImplemented(this.getClass(), "registerOutParameter(int,int,String)"); } + protected synchronized void startTimer() + { + if (timeout == 0) + return; + + /* + * there shouldn't be any previous timer active, but better safe than + * sorry. + */ + killTimer(); + + cancelTimer = new TimerTask() { + public void run() + { + try { + AbstractJdbc2Statement.this.cancel(); + } catch (SQLException e) { + } + } + }; + + Driver.addTimerTask( cancelTimer, timeout * 1000); + } + private synchronized void killTimer() { if ( cancelTimer != null ) diff --git a/org/postgresql/test/jdbc2/StatementTest.java b/org/postgresql/test/jdbc2/StatementTest.java index 0fb9373..9e16068 100644 --- a/org/postgresql/test/jdbc2/StatementTest.java +++ b/org/postgresql/test/jdbc2/StatementTest.java @@ -501,6 +501,56 @@ public class StatementTest extends TestCase } } + public void testSetQueryTimeoutWithSleep() throws SQLException, InterruptedException + { + // check that the timeout starts ticking at execute, not at the + // setQueryTimeout call. + Statement stmt = con.createStatement(); + try + { + stmt.setQueryTimeout(1); + Thread.sleep(3000); + stmt.execute("select pg_sleep(5)"); + this.fail( "statement should have been canceled by query timeout" ); + } catch( SQLException sqle ) + { + // state for cancel + if (sqle.getSQLState().compareTo("57014") != 0) + throw sqle; + } + } + + public void testSetQueryTimeoutOnPrepared() throws SQLException, InterruptedException + { + // check that a timeout set on a prepared statement works on every + // execution. + PreparedStatement pstmt = con.prepareStatement("select pg_sleep(5)"); + pstmt.setQueryTimeout(1); + for (int i = 1; i <= 3; i++) + { + try + { + ResultSet rs = pstmt.executeQuery(); + this.fail( "statement should have been canceled by query timeout (execution #" + i + ")" ); + } catch( SQLException sqle ) + { + // state for cancel + if (sqle.getSQLState().compareTo("57014") != 0) + throw sqle; + } + } + } + + public void testSetQueryTimeoutWithoutExecute() throws SQLException, InterruptedException + { + // check that a timeout set on one statement doesn't affect another + Statement stmt1 = con.createStatement(); + stmt1.setQueryTimeout(1); + + Statement stmt2 = con.createStatement(); + ResultSet rs = stmt2.executeQuery("SELECT pg_sleep(2)"); + } + public void testResultSetTwice() throws SQLException { Statement stmt = con.createStatement(); -- 1.8.4.3 --------------090901010001040001000007 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc --------------090901010001040001000007--