From: vlsi (@vlsi) <noreply+vlsi@github.com>
To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>
Subject: [pgjdbc/pgjdbc] issue #4517: Reconnect after an SSL or GSS 'E' response gets the connect timeout from the start of the attempt
Date: Tue, 03 Nov 2026 08:48:31 +0000
Message-ID: <gh-pgjdbc-pgjdbc-4517@github.com> (raw)
**Describe the issue**
When the server answers `E` to an SSLRequest or a GSSENCRequest, the driver closes the socket and connects again. The new `Socket.connect` gets the connect timeout computed when the attempt started, not what is left of it, so the time spent on the first connect and on waiting for the response is not counted. In the reproducer below the first connect takes 4 s of a 5 s `connectTimeout`, and the reconnect still gets 4998 ms: the attempt spends 8 s in `connect`.
The three reconnects pass the `connectTimeout` that `tryConnect` computed at its start: [GSS `E`](https://github.com/pgjdbc/pgjdbc/blob/f2a48a2abf3d23bfd4139c556b9938aa96792792/pgjdbc/src/main/java/...), [GSS authentication failure under `gssEncMode=prefer`](https://github.com/pgjdbc/pgjdbc/blob/f2a48a2abf3d23bfd4139c556b9938aa96792792/pgjdbc/src/main/java/...), and [SSL `E`](https://github.com/pgjdbc/pgjdbc/blob/f2a48a2abf3d23bfd4139c556b9938aa96792792/pgjdbc/src/main/java/...). When the GSS and SSL paths both reconnect, one attempt calls `connect` three times. Since 2a8772f0e (42.7.11) each `tryConnect` call starts from the remaining budget, but these reconnects inside one call do not.
**Driver Version?**
master at f2a48a2ab.
**Java Version?**
21.0.9
**OS Version?**
macOS 26. The reproducer replaces the socket and sends nothing over the network.
**PostgreSQL Version?**
Not used: the reproducer never reaches a server.
**To Reproduce**
`ConnectTimeoutProbe.java` from #4516, in mode `slowE`: each `connect` takes 4 s and succeeds, and the socket returns a single `E` byte, so the server appears to refuse the SSLRequest.
```text
$ java -cp postgresql-42.7.15-SNAPSHOT-all.jar:. ConnectTimeoutProbe slowE prefer
56 ms connect 192.0.2.1 timeout=4998
4072 ms connect 192.0.2.1 timeout=4998
8092 ms failed: org.postgresql.util.PSQLException: The connection attempt failed.; cause: java.io.EOFException: End of stream reached while trying to read integer4
```
The `EOFException` comes from the probe, which sends nothing after the `E`.
**Expected behaviour**
Each reconnect gets `connectTimeout` minus the time already spent on this host, as the plaintext and SSL fallbacks in `openConnectionImpl` do. In the run above the second `connect` gets about 1000 ms (5000 − 4000).
#4082 proposes one deadline for every startup phase; this issue covers only the reconnects, which can use the existing `remainingConnectTimeout`. The reconnects stay within one host, so this does not depend on #4515, which changes where the budget starts.