Skip to content

Stop replication streams properly when they are disposed early - #6716

Open
Brar wants to merge 4 commits into
npgsql:mainfrom
Brar:repl/stream-lifecycle
Open

Brar wants to merge 4 commits into
npgsql:mainfrom
Brar:repl/stream-lifecycle

Conversation

@Brar

@Brar Brar commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

Depends on #6715, which isn't merged yet; its commit shows up here until it is.

  • Leaving the loop. Breaking out of await foreach over a replication stream, or an exception in the consumer, left the inner enumerator undisposed. The connection stayed in replication mode with its feedback timers running, and every other command failed with NpgsqlOperationInProgressException. The enumerators are now disposed, which stops streaming:
    • Physical replication ends copy mode with CopyDone, which keeps the walsender ready for the next START_REPLICATION and keeps temporary slots.
    • Logical replication cancels the command, since a logical walsender can't stream again after CopyDone. Like any error, the cancellation drops the session's temporary slots, which the logical StartReplication methods now document. Servers before PostgreSQL 10 ignore cancellation requests on replication connections, so with them the logical stop breaks the connection instead.
    • A final status update reports the processed position first; for logical replication this is best effort, since the walsender may handle the cancellation before it reads the update.
    • Skipping what the consumer didn't read of the message it held waits for the rest of that message at most WalReceiverTimeout, or 30 s if that's infinite or too large for a socket, like the rest of stopping.
    • If stopping fails, the connection is broken, and the exception that made the consumer stop is never replaced.
  • Late status updates. Status updates that were still on their way when a stream ended (e.g. from a timer or another thread) could be written after the end of the command, interleaved with the next one, also with the START_REPLICATION of the next stream. The end of the stream now waits for an update in progress, and later ones are dropped until the next stream is in copy mode.
  • Unread data. Whatever the consumer didn't read of the message it held is skipped first, also when a plugin like pgoutput reads the data directly from the buffer. Reading beyond the end of a message breaks the connection.
  • Cancellation. After cancellation, messages that were already on their way were still handed out until the server's error arrived. They are now skipped before they count as received, so the next START_REPLICATION from the reported position gets them again. This makes PhysicalReplicationTests reliable, so they run again (some of them also used colliding slot names).
  • A second stream. Starting a second stream while one was in progress overwrote the state of the first one before failing, so disposing the connection didn't stop the first stream anymore. It now fails before touching anything, and a start that fails leaves no state behind.
  • Oversize messages. A message bigger than the read buffer (e.g. a long notice) made the connector switch to a bigger buffer while the stream kept reading from the old one. This is fixed for the replication stream itself; the pgoutput decoder still reads from the old buffer, which Read pgoutput messages from the connector's current read buffer #6721 fixes.

How a real server gets there

