Fix race between copying and dropping a replication slot
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
Hi,
(CCing Sawada-san for replication slot copying and dynamic logical
decoding.)
While looking into dynamic logical decoding toggling on PG 19+, I found
a race condition in copy_replication_slot() when the source slot is
concurrently dropped during the copy operation.
copy_replication_slot() copies the source slot in two phases: it first
reads the source slot's initial metadata, creates the destination slot
without holding the source slot, and then re-reads the source slot to
copy its latest LSN/xmin values and verify that it still exists.
However, the second-phase check does not verify the source slot's in_use
flag. Since dropping a slot only clears in_use without resetting its
persistent data, a concurrent drop of the source slot before the
destination slot reserves its resources can go undetected.
On PG 19+ with wal_level = replica, this is easy to hit because dropping
the last logical slot deactivates logical decoding, and
create_logical_replication_slot() then re-enables it for the destination
slot. Because the drop goes undetected, the copied slot is persisted
with LSNs preceding the deactivation record, causing subsequent decoding
to fail with "unexpected logical decoding status change 0".
Older branches are also affected, although there it requires a
checkpoint or VACUUM to remove WAL segments or catalog tuples referenced
by the stale restart_lsn and xmin values in that window, tripping
Assert(XLogGetLastRemovedSegno() < segno) or leaving a corrupted copied
slot.
Attached a patch to fix this by checking second_slot_contents.in_use and
ensuring MyReplicationSlot did not reuse src in the second phase. The
code fix applies back to PG 15.
--
Best Regards,
Zhijie Hou
On Thu, Nov 26, 2026 at 8:09 PM Zhijie Hou <houzhijie22@gmail.com> wrote:
Hi,
(CCing Sawada-san for replication slot copying and dynamic logical
decoding.)While looking into dynamic logical decoding toggling on PG 19+, I found
a race condition in copy_replication_slot() when the source slot is
concurrently dropped during the copy operation.copy_replication_slot() copies the source slot in two phases: it first
reads the source slot's initial metadata, creates the destination slot
without holding the source slot, and then re-reads the source slot to
copy its latest LSN/xmin values and verify that it still exists.However, the second-phase check does not verify the source slot's in_use
flag. Since dropping a slot only clears in_use without resetting its
persistent data, a concurrent drop of the source slot before the
destination slot reserves its resources can go undetected.On PG 19+ with wal_level = replica, this is easy to hit because dropping
the last logical slot deactivates logical decoding, and
create_logical_replication_slot() then re-enables it for the destination
slot. Because the drop goes undetected, the copied slot is persisted
with LSNs preceding the deactivation record, causing subsequent decoding
to fail with "unexpected logical decoding status change 0".
Thanks for reporting this. I agree with the problem statement.
Older branches are also affected, although there it requires a
checkpoint or VACUUM to remove WAL segments or catalog tuples referenced
by the stale restart_lsn and xmin values in that window, tripping
Assert(XLogGetLastRemovedSegno() < segno) or leaving a corrupted copied
slot.Attached a patch to fix this by checking second_slot_contents.in_use and
ensuring MyReplicationSlot did not reuse src in the second phase. The
code fix applies back to PG 15.
While reviewing the patch, I think there's still a window after the
revalidation check. The second read of the source slot takes only
src->mutex, just for copying the struct, and nothing protects the
source slot after that, so it can be dropped right after the check
passes. IIUC, for a physical slot copy, the destination slot has no
xmin until copy_xmin is copied into it. If the source is dropped in
between, ReplicationSlotsComputeRequiredXmin() might see no slot
holding that xmin, the horizon might move forward, and VACUUM could
remove tuples that the destination slot's xmin is then supposed to
protect? Let me know if my understanding is wrong. I will try to
reproduce this as well.
thanks
Shveta