You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cleanup/Stop replication stream - is this possible? #5523
Is there a way to stop replication? Once I get an IAsyncEnumerable of PgOutputRelationmessage, then the moment I get an enumerator from this and call movenext I can't seem to re-use the connection. Stopping iterating, and calling disposeasync on the given enumerator doesn't help. Trying to re-use the connection (for example in an integration test to drop the replication slot) results in The connection is already in state 'Replication'
Okay, I can kind of get it to work with creating a child cancellation token for startReplication, and cancelling that after I've .take(x) from the stream. The test is flakey. Adding a sleep after resolves the flakeyness, I guess this makes sense because I have no visibility on when the next await or explicit ct iscancellationrequest is called. It would be nice if this was deterministic.
could we make the enumerator dispose also do this?
Probably not easily with our current implementation.
My initial implementation had quite some complex logic around this (e. g., passing different cancellation tokens to StartReplication() and GetAsyncEnumerator(), an explicit API to stop replication, etc.).
When we (@roji and me) polished this for merging we removed most of this because it complicated things quite a bit and we deemed it not really necessary.
After all replication connections are typically long (more or less endlessly) running connections that (apart from tests) you typically don't start and stop on a regular base.
That being said, the current way of stopping (cancelling) replication should be deterministic to the point that at some point after cancelling the token you should get an OperationCanceledException which you can catch and move on.
If you control the writing side of the replicated tables you should also be able to make it even more deterministic as we do in our own tests.
As we know from experience though, flakiness is a thing in replication tests which I mostly attribute to PostgreSQL's not exactly deterministic cancellation mechanism.
@sitepodmatt if you show me a test, I can maybe help you improve it (make it more deterministic).
You can also have a look at Npgsql's replication tests which still are somewhat flaky but run correctly most of the time (most of them are enabled for our GitHub Actions test suite and we don't suffer too much).
@sitepodmatt I'm going ahead and close this now since you seem to have found a working solution.
Feel free to post here again, if you need more help with replication tests.
Reopening, since this is a bug: breaking out of await foreach over StartReplication, or an exception in the loop body, never disposes the inner enumerator (PhysicalReplicationConnection.cs:134-136, LogicalReplicationConnectionExtensions.cs:177-179). So the connection stays in the Replication state, and every later command throws NpgsqlOperationInProgressException ("The connection is already in state 'Replication'") (ReplicationConnection.cs:425-426). The feedback timers keep running too (inferred).
Cancelling the token works, but leaving the loop should stop the stream as well: physical replication can end COPY mode with CopyDone, logical replication with a cancel, and the connection stays usable.
Activity
roji commentedon Jan 11, 2024
/cc @Brar
Brar commentedon Jan 11, 2024
The intended way to stop replication is to pass a cancellation token and cancel it.
NinoFloris commentedon Jan 11, 2024
@Brar could we make the enumerator dispose also do this?
sitepodmatt commentedon Jan 12, 2024
Okay, I can kind of get it to work with creating a child cancellation token for startReplication, and cancelling that after I've .take(x) from the stream. The test is flakey. Adding a sleep after resolves the flakeyness, I guess this makes sense because I have no visibility on when the next await or explicit ct iscancellationrequest is called. It would be nice if this was deterministic.
Brar commentedon Jan 12, 2024
Probably not easily with our current implementation.
My initial implementation had quite some complex logic around this (e. g., passing different cancellation tokens to
StartReplication()andGetAsyncEnumerator(), an explicit API to stop replication, etc.).When we (@roji and me) polished this for merging we removed most of this because it complicated things quite a bit and we deemed it not really necessary.
After all replication connections are typically long (more or less endlessly) running connections that (apart from tests) you typically don't start and stop on a regular base.
That being said, the current way of stopping (cancelling) replication should be deterministic to the point that at some point after cancelling the token you should get an
OperationCanceledExceptionwhich you can catch and move on.If you control the writing side of the replicated tables you should also be able to make it even more deterministic as we do in our own tests.
As we know from experience though, flakiness is a thing in replication tests which I mostly attribute to PostgreSQL's not exactly deterministic cancellation mechanism.
Brar commentedon Jan 12, 2024
@sitepodmatt if you show me a test, I can maybe help you improve it (make it more deterministic).
You can also have a look at Npgsql's replication tests which still are somewhat flaky but run correctly most of the time (most of them are enabled for our GitHub Actions test suite and we don't suffer too much).
Brar commentedon Jul 8, 2024
@sitepodmatt I'm going ahead and close this now since you seem to have found a working solution.
Feel free to post here again, if you need more help with replication tests.
Brar commentedon Oct 25, 2026
Reopening, since this is a bug: breaking out of
await foreachoverStartReplication, or an exception in the loop body, never disposes the inner enumerator (PhysicalReplicationConnection.cs:134-136,LogicalReplicationConnectionExtensions.cs:177-179). So the connection stays in the Replication state, and every later command throws NpgsqlOperationInProgressException ("The connection is already in state 'Replication'") (ReplicationConnection.cs:425-426). The feedback timers keep running too (inferred).Cancelling the token works, but leaving the loop should stop the stream as well: physical replication can end COPY mode with CopyDone, logical replication with a cancel, and the connection stays usable.