Every case was run on PostgreSQL 18.6, most also on 14.24, against main, against #6715 where it matters, and against this PR:

  • Leaving the loop, then using the connection: a consumer stops early because it has what it needs or because its own code throws (a failing sink), and then runs IDENTIFY_SYSTEM, a slot command or StartReplication again. On main IDENTIFY_SYSTEM threw NpgsqlOperationInProgressException ("The connection is already in state 'Replication'"), pg_stat_replication kept showing the walsender in catchup with a moving reply_time (the status timer kept running), the next StartReplication failed the same way, and the server logged "unexpected EOF on standby connection" at the disposal. With this PR IDENTIFY_SYSTEM worked, the walsender went back to its startup state, and a second START_REPLICATION on the same walsender worked; test_decoding and pgoutput behaved the same, with the server logging the cancellation and the release of the slot.
  • Stopping inside a big message: a physical stream that catches up gets XLogData of up to 128 kB, 16 times the 8-kB read buffer. This PR skipped the rest of a 125,944-byte message of which the consumer had read 100 bytes, sent CopyDone and drained, and the connection worked; the same for 100 bytes of a 20,000-character pgoutput value.
  • An unread pgoutput row: a consumer that filters by relation stops after a change without reading its row. Main's DisposeAsync read the row's bytes as backend messages ("There is not enough data left in the buffer" from ParameterDescriptionMessage.Load, swallowed). This PR skipped the row and sent the cancellation, and the slot was released.
  • A second StartReplication: on main, "break, then StartReplication again" does exactly this, because the break leaves the first enumerator suspended. In Release, DisposeAsync then returned at once with both timers still set, and no cancellation reached the server ("unexpected EOF on standby connection"); a Debug build stopped the process on a Debug.Assert. With this PR the first stream was untouched, and DisposeAsync stopped it within about 25 ms.
  • Cancellation with data in flight, as during a catch-up or on a busy logical stream: with about 6 MB of backlog, main handed out 3 more 131,072-byte messages after the cancellation, this PR none.
  • The server goes away while the consumer throws (pg_terminate_backend, a shutdown, a lost connection): this PR logs event 50013, surfaces the consumer's exception and breaks the connection. Main has no stop path, so this test guards the new code.
  • A status update still on its way when the stream ends (a timer callback, or SendStatusUpdate on another thread): forced by holding the feedback semaphore and suspending the walsender, so that the next command stays blocked in its write. On main and on Handle the end of a replication stream at timeline switches and shutdown #6715, the late update failed with "Did not write the amount of bytes the message length specified", the connection broke, and the server logged "incomplete message from client". With this PR the end of the stream waited, and the update went out first. Without forcing, the window is one status-update write per stream end, so this is rare; there is no test for it.
  • Reading beyond a message: the only real case found is the stream abort that protocol version 4 with streaming = on misread before Fix pgoutput decoding of two-phase messages, stream aborts and bad data #6710. On main (18.6 and 16.15) it handed out garbage and then left the connector Ready while the walsender was still in copy mode, so that the next command was fatal on the server; with this PR it fails with "A replication message was read beyond its end." and breaks the connection. With that PR merged, the check is defensive hardening: it turns a silent misread into a clean break. No test.
  • A long notice mid-stream: notices can come between CopyData messages at any time. A realistic trigger is a publication row filter (PostgreSQL 15+) with websearch_to_tsquery and a row whose text holds more than about 8 KB of whitespace, which raises a notice that repeats the whole input. On 18.6 and 15.19 main failed with "Unknown replication message code 'd'" after the first row; with this PR the replication stream handles the notice, and the failure moves to the pgoutput decoder. The mock test's physical variant needs a debug setting on a real server.

Tests

  • ReplicationStopMockTests (11 cases, mock): on Handle the end of a replication stream at timeline switches and shutdown #6715's code, 10 of them fail (the stop tests time out because nothing ends copy mode, messages are handed out after the cancellation, a status update goes out before copy mode, and the skip when stopping has no bound), and Oversize_message_during_streaming hangs.
  • Live: ReplicationStopTests<Logical|Physical>.Stop_early_and_go_on, PgOutputStopTests.Stop_in_the_middle_of_a_change (a 20,000-character row), and the 9 PhysicalReplicationTests, which were [Explicit("Flakiness")] and now run ([NonParallelizable]).
  • The replication tests pass against PostgreSQL 18.6, in Debug and Release.

For the reviewer

  • New public constants in NpgsqlEventId: 50008 (StoppingReplication) and 50013 (StoppingReplicationFailed), both at Debug level. The class suppresses RS0016, so PublicAPI.Unshipped.txt doesn't change; there is no other API change.
  • On PostgreSQL 14 to 17, the server may not send pgoutput's Relation messages again in the next stream on the same connection after such a cancellation (on 14 after any error, on 15 to 17 if the connection created the slot with an exported snapshot; fixed in PostgreSQL 18 by 80b727eb9de). The pgoutput StartReplication docs say to start the next stream on a new connection, and the test avoids it.
  • Not changed: the logical stop doesn't wait for the server to read its final status update before it cancels. A busy walsender only reads replies once its output buffer fills, so stops would stall in large transactions.
  • Behavior changes, also in the 11.0 release notes: leaving the loop stops the stream and leaves the connection usable; a logical stop drops the session's temporary slots, and its final status update is best effort; SendStatusUpdate after the loop throws InvalidOperationException (send it before leaving the loop, or pass the processed position as walLocation when starting again); a second StartReplication while one is active throws NpgsqlOperationInProgressException.

Dependencies and merge order

Fixes #5523
Part of #6699 (the oversize-message bullet; #6721 completes it)

