Skip to content

Cleanup/Stop replication stream - is this possible? #5523

Description

@sitepodmatt

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'

Activity

  1. roji commented on Jan 11, 2024

    @roji
    Member

    /cc @Brar

  2. Brar commented on Jan 11, 2024

    @Brar
    Member

    The intended way to stop replication is to pass a cancellation token and cancel it.

  3. NinoFloris commented on Jan 11, 2024

    @NinoFloris
    Member

    @Brar could we make the enumerator dispose also do this?

  4. sitepodmatt commented on Jan 12, 2024

    @sitepodmatt
    Author

    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.

  5. Brar commented on Jan 12, 2024

    @Brar
    Member

    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.

  6. Brar commented on Jan 12, 2024

    @Brar
    Member

    @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).

  7. Brar commented on Jul 8, 2024

    @Brar
    Member

    @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.

  8. Brar commented on Oct 25, 2026

    @Brar
    Member

    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.

  9. reopened this on Oct 25, 2026
  10. self-assigned this
    on Oct 25, 2026
  11. added theissue type on Oct 25, 2026
  12. added this to the 11.0.0 milestone on Oct 25, 2026
  13. modified the milestones: 11.0.0, 12.0.0 on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions