[PATCH] Clear walsender reader pointers after cleanup

Started by Ayush Tiwariabout 1 month ago4 messageshackers
Jump to latest
#1Ayush Tiwari
ayushtiwari.slg01@gmail.com

Hi,

FreeDecodingContext() frees the reader when logical streaming ends, but
xlogreader is left pointing to it. If the next command on that connection
fails, WalSndErrorCleanup() still follows that pointer.

I tried sending CopyDone, draining the replies, then TIMELINE_HISTORY 99.
With --enable-cassert, I get close(0x7F7F7F7F). The connection stays up,
but the attached patch stops that invalid close.

The patch clears the pointers after streaming and error cleanup. For
physical streaming, I've also freed the reader before returning. Is there
a reason to leave that part to cmd_context reset instead?

I've adjusted the assertion too, since it runs after the streaming
function returns. At that point the pointers are now NULL.

Regards,
Ayush

Attachments:

t254142_1
v1-0001-Clear-walsender-reader-pointers-after-cleanup.patchapplication/octet-stream; name=v1-0001-Clear-walsender-reader-pointers-after-cleanup.patchDownload+9-2
#2Michael Paquier
michael@paquier.xyz
In reply to: Ayush Tiwari (#1)
Re: [PATCH] Clear walsender reader pointers after cleanup

On Sun, Oct 11, 2026 at 02:54:27AM -0700, Ayush Tiwari wrote:

FreeDecodingContext() frees the reader when logical streaming ends, but
xlogreader is left pointing to it. If the next command on that connection
fails, WalSndErrorCleanup() still follows that pointer.

You have been able to reach that without patching the backend and only
sending protocol-level messages while maintaining a replication
connection with a database, haven't you? This would be enough to
warrant a backpatch to keep the state of the live connection clean.

The patch clears the pointers after streaming and error cleanup. For
physical streaming, I've also freed the reader before returning. Is there
a reason to leave that part to cmd_context reset instead?

StartReplication() is complicated enough that I'd be OK with just
adding an extra XLogReaderFree() there. It makes plainly clear that
once we are done with the replication bytes, the xlogreader state
allocated at the beginning of StartReplication() stored statically is
not something we should rely on anymore.

I've adjusted the assertion too, since it runs after the streaming
function returns. At that point the pointers are now NULL.

I don't feel strongly about these additions though. What do they
actually buy?
--
Michael

#3Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: Michael Paquier (#2)
Re: [PATCH] Clear walsender reader pointers after cleanup

Hi,

On Tue, 13 Oct 2026 at 04:35, Michael Paquier <michael@paquier.xyz> wrote:

On Sun, Oct 11, 2026 at 02:54:27AM -0700, Ayush Tiwari wrote:

FreeDecodingContext() frees the reader when logical streaming ends, but
xlogreader is left pointing to it. If the next command on that connection
fails, WalSndErrorCleanup() still follows that pointer.

You have been able to reach that without patching the backend and only
sending protocol-level messages while maintaining a replication
connection with a database, haven't you? This would be enough to
warrant a backpatch to keep the state of the live connection clean.

Yes, I had not made any changes in backend.

The patch clears the pointers after streaming and error cleanup. For
physical streaming, I've also freed the reader before returning. Is there
a reason to leave that part to cmd_context reset instead?

StartReplication() is complicated enough that I'd be OK with just
adding an extra XLogReaderFree() there. It makes plainly clear that
once we are done with the replication bytes, the xlogreader state
allocated at the beginning of StartReplication() stored statically is
not something we should rely on anymore.

Sounds good.

I've adjusted the assertion too, since it runs after the streaming
function returns. At that point the pointers are now NULL.

I don't feel strongly about these additions though. What do they
actually buy?

So just by looking at while I was clearing xlogreader, and the assert
in master branch said Assert(xlogreader != NULL);

The new assertion was meant to catch either streaming path leaving the
global pointers set after returning. It isn't needed for the fix, so
I'm fine with dropping it too..

Regards,
Ayush

#4Michael Paquier
michael@paquier.xyz
In reply to: Ayush Tiwari (#3)
Re: [PATCH] Clear walsender reader pointers after cleanup

On Mon, Oct 12, 2026 at 06:30:52PM -0700, Ayush Tiwari wrote:

The new assertion was meant to catch either streaming path leaving the
global pointers set after returning. It isn't needed for the fix, so
I'm fine with dropping it too..

Hmm. I was looking at all that again, and at the end I have moved
the assertion added by 879ad9f90e83 to be checked at the beginning of
the execution of a replication command, extending it to also check
after the logical replication context.

And done as of f2dd2a3bd44e.
--
Michael