pg.ddx.io  GitHub: pgjdbc/pgjdbc (issues + PRs as email threads)  
help / color / mirror / Atom feed
From: ump45nose (@ump45nose) <noreply+ump45nose@github.com>
To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>
Subject: [pgjdbc/pgjdbc] PR #4525: fix: do not retry sslmode=prefer after a TCP connect timeout
Date: Sun, 08 Nov 2026 15:41:13 +0000
Message-ID: <gh-pgjdbc-pgjdbc-4525@github.com> (raw)

Fixes #4516

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing](https://github.com/pgjdbc/pgjdbc/blob/master/CONTRIBUTING.md) document?
* [x] Have you checked to ensure there aren't other open [Pull Requests](../../pulls) for the same update/change?

### Changes to Existing Features:

* [x] Does this break existing behaviour? If so please explain.
* [x] Have you added an explanation of what your changes do and why you'd like us to include them?
* [x] Have you written new tests for your core changes, as applicable?
* [x] Have you successfully run tests with your changes locally?

### What this changes

With the default `sslmode=prefer`, a TCP `Socket.connect` timeout was treated as an SSLRequest read timeout. The driver then opened a second, plaintext connection to the same host. After the first connect used up the `connectTimeout`, the second attempt was left with 1 ms, so it could not succeed and only added another wait.

The plaintext fallback from #3968 is still used when the TCP connection is already open and waiting for the SSLRequest response times out. A timeout from `Socket.connect` itself now ends the attempt on that host, the same way `sslmode=disable` already does.

This is done by wrapping the connect-time `SocketTimeoutException` so the `sslmode=prefer` fallback can tell it apart from a later SSLRequest read timeout.

### Tests

Added `SslPreferConnectTimeoutTest`, which uses a `socketFactory` and never opens a real network connection:

* `sslmode=prefer` after a connect timeout opens one socket (the regression from #4516)
* `sslmode=disable` still opens one socket on a connect timeout
* `sslmode=prefer` still opens a second socket when the SSLRequest response times out (#3968)

Ran locally:

```
./gradlew :postgresql:test --tests org.postgresql.test.jdbc2.SslPreferConnectTimeoutTest
./gradlew :postgresql:styleCheck
```

Both succeeded (3 tests, 0 failed). No PostgreSQL server is required.

### AI assistance

This change was prepared with assistance from an AI coding agent. I reviewed the issue, the `sslmode=prefer` fallback, and the new tests before opening this pull request.


inline patch 1: CHANGELOG.md, pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java, pgjdbc/src/test/java/org/postgresql/test/jdbc2/SslPreferConnectTimeoutTest.java (+198/-2 lines) download inline-1.patchdiff --git a/CHANGELOG.md b/CHANGELOG.md index a85ff9edcf..4776e2591c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). ## [Unreleased] ### Fixed +* fix: a TCP connect timeout under `sslmode=prefer` no longer retries the same host without SSL. The plaintext fallback added in 42.7.11 is only for a timeout while waiting for the SSLRequest response, not for `Socket.connect` itself [Issue #4516](https://github.com/pgjdbc/pgjdbc/issues/4516) * fix: `GSSInputStream.available()` now returns the number of decrypted bytes the stream holds and has not returned yet, instead of 0. Under GSS encryption, `PGConnection.getNotifications()` and non-blocking `COPY` reads no longer set a 1 ms socket timeout to probe for data while the stream still holds decrypted bytes [PR #4383](https://github.com/pgjdbc/pgjdbc/pull/4383) * fix: `PGStream.skip` no longer loops forever when the connection ends while the driver is discarding bytes, and no longer mistakes a stream that skips nothing for one that has ended. `InputStream.skip` may return zero with more data still coming, which a stream supplied through the `socketFactory` property is free to do; the driver now reads a byte in that case and treats only `-1` as the end [PR #4358](https://github.com/pgjdbc/pgjdbc/pull/4358) * fix: `LargeObjectManager` operations now work inside an active XA transaction. Since 42.7.13 an XA branch keeps the caller's `autoCommit=true`, and the guard rejected large objects as if the connection were idle. It now checks the server transaction state (`getTransactionState()`) instead of `autoCommit`, so a genuine auto-commit connection with no open transaction is still refused [Issue #4309](https://github.com/pgjdbc/pgjdbc/issues/4309) [PR #4310](https://github.com/pgjdbc/pgjdbc/pull/4310) diff --git a/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java b/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java index 0ed56e576e..6aaa9c9950 100644 --- a/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java +++ b/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java @@ -133,6 +133,19 @@ private static ISSPIClient createSSPI(PGStream pgStream, } } + /** + * A {@link SocketTimeoutException} from {@code Socket.connect} rather than from waiting for a + * response after the TCP connection is already open. {@code sslmode=prefer} retries only the + * latter, so a connect timeout must be distinguishable from an SSLRequest read timeout. + */ + @SuppressWarnings("serial") + static final class TcpConnectTimeoutException extends SocketTimeoutException { + TcpConnectTimeoutException(SocketTimeoutException cause) { + super(cause.getMessage()); + initCause(cause); + } + } + /** * Attempts a non-SSL connection after an SSL connection attempt failed. * Used when sslMode is PREFER: SSL is preferred but not required, @@ -218,7 +231,14 @@ private PGStream tryConnect(Properties info, SocketFactory socketFactory, HostSp } int maxSendBufferSize = PGProperty.MAX_SEND_BUFFER_SIZE.getInt(info); - PGStream newStream = new PGStream(socketFactory, hostSpec, connectTimeout, maxSendBufferSize); + PGStream newStream; + try { + newStream = new PGStream(socketFactory, hostSpec, connectTimeout, maxSendBufferSize); + } catch (SocketTimeoutException e) { + // Socket.connect timed out before any protocol bytes were exchanged. This is not the + // SSLRequest read timeout that sslmode=prefer should downgrade to plaintext for. + throw new TcpConnectTimeoutException(e); + } try { // Set the socket timeout if the "socketTimeout" property has been set. int socketTimeout = PGProperty.SOCKET_TIMEOUT.getInt(info); @@ -387,9 +407,13 @@ public QueryExecutor openConnectionImpl(HostSpec[] hostSpecs, Properties info) t throw e; } } catch (IOException e) { - if (sslMode == SslMode.PREFER && e instanceof SocketTimeoutException) { + if (sslMode == SslMode.PREFER + && e instanceof SocketTimeoutException + && !(e instanceof TcpConnectTimeoutException)) { // SSL negotiation timed out (server didn't respond to SSLRequest). // Since sslMode is PREFER, fall back to a non-encrypted connection. + // A TCP connect timeout is excluded: retrying the same unreachable host + // without SSL only repeats Socket.connect with whatever timeout is left. newStream = tryConnectWithoutSsl(info, socketFactory, hostSpec, gssEncMode, connectTimeoutMs, startNanos, e); } else if (sslMode == SslMode.ALLOW) { // Plaintext connection failed (e.g. server reset the connection diff --git a/pgjdbc/src/test/java/org/postgresql/test/jdbc2/SslPreferConnectTimeoutTest.java b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/SslPreferConnectTimeoutTest.java new file mode 100644 index 0000000000..dfa3a0caed --- /dev/null +++ b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/SslPreferConnectTimeoutTest.java @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2026, PostgreSQL Global Development Group + * See the LICENSE file in the project root for more information. + */ + +package org.postgresql.test.jdbc2; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.postgresql.PGProperty; +import org.postgresql.test.TestUtil; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.ResourceLock; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.Socket; +import java.net.SocketAddress; +import java.net.SocketTimeoutException; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicInteger; + +import javax.net.SocketFactory; + +/** + * {@code sslmode=prefer} must not retry a host whose TCP {@code connect} timed out. + * Falling back to plaintext after a connect timeout only repeats {@code Socket.connect} + * against the same unreachable host, usually with 1 ms of leftover timeout. + * A timeout while waiting for the SSLRequest response must still fall back, as #3968 intended. + * + * @see <a href="https://github.com/pgjdbc/pgjdbc/issues/4516">Issue #4516</a> + */ +@ResourceLock("SslPreferConnectTimeoutTest") +class SslPreferConnectTimeoutTest { + // Documentation range; the socket factory never opens a real connection. + private static final String URL = "jdbc:postgresql://192.0.2.1:5432/test"; + + @BeforeEach + void setUp() { + TestUtil.initDriver(); + CountingSocketFactory.reset(); + } + + @Test + void preferDoesNotRetryAfterConnectTimeout() { + SQLException ex = connectAndFail("prefer", CountingSocketFactory.Mode.CONNECT_TIMEOUT); + assertInstanceOf(SocketTimeoutException.class, ex.getCause()); + assertEquals(1, CountingSocketFactory.connectCount.get(), + "sslmode=prefer should not open a second socket after Socket.connect times out"); + } + + @Test + void disableConnectsOnceOnConnectTimeout() { + SQLException ex = connectAndFail("disable", CountingSocketFactory.Mode.CONNECT_TIMEOUT); + assertInstanceOf(SocketTimeoutException.class, ex.getCause()); + assertEquals(1, CountingSocketFactory.connectCount.get()); + } + + @Test + void preferStillFallsBackAfterSslResponseTimeout() { + SQLException ex = connectAndFail("prefer", CountingSocketFactory.Mode.SSL_RESPONSE_TIMEOUT); + assertInstanceOf(SocketTimeoutException.class, ex.getCause()); + assertEquals(2, CountingSocketFactory.connectCount.get(), + "sslmode=prefer should still retry without SSL when the SSLRequest response times out"); + } + + private static SQLException connectAndFail(String sslMode, CountingSocketFactory.Mode mode) { + CountingSocketFactory.mode = mode; + Properties props = new Properties(); + PGProperty.USER.set(props, "test"); + PGProperty.PASSWORD.set(props, "test"); + PGProperty.CONNECT_TIMEOUT.set(props, 5); + PGProperty.SOCKET_FACTORY.set(props, CountingSocketFactory.class.getName()); + PGProperty.GSS_ENC_MODE.set(props, "disable"); + PGProperty.SSL_MODE.set(props, sslMode); + return assertThrows(SQLException.class, () -> DriverManager.getConnection(URL, props)); + } + + /** + * Stands in for an unreachable host or a server that never answers SSLRequest. + * Instantiated by {@code ObjectFactory} from the {@code socketFactory} property. + */ + public static class CountingSocketFactory extends SocketFactory { + enum Mode { + CONNECT_TIMEOUT, + SSL_RESPONSE_TIMEOUT + } + + static final AtomicInteger connectCount = new AtomicInteger(); + static volatile Mode mode = Mode.CONNECT_TIMEOUT; + + static void reset() { + connectCount.set(0); + mode = Mode.CONNECT_TIMEOUT; + } + + @Override + public Socket createSocket() { + return new Socket() { + private boolean connected; + + @Override + public void connect(SocketAddress endpoint, int timeout) throws IOException { + connectCount.incrementAndGet(); + if (mode == Mode.CONNECT_TIMEOUT) { + throw new SocketTimeoutException("Connect timed out"); + } + connected = true; + } + + @Override + public boolean isConnected() { + return connected; + } + + @Override + public InputStream getInputStream() { + // First connect: pretend the SSLRequest response timed out. + // Later connects (the prefer fallback) return EOF so authentication + // fails immediately instead of retrying a SocketTimeoutException + // forever when no socketTimeout was requested. + final boolean sslResponseTimeout = mode == Mode.SSL_RESPONSE_TIMEOUT + && connectCount.get() == 1; + return new InputStream() { + @Override + public int read() throws IOException { + if (sslResponseTimeout) { + throw new SocketTimeoutException("Read timed out"); + } + return -1; + } + }; + } + + @Override + public OutputStream getOutputStream() { + return new ByteArrayOutputStream(); + } + }; + } + + @Override + public Socket createSocket(String host, int port) { + throw new UnsupportedOperationException(); + } + + @Override + public Socket createSocket(String host, int port, InetAddress localHost, int localPort) { + throw new UnsupportedOperationException(); + } + + @Override + public Socket createSocket(InetAddress host, int port) { + throw new UnsupportedOperationException(); + } + + @Override + public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) { + throw new UnsupportedOperationException(); + } + } +}

Message-ID: <gh-pgjdbc-pgjdbc-4525@github.com>
Permalink:  ../gh-pgjdbc-pgjdbc-4525@github.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