Repository navigation
[#1470] Fix incremental backups on macOS - #1472
Conversation
|
The |
Pranav-error
left a comment
There was a problem hiding this comment.
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?
9749d98 to
195a72b
Compare
|
@Pranav-error |
|
Thanks @lookslikeameya. I re-tested on macOS: the build has 0 warnings, and |
Pranav-error
left a comment
There was a problem hiding this comment.
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(): copyingdirname()'s result into a fresh buffer (pgmoneta_append) and freeingpath_copyfixes both the double free and thepath_copyleak. Right fix.- Replacing
dirname()withstrrchr()+truncate instream_restore_file()andobject_download_one_file()is the correct thread-safe approach — macOS'sdirname()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
mallocfor the read buffer, plus moving the stream-callback buffer intostruct streamer's newoutput[]field, both get it off the thread stack. I checked thatstruct streameris 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:
-
struct streamergrew byBUFFER_SIZE(1 MB). Themallocatstream.c:54ismemset'd immediately with no NULL check (pre-existing), so a failed — now 1 MB larger — allocation would segfault in thememsetrather than erroring out. A guard there would be nice while the struct is growing. -
The
strrchr()+truncate parent logic differs slightly fromdirname()on edge inputs (a path whose only/is at index 0 skips themkdir, which is fine since the parent is root; a trailing-slash path would truncate one level short).dstis always a real file path here so neither bites — just noting it in case these helpers get reused.
LGTM.
195a72b to
8b770ca
Compare
|
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. |
|
Please, rebase |
8b770ca to
20f693a
Compare
|
Merged. Thanks for your contribution ! |
Fixes #1470.
Incremental backups on macOS fail in three places that glibc hides:
split_file_path()handed back the result ofdirname(), which the caller frees. macOS'sdirname()returns its own reused buffer, so the second incremental file was a double free. It now returns a copy and freespath_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 inrestore_stream_cb(). macOS gives secondary threads 512 KB. The read buffer is now allocated per file, and the stream callbacks use anoutputbuffer in the heap-allocatedstruct streamer. Since that makes the streamer 2 MB,pgmoneta_streamer_create()now checks itsmalloc()(pointed out in review).stream_restore_file(),object_download_one_file()andcopy_file()calldirname()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 onmain, 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.