Skip to content

[#1470] Fix incremental backups on macOS - #1472

Merged
jesperpedersen merged 1 commit into
pgmoneta:mainfrom
lookslikeameya:split-file-path-dirname
Nov 11, 2026
Merged

jesperpedersen merged 1 commit into
pgmoneta:mainfrom
lookslikeameya:split-file-path-dirname

Conversation

@lookslikeameya

@lookslikeameya lookslikeameya commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1470.

Incremental backups on macOS fail in three places that glibc hides:

  • split_file_path() handed back the result of dirname(), which the caller frees. macOS's dirname() returns its own reused buffer, so the second incremental file was a double free. It now returns a copy and frees path_copy.

  • The hot standby rebuild runs stream_restore_file() on worker threads, which had a 1 MB buffer on the stack, plus another 1 MB in restore_stream_cb(). macOS gives secondary threads 512 KB. The read buffer is now allocated per file, and the stream callbacks use an output buffer in the heap-allocated struct streamer. Since that makes the streamer 2 MB, pgmoneta_streamer_create() now checks its malloc() (pointed out in review).

  • stream_restore_file(), object_download_one_file() and copy_file() call dirname() on worker threads, and on macOS all threads share its buffer, so workers got each other's parent directory. They now cut the path at the last / themselves, as Fix empty incremental-backup filename on non-glibc dirname() #1352 did. (copy_file() was pointed out in review.)
    Tested:

  • macOS 26.5.2 (arm64), PG17, test/check.sh: the 11 tests (test_pgmoneta_backup_incremental_basic, test_pgmoneta_hot_standby_incremental, test_pgmoneta_restore_incremental_chain, test_pgmoneta_delete_chain_{root,middle,last}, test_pgmoneta_tablespace_{backup,restore}_incremental, test_not_double_compression_{incremental_backup,wal_files}, test_cli_backup_verify_restore_aes_chain) that fail on main, now pass.
    The remaining macOS failures are the same on main, unrelated to this change.

  • Rocky Linux 10 (aarch64, glibc 2.39) container set up like the CI Linux job, clang Debug, PG17, test/check.sh ci: 287/287 pass.

@lookslikeameya

Copy link
Copy Markdown
Contributor Author

The async tests added in a010f15 hit the same crash on macOS: 5 of 9 fail on main, with pgmoneta killed by SIGTRAP in pgmoneta_backup_size(). With this PR applied on top of current main, test/check.sh -m async passes 9/9, and the PR still applies cleanly.

@Pranav-error Pranav-error left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @lookslikeameya, nice debugging. I checked the causes on an M-series Mac (macOS 26.7.1): the default stack size of a secondary thread is 512 KB (pthread_attr_getstacksize()), and BUFFER_SIZE is 1 MB, so those stack buffers can't fit on a worker thread. dirname() also returned the same static buffer in two threads and in the main thread, so concurrent callers do overwrite each other. The branch builds without warnings on macOS, and split_file_path() looks right: rel_path is now always heap memory the caller owns.

One more place has the same pattern on a worker thread: copy_file() in utils.c (pgmoneta_copy_string(dirname(fi->to), &dn), around line 2867) runs on the workers when pgmoneta_copy_file() is given a pool, as restore does. On macOS another thread can replace the buffer between dirname() and the copy, so dn could be another file's parent directory. The other remaining dirname() calls are single-threaded or Linux-only (the O_DIRECT check). Would you like to fold it into this PR?

@lookslikeameya
lookslikeameya force-pushed the split-file-path-dirname branch from 9749d98 to 195a72b Compare October 27, 2026 12:01
@lookslikeameya

lookslikeameya commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor Author

@Pranav-error
Thanks for testing it on your Mac and for spotting copy_file(). Yes, it's the same race. I've folded it into the commit: it now cuts the path at the last '/' like the other two places. On Linux (CI-like, ASan) it's 287/287, and the incremental, restore and hot standby tests pass on macOS.

@Pranav-error

Copy link
Copy Markdown
Collaborator

Thanks @lookslikeameya. I re-tested on macOS: the build has 0 warnings, and copy_file() now cuts the path at the last / like the other two places. The two remaining dirname() calls are Linux-only or single-threaded, so they're fine. LGTM.

@Pranav-error Pranav-error left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review from the macOS-portability angle (read-through, I didn't build locally). Clean fix for all three #1470 failures — thanks.

  • split_file_path(): copying dirname()'s result into a fresh buffer (pgmoneta_append) and freeing path_copy fixes both the double free and the path_copy leak. Right fix.
  • Replacing dirname() with strrchr()+truncate in stream_restore_file() and object_download_one_file() is the correct thread-safe approach — macOS's dirname() shares one buffer across threads, which was the "failed to create parent directory" failure. Those are the worker-thread paths, so that's the right place.
  • The 1 MB buffers: the per-file malloc for the read buffer, plus moving the stream-callback buffer into struct streamer's new output[] field, both get it off the thread stack. I checked that struct streamer is heap-allocated (stream.c:54, malloc + memset), so the new 1 MB field lands on the heap and doesn't just move the 512 KB-stack problem into the struct — good.

Two minor, non-blocking notes:

  1. struct streamer grew by BUFFER_SIZE (1 MB). The malloc at stream.c:54 is memset'd immediately with no NULL check (pre-existing), so a failed — now 1 MB larger — allocation would segfault in the memset rather than erroring out. A guard there would be nice while the struct is growing.

  2. The strrchr()+truncate parent logic differs slightly from dirname() on edge inputs (a path whose only / is at index 0 skips the mkdir, which is fine since the parent is root; a trailing-slash path would truncate one level short). dst is always a real file path here so neither bites — just noting it in case these helpers get reused.

LGTM.

@lookslikeameya
lookslikeameya force-pushed the split-file-path-dirname branch from 195a72b to 8b770ca Compare November 7, 2026 23:03
@lookslikeameya

Copy link
Copy Markdown
Contributor Author

Thanks @Pranav-error. I've added a NULL check after the streamer malloc, since this PR doubles its size; with that malloc forced to fail, pgmoneta_streamer_create() now returns an error where it used to segfault. On the strrchr() point: agreed, the destinations are always file paths here, so I've left it as is.

@jesperpedersen

Copy link
Copy Markdown
Member

Please, rebase

@lookslikeameya
lookslikeameya force-pushed the split-file-path-dirname branch from 8b770ca to 20f693a Compare November 11, 2026 22:40
@jesperpedersen
jesperpedersen merged commit 20f693a into pgmoneta:main Nov 11, 2026
9 of 10 checks passed
@jesperpedersen

Copy link
Copy Markdown
Member

Merged.

Thanks for your contribution !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incremental backups fail on macOS: dirname() misuse and 1 MB stack buffers on worker threads

3 participants