Ensure temporary files are unlinked on errors in pg_receivewal

Started by Chao Li6 days ago5 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1Chao Li
li.evan.chao@gmail.com

Hi,

This derives from a previous patch [1]/messages/by-id/CAHGQGwHfCrEogr6=wr0nCkQevAJ9A3HBqQRjT9iJryO7EjF6Lw@mail.gmail.com that fixed a resource leak issue in dir_close() for pg_receivewal. While reviewing the patch, I found a path where, if an LZ4 compression operation fails, the current code goes directly to error handling, bypassing the CLOSE_UNLINK branch, which may cause a temporary file leak. I raised the issue as a review comment, then Fujii-san considered it a separate issue that would be better fixed by a new patch.

This patch moves the CLOSE_UNLINK handling into the common cleanup path, so a preceding operation failure (such as LZ4 compressing or file closing) won't prevent unlinking the temporary file. If unlinking also fails, we preserve the earlier error rather than reporting the unlink failure.

See the attached patch for the detailed changes.

[1]: /messages/by-id/CAHGQGwHfCrEogr6=wr0nCkQevAJ9A3HBqQRjT9iJryO7EjF6Lw@mail.gmail.com

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

t259649_1
v1-0001-Ensure-CLOSE_UNLINK-removes-files-even-after-earl.patchapplication/octet-stream; name=v1-0001-Ensure-CLOSE_UNLINK-removes-files-even-after-earl.patch; x-unix-mode=0644Download+17-14
#2Xuneng Zhou
xunengzhou@gmail.com
In reply to: Chao Li (#1)
Re: Ensure temporary files are unlinked on errors in pg_receivewal

Hi Evan,

Thanks for the raising this.

On Mon, Nov 23, 2026 at 2:56 PM Chao Li <li.evan.chao@gmail.com> wrote:

Hi,

This derives from a previous patch [1] that fixed a resource leak issue in
dir_close() for pg_receivewal. While reviewing the patch, I found a path
where, if an LZ4 compression operation fails, the current code goes
directly to error handling, bypassing the CLOSE_UNLINK branch, which may
cause a temporary file leak. I raised the issue as a review comment, then
Fujii-san considered it a separate issue that would be better fixed by a
new patch.

It seems worth a small patch.

This patch moves the CLOSE_UNLINK handling into the common cleanup path,
so a preceding operation failure (such as LZ4 compressing or file closing)
won't prevent unlinking the temporary file. If unlinking also fails, we
preserve the earlier error rather than reporting the unlink failure.

Looks sensible to me.

See the attached patch for the detailed changes.

[1]
/messages/by-id/CAHGQGwHfCrEogr6=wr0nCkQevAJ9A3HBqQRjT9iJryO7EjF6Lw@mail.gmail.com

Just a stylish comment:

+ int unlink_result = unlink(df->fullpath);

We could save the variable assignment here if you want. Otherwise, LGTM.

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.

#3Fujii Masao
masao.fujii@gmail.com
In reply to: Xuneng Zhou (#2)
Re: Ensure temporary files are unlinked on errors in pg_receivewal

On Mon, Nov 23, 2026 at 6:13 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:

This patch moves the CLOSE_UNLINK handling into the common cleanup path

Thanks for the patch!

+ int unlink_result = unlink(df->fullpath);

We could save the variable assignment here if you want. Otherwise, LGTM.

+1. Attached is v2 with that change, along with a backpatch version for v15.

Barring any objections, I will commit the patch.

Regards,

--
Fujii Masao

Attachments:

t259649_3
v2-0001-pg_receivewal-pg_basebackup-Fix-temporary-file-cl.patchapplication/octet-stream; name=v2-0001-pg_receivewal-pg_basebackup-Fix-temporary-file-cl.patchDownload+14-14
nocfbot-v2-0001-pg_receivewal-pg_basebackup-Fix-temporary-file-cl-v15.patchapplication/octet-stream; name=nocfbot-v2-0001-pg_receivewal-pg_basebackup-Fix-temporary-file-cl-v15.patchDownload+14-13
#4Chao Li
li.evan.chao@gmail.com
In reply to: Fujii Masao (#3)
Re: Ensure temporary files are unlinked on errors in pg_receivewal

On Nov 24, 2026, at 22:39, Fujii Masao <masao.fujii@gmail.com> wrote:

On Mon, Nov 23, 2026 at 6:13 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:

This patch moves the CLOSE_UNLINK handling into the common cleanup path

Thanks for the patch!

+ int unlink_result = unlink(df->fullpath);

We could save the variable assignment here if you want. Otherwise, LGTM.

+1. Attached is v2 with that change, along with a backpatch version for v15.

Barring any objections, I will commit the patch.

Regards,

--
Fujii Masao
<v2-0001-pg_receivewal-pg_basebackup-Fix-temporary-file-cl.patch><nocfbot-v2-0001-pg_receivewal-pg_basebackup-Fix-temporary-file-cl-v15.patch>

Both v2 and the back-patch version look good to me.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#5Fujii Masao
masao.fujii@gmail.com
In reply to: Chao Li (#4)
Re: Ensure temporary files are unlinked on errors in pg_receivewal

On Wed, Nov 25, 2026 at 8:09 AM Chao Li <li.evan.chao@gmail.com> wrote:

Both v2 and the back-patch version look good to me.

I've pushed the patch. Thanks!

Regards,

--
Fujii Masao