postgres.git / summary / log / commit / refs

commit    d21a3e1fe5f454355efbc3bccbd2b4d81e0f7c27
Author:   Michael Paquier <michael@paquier.xyz>
Date:     Wed Oct 14 03:05:12 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 18.7, 19.0

    Fix potential use-after-frees in WAL sender
    
    A WAL sender tracks its WAL reader and logical decoding context as two
    static variables, which were left set after being freed for a
    START_REPLICATION command.  The WAL reader is used for in a physical
    replication command, both the WAL reader and the logical decoding
    context are used for a logical replication command.
    
    Both variables are now cleared once their contents are released at the
    end of each replication command, to keep the release of the resources
    and the maintenance of the pointers close to each other.  Some more
    cleanup is done by WalSndErrorCleanup(), as well, once the WAL reader is
    clear.
    
    The assertion added by 879ad9f90e83 is moved before the execution of a
    replication command, checking now that both the WAL reader and the
    logical decoding context are never set before the execution of a new
    command.
    
    Author: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
    Reviewed-by: Michael Paquier <michael@paquier.xyz>
    Discussion: https://postgr.es/m/CAJTYsWW+hdmmVt_A4hvGv2_5RWK8-kaOf5ncT65=5i_hyD=BHg@mail.gmail.com
    Backpatch-through: 14


src/backend/replication/walsender.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index 47aec4cd2b2..af7b3b41ac1 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -351,6 +351,9 @@ WalSndErrorCleanup(void) if (xlogreader != NULL && xlogreader->seg.ws_file >= 0) wal_segment_close(xlogreader); + xlogreader = NULL; + logical_decoding_ctx = NULL; + if (MyReplicationSlot != NULL) ReplicationSlotRelease(); @@ -981,6 +984,9 @@ StartReplication(StartReplicationCmd *cmd) Assert(streamingDoneSending && streamingDoneReceiving); } + XLogReaderFree(xlogreader); + xlogreader = NULL; + if (cmd->slotname) ReplicationSlotRelease(); @@ -1538,6 +1544,8 @@ StartLogicalReplication(StartReplicationCmd *cmd) WalSndLoop(XLogSendLogical); FreeDecodingContext(logical_decoding_ctx); + logical_decoding_ctx = NULL; + xlogreader = NULL; ReplicationSlotRelease(); replication_active = false; @@ -2045,6 +2053,9 @@ exec_replication_command(const char *cmd_string) CHECK_FOR_INTERRUPTS(); + Assert(xlogreader == NULL); + Assert(logical_decoding_ctx == NULL); + /* * Prepare to parse and execute the command. * @@ -2202,7 +2213,6 @@ exec_replication_command(const char *cmd_string) /* dupe, but necessary per libpqrcv_endstreaming */ EndReplicationCommand(cmdtag); - Assert(xlogreader != NULL); break; } [parent: c6a51ad94a23]