pg_rewind: Skip vanished source files during traversal
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
Hi,
While looking at pg_rewind's source directory traversal, I noticed a small
thing in recurse_dir() that I wanted to ask about.
When lstat() fails with ENOENT, the existing comment says that this is OK
when scanning a live source data directory, because the file may have been
removed by the new primary:
File doesn't exist anymore. This is ok, if the new primary
is running and the file was just removed.
But after that branch, the code continues and later checks fst.st_mode to
decide whether the entry is a regular file, directory, or symlink. Since
lstat() failed in that case, is fst still safe to inspect? It seems to me
that the intended behavior might be to skip that directory entry once ENOENT
is accepted.
The attached patch adds a continue in that branch.
I tested this with a small harness around traverse_datadir() and an lstat()
shim that returns ENOENT for one directory entry. Without the patch, the
callback was still invoked for that entry; with the patch, it was skipped.
Does this interpretation make sense, or is there a reason we should continue
processing the entry after ENOENT?
Regards,
Ayush
On 28/05/2026 09:01, Ayush Tiwari wrote:
Hi,
While looking at pg_rewind's source directory traversal, I noticed a small
thing in recurse_dir() that I wanted to ask about.When lstat() fails with ENOENT, the existing comment says that this is OK
when scanning a live source data directory, because the file may have been
removed by the new primary:File doesn't exist anymore. This is ok, if the new primary
is running and the file was just removed.But after that branch, the code continues and later checks fst.st_mode to
decide whether the entry is a regular file, directory, or symlink. Since
lstat() failed in that case, is fst still safe to inspect? It seems to me
that the intended behavior might be to skip that directory entry once ENOENT
is accepted.The attached patch adds a continue in that branch.
I tested this with a small harness around traverse_datadir() and an lstat()
shim that returns ENOENT for one directory entry. Without the patch, the
callback was still invoked for that entry; with the patch, it was skipped.Does this interpretation make sense, or is there a reason we should continue
processing the entry after ENOENT?
So, the full comment is:
/*
* File doesn't exist anymore. This is ok, if the new primary
* is running and the file was just removed. If it was a data
* file, there should be a WAL record of the removal. If it
* was something else, it couldn't have been anyway.
*
* TODO: But complain if we're processing the target dir!
That explanation doesn't make a whole lot of sense to me. We don't
support running pg_rewind on a source directory while the source server
is running. We do support the "connection" mode on a running server, but
that doesn't use this function. (And it's not clear what the "couldn't
have been anyway" means here. Mea culpa, I wrote that comment)
And it would indeed be nice to implement that TODO. But AFAICS we should
just always throw an error here. It's not OK if a file goes missing in
the target dir, and we don't expect it in the source dir either, because
the server shouldn't be running.
- Heikki
Hi.
On Mon, 6 Jul 2026 at 18:49, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
On 28/05/2026 09:01, Ayush Tiwari wrote:
Does this interpretation make sense, or is there a reason we should
continue
processing the entry after ENOENT?
So, the full comment is:
/*
* File doesn't exist anymore. This is ok, if the new primary
* is running and the file was just removed. If it was a data
* file, there should be a WAL record of the removal. If it
* was something else, it couldn't have been anyway.
*
* TODO: But complain if we're processing the target dir!That explanation doesn't make a whole lot of sense to me. We don't
support running pg_rewind on a source directory while the source server
is running. We do support the "connection" mode on a running server, but
that doesn't use this function. (And it's not clear what the "couldn't
have been anyway" means here. Mea culpa, I wrote that comment)
And it would indeed be nice to implement that TODO. But AFAICS we should
just always throw an error here. It's not OK if a file goes missing in
the target dir, and we don't expect it in the source dir either, because
the server shouldn't be running.
Ahh, yes you are right. I'd only been looking at the uninitialized
fst.st_mode read (lstat() fails, but the code still inspects st_mode),
and continue was the minimal way to avoid it.
In supported paths, recurse_dir() only runs on a stopped local source
data directory or the target data directory; the running-source case
goes through libpq_traverse_files(). So a vanished file isn't expected
there, and erroring would be the better fix.
v2 attached: it just pg_fatal()s on lstat() failure, dropping the ENOENT
special case and the TODO.
Regards,
Ayush
Hi Ayush, Heikki,
I tested v2 on a real local-mode rewind -- primary, standby promoted, old
primary rewound from it -- forcing the branch with an LD_PRELOAD shim that
makes lstat() return ENOENT for one chosen name, since the readdir/lstat
race isn't reproducible by hand. Three things worth adding.
1. On master the consequence is worse than an uninitialized read: the entry
can be dropped silently.
The same injection in two places, on master:
a file in the source root
pg_rewind: error: could not open directory ".../vanishme_test":
Not a directory
exit 1
a file in source global/
pg_rewind: Done!
exit 0
The second one is the bad case. That file exists in the source, and a
control run without the injection copies it to the target; with the
injection it is simply absent from the rewound target and pg_rewind reports
success. Which of the two you get depends on whether the garbage st_mode
happens to satisfy S_ISREG/S_ISDIR/S_ISLNK -- in the first run it looked
like a directory, so recurse_dir() descended into a path that isn't there
and reported a problem unrelated to the real one. So this is not only
undefined in theory: one of its outcomes is an incomplete rewind that exits
0.
v2 gives the same message in both places, and exits 1:
pg_rewind: error: could not stat file ".../global/vanishme2": No
such file or directory
2. The premise behind the change is enforced, not just expected.
sanityChecks() already refuses to run unless the target -- and, for
--source-pgdata, the source -- has DB_SHUTDOWNED or DB_SHUTDOWNED_IN_RECOVERY
in its control file, and it runs well before either traversal. A running
source therefore cannot reach recurse_dir() at all, which is a bit stronger
than what the new comment claims, and might be worth pointing at from there.
The one gap is the one that check's own comment already admits -- "This
doesn't guard against someone starting the cluster concurrently" -- and that
is precisely the case where the new pg_fatal() fires. Failing loudly is the
right answer there.
3. The new error cannot leave a half-rewound target.
Both traversals run before anything is written to the target; the first
modification is remove_target() in the apply loop, with createBackupLabel()
after it. Confirmed rather than assumed: with the error injected I compared
the target against a copy taken before the run, and diff -r reports no
difference, so the rewind can just be retried.
make check in src/bin/pg_rewind passes with v2. For what it's worth,
nothing else in pg_rewind reads a struct stat after tolerating a failed
stat: the only other ENOENT is remove_target_file()'s missing_ok, which is
deliberate and doesn't inspect anything afterwards.
I found nothing to change in the code -- as far as I can tell v2 is ready to
commit. Two things for the committer rather than for the patch:
- The commit message explains the bug as "reading an uninitialized struct
stat", which undersells it. The observable outcome in my second run was a
rewind that exits 0 with a source file missing from the target. That seems
worth saying, not least because it bears on whether this is back-patch
material -- I don't have a strong view there, since the race needs
something removing files from a data directory pg_rewind has already
verified is shut down, which is not an everyday event.
- The new comment could point at sanityChecks() for its "never over a running
server" claim, since that is where it is actually enforced.
Regards,
Rui
Hi all,
On Mon, Sep 7, 2026 at 3:35 PM Ayush Tiwari <ayushtiwari.slg01@gmail.com> wrote:
Hi.
On Mon, 6 Jul 2026 at 18:49, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
On 28/05/2026 09:01, Ayush Tiwari wrote:
Does this interpretation make sense, or is there a reason we should continue
processing the entry after ENOENT?So, the full comment is:
/*
* File doesn't exist anymore. This is ok, if the new primary
* is running and the file was just removed. If it was a data
* file, there should be a WAL record of the removal. If it
* was something else, it couldn't have been anyway.
*
* TODO: But complain if we're processing the target dir!That explanation doesn't make a whole lot of sense to me. We don't
support running pg_rewind on a source directory while the source server
is running. We do support the "connection" mode on a running server, but
that doesn't use this function. (And it's not clear what the "couldn't
have been anyway" means here. Mea culpa, I wrote that comment)And it would indeed be nice to implement that TODO. But AFAICS we should
just always throw an error here. It's not OK if a file goes missing in
the target dir, and we don't expect it in the source dir either, because
the server shouldn't be running.Ahh, yes you are right. I'd only been looking at the uninitialized
fst.st_mode read (lstat() fails, but the code still inspects st_mode),
and continue was the minimal way to avoid it.In supported paths, recurse_dir() only runs on a stopped local source
data directory or the target data directory; the running-source case
goes through libpq_traverse_files(). So a vanished file isn't expected
there, and erroring would be the better fix.v2 attached: it just pg_fatal()s on lstat() failure, dropping the ENOENT
special case and the TODO.
Thank you for the updated patch. I reviewed and tested the v2 patch.
The patch changes the handling of lstat() failures in recurse_dir()
and instead of ignoring an ENOENT and continuing the traversal, the
patch now reports the failure as a fatal error. This looks correct to
me since recurse_dir() is used for stopped data directories, while the
running source-server case uses a different traversal path. I also
tested the specific failure scenario by creating a real timeline
divergence between the source and target clusters. I used an
LD_PRELOAD shim to force lstat() to return ENOENT for a selected file
under the source global directory.
With the patch, pg_rewind correctly stopped with:
pg_rewind: error: could not stat file
"/tmp/pg_rewind_test/source/global/vanishme2": No such file or
directory
and exited with status 1
I also verified that the target cluster was not modified after the
failed rewind. The existing target-side table was still present, while
the source-only table was not introduced. Based on the above testing,
the patch behaves as expected and correctly handles an unexpectedly
vanished file during traversal.
The patch looks good to me.
Regards
Solai