Skip to content

Commit

Permalink
fix: properly set cancel socket timeout (#2044)
Browse files Browse the repository at this point in the history
  • Loading branch information
Powerrr committed Feb 16, 2021
1 parent 9d6ab68 commit e551d1a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void sendQueryCancel() throws SQLException {
cancelStream =
new PGStream(pgStream.getSocketFactory(), pgStream.getHostSpec(), cancelSignalTimeout);
if (cancelSignalTimeout > 0) {
cancelStream.getSocket().setSoTimeout(cancelSignalTimeout);
cancelStream.setNetworkTimeout(cancelSignalTimeout);
}
cancelStream.sendInteger4(16);
cancelStream.sendInteger2(1234);
Expand Down
28 changes: 28 additions & 0 deletions pgjdbc/src/test/java/org/postgresql/test/jdbc2/StatementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.postgresql.PGProperty;
import org.postgresql.core.ServerVersion;
import org.postgresql.jdbc.PgStatement;
import org.postgresql.test.TestUtil;
import org.postgresql.test.util.StrangeProxyServer;
import org.postgresql.util.PSQLState;

import org.junit.After;
Expand All @@ -23,6 +25,7 @@
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand All @@ -31,6 +34,7 @@
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -844,6 +848,30 @@ public void testMultipleCancels() throws Exception {
assertEquals(0, sharedTimer.getRefCount());
}

@Test(timeout = 30000)
public void testCancelQueryWithBrokenNetwork() throws SQLException, IOException, InterruptedException {
// check that stmt.cancel() doesn't hang forever if the network is broken

ExecutorService executor = Executors.newSingleThreadExecutor();

try (StrangeProxyServer proxyServer = new StrangeProxyServer(TestUtil.getServer(), TestUtil.getPort())) {
Properties props = new Properties();
props.setProperty(TestUtil.SERVER_HOST_PORT_PROP, String.format("%s:%s", "localhost", proxyServer.getServerPort()));
PGProperty.CANCEL_SIGNAL_TIMEOUT.set(props, 1);

try (Connection conn = TestUtil.openDB(props); Statement stmt = conn.createStatement()) {
executor.submit(() -> stmt.execute("select pg_sleep(60)"));

Thread.sleep(1000);
proxyServer.stopForwardingAllClients();

stmt.cancel();
}
}

executor.shutdownNow();
}

@Test(timeout = 10000)
public void testCloseInProgressStatement() throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public void stopForwardingOlderClients() {
this.minAcceptedAt = System.currentTimeMillis();
}

public void stopForwardingAllClients() {
this.minAcceptedAt = Long.MAX_VALUE;
}

private void doAsync(Runnable task) {
Thread thread = new Thread(task);
thread.setDaemon(true);
Expand Down

0 comments on commit e551d1a

Please sign in to comment.