Brar and others added 2 commits November 8, 2026 00:08
- At the end of a historic timeline (e.g. after the standby we stream
  from got promoted), the server ends copy mode with CopyDone. Npgsql
  expected only CopyData and broke the connection ("Received backend
  message CopyDone while expecting CopyDataMessage"). It now answers
  with CopyDone, reads the next timeline the server sends, both
  CommandCompletes and ReadyForQuery, and ends the enumeration normally,
  leaving the connection usable. Status updates stop before our
  CopyDone, since the protocol doesn't allow CopyData after it. The next
  timeline is logged, and the StartReplication docs explain how to
  continue: IDENTIFY_SYSTEM returns the current timeline and
  TIMELINE_HISTORY the position where it branched off, which can be
  before the end of the WAL received on the old timeline.
- Asked to start at the end of a historic timeline, the server doesn't
  enter copy mode but sends the next timeline right away, which Npgsql
  treated as an unexpected message. The enumeration now ends right away.
- A walsender that exits after everything has been replicated (e.g. at
  server shutdown, npgsql#5752) sends CommandComplete, which also broke the
  connection with a misleading error. It now throws an NpgsqlException
  saying that the server ended the replication stream.
- In logical replication, unknown CopyData message types are skipped
  instead of breaking the connection, like the logical replication
  worker, which ignores them on purpose. Physical replication keeps
  rejecting them, like walreceiver, since it must not lose WAL.
  Keepalive fields that newer servers may add are skipped in both.
- In physical replication, the received position reported to the server
  was the server's own WAL end, not the end of the WAL received, so
  pg_stat_replication.write_lsn and synchronous_commit = remote_write
  treated WAL as written before it arrived. It is now the end of the
  data the consumer is done with, like walreceiver reports what it
  wrote.
- LastReceivedLsn starts over at the start position of each stream
  instead of keeping the maximum of all previous streams, which was
  wrong when continuing on a new timeline at a lower position.
- The XLogData stream is disposed after each message instead of later in
  cleanup, where it could skip data based on a stale position: a server
  error that ended the stream after a keepalive (e.g. after
  pg_cancel_backend) turned into a timeout and a broken connection.

The new cluster tests stream physically across a standby's promotion
and through a fast shutdown, using the throwaway clusters from
NPGSQL_TEST_PG_BIN.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Breaking out of a replication enumeration, or an exception in the
  consumer, left the inner enumerator undisposed. The connection stayed
  in replication mode with its feedback timers running, and every other
  command failed with NpgsqlOperationInProgressException. The
  enumerators are now disposed, which stops streaming: physical
  replication ends copy mode with CopyDone, which keeps the walsender
  ready for the next START_REPLICATION and keeps temporary slots;
  logical replication cancels the command, since a logical walsender
  can't stream again after CopyDone. Like any error, the cancellation
  drops the temporary slots of the session (documented on the logical
  StartReplication methods). Servers before PostgreSQL 10 ignore
  cancellation requests on replication connections, so with them the
  logical stop breaks the connection instead.
  A final status update reports the processed position first; for
  logical replication this is best effort, since the walsender may
  handle the cancellation before it reads the update. If stopping fails,
  the connection is broken; the exception that made the consumer stop is
  never replaced. On PostgreSQL 14-17, the server may not send
  pgoutput's Relation messages again in the next stream on the same
  connection after such a cancellation (on 14 after any error, on 15-17
  if the connection created the slot with an exported snapshot; fixed in
  PostgreSQL 18 by 80b727eb9de). This is documented on the pgoutput
  StartReplication, and the test avoids it.
- Status updates that were still on their way when a stream ended (e.g.
  from a timer or another thread) could be written after the end of the
  command, interleaved with the next one. They are now dropped.
- Whatever the consumer didn't read of the message it held is skipped
  first, also when a plugin like pgoutput reads the data directly from
  the buffer. Reading beyond the end of a message breaks the connection.
- After cancellation, messages that were already on their way were still
  handed out until the server's error arrived. They are now skipped.
  This makes the physical replication tests reliable, so they run again
  (some of them also used colliding slot names).
- Starting a second stream while one was in progress overwrote the state
  of the first one before failing, so disposing the connection didn't
  stop the first stream anymore. It now fails before touching anything,
  and a start that fails leaves no state behind.
- A message bigger than the read buffer (e.g. a long notice) made the
  connector switch to a bigger buffer while the stream kept reading from
  the old one. This is fixed for the replication stream itself; the
  pgoutput decoder still reads from the old buffer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Brar
Brar force-pushed the repl/stream-lifecycle branch from 909d574 to 37d8983 Compare November 7, 2026 23:34
Copilot AI balanced review requested due to automatic review settings November 7, 2026 23:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved replication races can corrupt the protocol on immediate restart, acknowledge WAL before it arrives, and advance logical positions or send replies after cancellation.

4 open issues
What changed in this PR

This PR adds early-stop cleanup to Npgsql replication streams so leaving an enumeration can release replication mode and allow connection reuse, addressing #5523. It also includes the stream-end handling from dependency #6715.

Changes:

  • Dispose inner enumerators and stop physical or logical replication using the appropriate protocol.
  • Coordinate feedback shutdown, handle unread payloads and cancellation, and follow read-buffer replacements.
  • Add mock and live regression tests, restore physical replication tests, and document lifecycle behavior.
File Description
test/​Npgsql.Tests/​Replication/​ReplicationStreamEndMockTests.cs Tests timeline endings, server errors, and received positions.
test/​Npgsql.Tests/​Replication/​ReplicationStopTests.cs Tests early stops and connection reuse against live servers.
test/​Npgsql.Tests/​Replication/​ReplicationStopMockTests.cs Tests stopping, cancellation, overlapping starts, and oversized notices.
test/​Npgsql.Tests/​Replication/​PhysicalReplicationTests.cs Re-enables tests without parallel execution and fixes slot-name collisions.
test/​Npgsql.Tests/​Replication/​PhysicalReplicationClusterTests.cs Tests standby promotion and server shutdown.
src/​Npgsql/​Replication/​TestDecoding/​TestDecodingConnectionExtensions.cs Documents logical early-stop behavior.
src/​Npgsql/​Replication/​ReplicationConnection.cs Implements stream cleanup, feedback coordination, and protocol handling.
src/​Npgsql/​Replication/​PhysicalReplicationConnection.cs Disposes inner enumerators and documents timeline continuation.
src/​Npgsql/​Replication/​PgOutput/​PgOutputConnectionExtensions.cs Documents cancellation effects and connection-reuse limitations.
src/​Npgsql/​Replication/​Internal/​LogicalReplicationConnectionExtensions.cs Disposes inner enumerators and supplies the starting WAL position.
src/​Npgsql/​NpgsqlEventId.cs Adds replication lifecycle event identifiers.
src/​Npgsql/​LogMessages.cs Adds stream-ending and stop-failure log messages.

Review effort: Balanced

- A status update could go out while a stream started, before the
  server's CopyBothResponse: one sent then, or one that had waited
  while the previous stream ended and only got its turn after the next
  stream reset the flag that stops them. In the write buffer, it could
  interleave with START_REPLICATION. Status updates now stay stopped
  until the server is in copy mode, and are allowed again under the
  feedback semaphore.
- Stopping a stream skipped the rest of the message the consumer held
  with the stream's read timeout, which is infinite with an infinite
  WalReceiverTimeout, so a server that stopped sending in the middle of
  a message made the stop hang. The skip now has the bound of the rest
  of stopping.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 8, 2026 10:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved cancellation races and received-position accounting errors can yield messages after cancellation, skip messages on resume, or deadlock synchronous commits.

1 open issue
2 previously missed issues

In code that hasn't changed since last review

Medium severity Enforce an overall deadline when skipping a trickling payload

src/​Npgsql/​Replication/​ReplicationConnection.cs:697

This sets an inactivity timeout rather than an overall deadline: the read buffer restarts its timeout after successful reads. A trickling message tail can therefore keep disposal pending far beyond WalReceiverTimeout or the 30-second fallback. Enforce an overall deadline for the skip, breaking the connector and awaiting the outstanding read when it expires, and cover this with a trickling-payload test.

Medium severity Apply the timeout range check before installing the fallback

src/​Npgsql/​Replication/​ReplicationConnection.cs:1114

ToBufferTimeout treats positive values above int.MaxValue milliseconds as infinite, but this fallback does not. With WalReceiverTimeout = TimeSpan.FromDays(30), stopping therefore installs an infinite read timeout and can wait indefinitely for a missing message tail or stream-end response. Apply the same representable-range check here so effectively infinite settings receive the 30-second fallback.

4 resolved since last review

Review effort: Balanced

The bound for stopping used WalReceiverTimeout unless it was zero or
infinite, so a value that is too large for the buffers' timeouts (more
than int.MaxValue milliseconds, e.g. 30 days), which they treat as no
timeout, made stopping wait forever again. Such values now get the
30-second fallback too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 8, 2026 12:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@Brar

Brar commented Nov 8, 2026

Copy link
Copy Markdown
Member Author

On the two points in the summary of the latest review:

  • The timeout range is fixed in 3d29849: a WalReceiverTimeout too large for the buffers' timeouts (e.g. 30 days), which they treat as no timeout, now gets the 30-second fallback too, and Stopping_waits_for_the_rest_of_a_held_message_with_a_bound checks that case.
  • The overall deadline I'd leave: the bound applies to each read, like Npgsql's other timeouts, so it stops a peer that stalled, not one that's slow. A slow link that keeps delivering the rest of a message (at most 128 kB) is making progress, and an overall deadline would turn a slow stop into a broken connection.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cleanup/Stop replication stream - is this possible?

2 participants