pg.ddx.io  GitHub: pgjdbc/pgjdbc (issues + PRs as email threads)  
help / color / mirror / Atom feed
From: davecramer (@davecramer) <noreply+davecramer@github.com>
To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>
Subject: Re: [pgjdbc/pgjdbc] PR #4331: fix(copy): keep the COPY active when a read times out
Date: Thu, 05 Nov 2026 11:25:17 +0000
Message-ID: <gh-pgjdbc-pgjdbc-4331-c6656774966@github.com> (raw)
In-Reply-To: <gh-pgjdbc-pgjdbc-4331@github.com>
References: <gh-pgjdbc-pgjdbc-4331@github.com>

I found the description hard to follow, so here is a rewrite you could use. It is about half the length and lists all eleven new tests. The current text says "six new tests" and its table has seven rows, but the PR adds ten tests to `CopyTest` and one to `LogicalReplicationTest`.

I left out "each checked to fail without its part of the change", because I could only confirm that for `idleStreamSurvivesSocketReadTimeout`. I also left out the paragraph about your own test run. If you take the `hasLockOn` change from my review, the "What I changed" section needs a line saying a COPY on a closed connection reports itself inactive, and the test list needs those two tests.

````markdown
## Why

Since 42.7.11, logical replication with the blocking `PGReplicationStream.read()` fails as soon as the slot has been idle for one status interval. The error is `Tried to write to an inactive copy operation` (#4328).

Here is what happens. The replication stream sets the socket timeout to the status interval on purpose. When the read times out, the stream sends a standby status update and reads again. In 42.7.11, b5080e76f (the fix for #3957) made `readFromCopy` release the COPY lock on any `IOException`. A read timeout is an `IOException`, so the timeout ended the COPY, and the status update after it failed.

You only see this with `socketTimeout` set. Without it, the driver catches the timeout itself and reads again.

The same change breaks `COPY TO STDOUT` with `socketTimeout`. After a timeout the COPY is no longer active, so `cancelCopy()` throws `Tried to cancel an inactive copy operation`, and the server is left in COPY mode.

## What I changed

I keep the COPY active when a read times out between two messages. That is the one failure where reading again is safe, and replication depends on it. `processNotifies` already handles a read timeout the same way.

I close the connection when a read times out inside a message. By then the type byte has been read, so reading again would treat the rest of that message as a new one.

For every other I/O failure during a COPY, I close the connection and keep the lock. Releasing the lock said the connection was free to use when it was out of sync. Closing it is what the rest of `QueryExecutorImpl` does after an I/O failure. `waitOnLock()` now gives up once the connection is closed, so the hang from #3957 stays fixed.

`startCopy` and `cancelCopy` also close the connection on an I/O failure, because neither can be retried. Retrying `startCopy` would send the query twice.

`unlock()` now wakes every waiting thread. With `signal()`, one thread woke and the others kept waiting for a COPY that had already ended.

In a separate commit, I made `SEPARATE_THREAD` the default test timeout mode. A test stuck in a socket read ignores interrupts, so without this a stuck test hangs the build. That commit can be dropped on its own.

## Tests

I added eleven tests:

- **A read timeout between messages:** `CopyTest.readTimeoutKeepsCopyActive` checks that the COPY stays active and `cancelCopy()` still works. `LogicalReplicationTest.idleStreamSurvivesSocketReadTimeout` checks that a replication stream keeps working after being idle for several status intervals.
- **A read timeout inside a message:** `readTimeoutInsideMessageEndsTheCopy` checks that this closes the connection. `readTimeoutAfterAnErrorResponseStillReportsTheError` checks that an error the server sent before the timeout still reaches the caller.
- **Failures that close the connection:** `startCopyFailureClosesConnection`, `cancelCopyFailureClosesConnection`, `writeToCopyFromByteStreamWriterFailureClosesConnection` and `endCopyGivesTheConnectionUpWhenItCannotReleaseTheLock`.
- **Threads waiting for the COPY lock:** `waitingThreadGivesUpWhenCopyDiesWithConnection`, `allWaitingThreadsResumeWhenCopyEnds` and `interruptingAThreadWaitingForTheCopyKeepsTheInterruptFlag`.

`TestUtil.openReplicationConnection` has a new overload that takes a `Consumer<Properties>`, so the replication test can set `socketTimeout` itself.

## What I left out

`endCopy` still closes the connection on a read timeout. Reading again there would be safe on the wire, but three callers expect the current behavior:

- `CopyManager.copyIn` would send a cancel that fails and hides the timeout.
- `PGCopyOutputStream.close()` would leave a pooled connection holding the lock.
- `PGCopyOutputStream.endCopy()` would send up to 64 KB of data twice.

Fixing those changes how the COPY API behaves, so it belongs in its own PR.
````


view thread (14+ messages)

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