[libpostgresql-jdbc-java] 04/14: Statement.cancel was prone to race conditions even with a single threaded client. It would fire off a cancel message without waiting for an acknowledgement of its success. This resulted in future queries being cancelled when the cancel message was received by the backend. Ensure we get an EOF from the server before returning from the cancel call. This still does nothing about multi-threaded race conditions.

Emmanuel Bourg ebourg-guest at moszumanska.debian.org
Mon Jan 9 10:19:50 UTC 2017


This is an automated email from the git hooks/post-receive script.

ebourg-guest pushed a commit to tag REL8_1_405
in repository libpostgresql-jdbc-java.

commit b2731e463b2764777bd652cd3c95f01c6cf7b396
Author: Kris Jurka <books at ejurka.com>
Date:   Fri Dec 2 03:06:05 2005 +0000

    Statement.cancel was prone to race conditions even with a single
    threaded client.  It would fire off a cancel message without waiting
    for an acknowledgement of its success.  This resulted in future
    queries being cancelled when the cancel message was received by
    the backend.  Ensure we get an EOF from the server before returning             from the cancel call.  This still does nothing about multi-threaded
    race conditions.
    
    Original analysis and patch by Oliver Jowett
---
 org/postgresql/core/PGStream.java                  | 16 +++++++++++++++-
 org/postgresql/core/v2/ProtocolConnectionImpl.java |  3 ++-
 org/postgresql/core/v3/ProtocolConnectionImpl.java |  3 ++-
 org/postgresql/test/jdbc2/MiscTest.java            | 17 ++++++++++++++++-
 4 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/org/postgresql/core/PGStream.java b/org/postgresql/core/PGStream.java
index 3d0605b..c7dd8ae 100644
--- a/org/postgresql/core/PGStream.java
+++ b/org/postgresql/core/PGStream.java
@@ -3,7 +3,7 @@
 * Copyright (c) 2003-2005, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/core/PGStream.java,v 1.15 2005/04/20 00:10:58 oliver Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/core/PGStream.java,v 1.16 2005/06/25 00:53:15 oliver Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -21,6 +21,8 @@ import java.net.Socket;
 import java.sql.*;
 
 import org.postgresql.util.GT;
+import org.postgresql.util.PSQLState;
+import org.postgresql.util.PSQLException;
 
 /**
  * Wrapper around the raw connection to the server that implements some basic
@@ -501,6 +503,18 @@ public class PGStream
     }
 
     /**
+     * Consume an expected EOF from the backend
+     * @exception SQLException if we get something other than an EOF
+     */
+    public void ReceiveEOF() throws SQLException, IOException
+    {
+        int c = pg_input.read();
+        if (c < 0)
+            return;
+        throw new PSQLException(GT.tr("Expected an EOF from server, got: {0}", new Integer(c)), PSQLState.COMMUNICATION_ERROR);
+    }
+
+    /**
      * Closes the connection
      *
      * @exception IOException if an I/O Error occurs
diff --git a/org/postgresql/core/v2/ProtocolConnectionImpl.java b/org/postgresql/core/v2/ProtocolConnectionImpl.java
index 2b94fe7..b09a1b0 100644
--- a/org/postgresql/core/v2/ProtocolConnectionImpl.java
+++ b/org/postgresql/core/v2/ProtocolConnectionImpl.java
@@ -4,7 +4,7 @@
 * Copyright (c) 2004, Open Cloud Limited.
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/core/v2/ProtocolConnectionImpl.java,v 1.6 2005/04/20 00:10:58 oliver Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/core/v2/ProtocolConnectionImpl.java,v 1.7 2005/06/21 18:07:09 davec Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -93,6 +93,7 @@ class ProtocolConnectionImpl implements ProtocolConnection {
             cancelStream.SendInteger4(cancelPid);
             cancelStream.SendInteger4(cancelKey);
             cancelStream.flush();
+            cancelStream.ReceiveEOF();
             cancelStream.close();
             cancelStream = null;
         }
diff --git a/org/postgresql/core/v3/ProtocolConnectionImpl.java b/org/postgresql/core/v3/ProtocolConnectionImpl.java
index c0b0520..eedffed 100644
--- a/org/postgresql/core/v3/ProtocolConnectionImpl.java
+++ b/org/postgresql/core/v3/ProtocolConnectionImpl.java
@@ -4,7 +4,7 @@
 * Copyright (c) 2004, Open Cloud Limited.
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/core/v3/ProtocolConnectionImpl.java,v 1.7 2005/04/20 00:10:58 oliver Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/core/v3/ProtocolConnectionImpl.java,v 1.8 2005/06/21 18:07:09 davec Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -91,6 +91,7 @@ class ProtocolConnectionImpl implements ProtocolConnection {
             cancelStream.SendInteger4(cancelPid);
             cancelStream.SendInteger4(cancelKey);
             cancelStream.flush();
+            cancelStream.ReceiveEOF();
             cancelStream.close();
             cancelStream = null;
         }
diff --git a/org/postgresql/test/jdbc2/MiscTest.java b/org/postgresql/test/jdbc2/MiscTest.java
index f4ee26b..e11df3e 100644
--- a/org/postgresql/test/jdbc2/MiscTest.java
+++ b/org/postgresql/test/jdbc2/MiscTest.java
@@ -3,7 +3,7 @@
 * Copyright (c) 2004-2005, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/MiscTest.java,v 1.17 2005/01/11 08:25:48 jurka Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/MiscTest.java,v 1.18 2005/04/10 16:44:13 jurka Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -53,6 +53,21 @@ public class MiscTest extends TestCase
         TestUtil.closeDB(con);
     }
 
+    /**
+     * Ensure the cancel call does not return before it has completed.
+     * Previously it did which cancelled future queries.
+     */
+    public void testSingleThreadCancel() throws Exception
+    {
+        Connection con = TestUtil.openDB();
+        Statement stmt = con.createStatement();
+        for (int i=0; i<100; i++) {
+            ResultSet rs = stmt.executeQuery("SELECT 1");
+            rs.close();
+            stmt.cancel();
+        }
+    }
+
     public void testError() throws Exception
     {
         Connection con = TestUtil.openDB();

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/libpostgresql-jdbc-java.git



More information about the pkg-java-commits mailing list