postgres.git / summary / log / commit / refs

commit    f753bbf57809fc969efd17b3fd6f68e1da3ff979
Author:   Fujii Masao <fujii@postgresql.org>
Date:     Fri Nov 20 11:30:28 2026 +0000

    pg_receivewal: Fix resource leaks and error reporting during cleanup
    
    Failures while finalizing LZ4 compression or writing the final compressed
    data could leak a file descriptor and memory. Without --no-loop,
    repeated failures across reconnects could accumulate these resources.
    
    This happened because dir_close() returned immediately on these failures,
    skipping the code that closes the file and releases its associated
    resources. Fix this by closing the file and executing the common cleanup
    code before returning.
    
    Backpatch to all supported versions.
    
    Author: Fujii Masao <masao.fujii@gmail.com>
    Reviewed-by: Chao Li <li.evan.chao@gmail.com>
    Reviewed-by: Michael Paquier <michael@paquier.xyz>
    Reviewed-by: Xuneng Zhou <xunengzhou@gmail.com>
    Discussion: https://postgr.es/m/CAHGQGwHfCrEogr6=wr0nCkQevAJ9A3HBqQRjT9iJryO7EjF6Lw@mail.gmail.com
    Backpatch-through: 15


src/bin/pg_basebackup/walmethods.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/bin/pg_basebackup/walmethods.c b/src/bin/pg_basebackup/walmethods.c index 6204ba8aba7..41707e42989 100644 --- a/src/bin/pg_basebackup/walmethods.c +++ b/src/bin/pg_basebackup/walmethods.c @@ -383,7 +383,7 @@ dir_get_current_pos(Walfile f) static int dir_close(Walfile f, WalCloseMethod method) { - int r; + int r = -1; DirectoryMethodFile *df = (DirectoryMethodFile *) f; char tmppath[MAXPGPATH]; char tmppath2[MAXPGPATH]; @@ -411,7 +411,8 @@ dir_close(Walfile f, WalCloseMethod method) if (LZ4F_isError(compressed)) { dir_data->lasterrstring = LZ4F_getErrorName(compressed); - return -1; + (void) close(df->fd); + goto cleanup; } errno = 0; @@ -419,7 +420,8 @@ dir_close(Walfile f, WalCloseMethod method) { /* If write didn't set errno, assume problem is no disk space */ dir_data->lasterrno = errno ? errno : ENOSPC; - return -1; + (void) close(df->fd); + goto cleanup; } r = close(df->fd); @@ -493,6 +495,7 @@ dir_close(Walfile f, WalCloseMethod method) dir_data->lasterrno = errno; #ifdef USE_LZ4 +cleanup: pg_free(df->lz4buf); /* supports free on NULL */ LZ4F_freeCompressionContext(df->ctx); [parent: 4f45092f1317]