Skip to content

pgoutput: NullReferenceException with two-phase slots and stream aborts, misread stream aborts with protocol V4 unless streaming = parallel #6696

Closed

Description

@Brar

Several bugs in the pgoutput decoder (PgOutputAsyncEnumerable.cs unless noted):

  1. A slot with two-phase decoding enabled (PostgreSQL 15+) sends prepared-transaction messages whatever proto_version the client requests (postgres logical.c:674). Npgsql allocates those message instances only for protocol V3 and later, so a V1 or V2 session fails with NullReferenceException at the first prepared transaction (:80-87, 450).
  2. PostgreSQL 18.0 to at least 18.6 can send a Stream Abort even with streaming off and at any protocol version (postgres bug #19616, fixed on REL_18_STABLE in caa463e3f8d). With V1, Npgsql has no instance for it either: NullReferenceException (:89-96, 441, postgres caa463e3f8d). With V4, Npgsql reads it as the 24-byte variant of 3 and misreads what follows (:428-435).
  3. With V4 and streaming = on, Npgsql reads every Stream Abort as the 24-byte parallel variant, but the server sends the abort LSN and timestamp only with streaming = parallel (:428-443, postgres pgoutput.c:1901, proto.c:1157-1177). The 16 extra bytes come from what follows, which is then misread (inferred).
  4. Transaction GIDs are read with synchronous I/O when they aren't fully buffered (:455 and the four similar reads).
  5. Unknown message codes and unknown tuple types in Update and Delete messages throw NotSupportedException, and unknown column kinds ArgumentOutOfRangeException (TupleEnumerator.cs:69), without breaking the connection (:321-322, 363-364, 507-509). In an Insert, an unknown tuple type is read as a new row, since only a Debug.Assert checks it (:261). The connection stays in the middle of the stream, in the Replication state, because the wrapper never disposes the inner enumerator (LogicalReplicationConnectionExtensions.cs:177-179): later commands throw NpgsqlOperationInProgressException, and the walsender keeps the slot.
  6. Reading a ReplicationValue after it was consumed (e.g. after the column enumeration moved past it) reads whatever comes next on the wire instead of throwing (ReplicationValue.cs:114-126, 128-130). Get<int?>() on a NULL fails in text mode (inferred: the conversion is resolved before the NULL check, ReplicationValue.cs:154, 157).
  7. The linked CancellationTokenSource of every pgoutput and test_decoding enumeration is never disposed (:108, TestDecodingAsyncEnumerable.cs:38).
  8. PgOutputReplicationOptions.Equals and GetHashCode compare the publication names by reference (PgOutputReplicationOptions.cs:174, 182).

1 and 2 are regressions in 9.0, where the per-version allocation came with protocol V4 support (ea3b161, #5761); 8.0 allocates every instance. 3 exists since V4 support (git log -S). 4, 5, 7 and 8 affect 8.0 too.

Activity

  1. added this to the 11.0.0 milestone on Oct 25, 2026
  2. self-assigned this
    on Oct 25, 2026
  3. added theissue type on Oct 25, 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

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions