pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #16032: pg_basebackup when running on Windows doesn't clean up on failure correctly
4+ messages / 3 participants
[nested] [flat]
* BUG #16032: pg_basebackup when running on Windows doesn't clean up on failure correctly
@ 2019-10-01 16:06 PG Bug reporting form <noreply@postgresql.org>
0 siblings, 1 reply; 4+ messages in thread
From: PG Bug reporting form @ 2019-10-01 16:06 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; +Cc: postgresql@mintsoft.net
The following bug has been logged on the website:
Bug reference: 16032
Logged by: Rob Emery
Email address: postgresql@mintsoft.net
PostgreSQL version: 11.5
Operating system: Windows
Description:
Hello,
When running a pg_basebackup in tar mode on windows against a PG 9.5 box we
are finding that the backup doesn't get deleted successfully when it
fails.
Based on
https://github.com/postgres/postgres/commit/9083353b15c3cf8e7bbac104a81ad42281178cdf#diff-473d306e41...
we would expect it to?
To reproduce start running pg_basebackup like so:
PS C:\Users\developer> & "$PgBinPath\pg_basebackup.exe" --host="PGSERVER"
--pgdata="D:\Backups\rofl" --format=tar --wal-method=fetch
--username="backup_user"
from a psql on the box run:
`SELECT pg_terminate_backend(pid) FROM pg_stat_replication WHERE state =
'backup';`
this kills the backup.
If you run pg_basebackup on a linux box it nicely deletes the base.tar and
the directory, whereas on windows we get the following error:
```
pg_basebackup: could not read COPY data: SSL SYSCALL error: EOF detected
pg_basebackup: removing data directory "D:\Backups\rofl"
could not remove file or directory "D:\Backups\rofl/base.tar": Permission
denied
could not remove file or directory "D:\Backups\rofl": Directory not empty
pg_basebackup: failed to remove data directory
```
Once pg_basebackup has exited, the file lock seems to be gone and we can
delete it ourselves,
it looks like pg_basebackup is contending with itself but only on Windows.
We've reproduced this on 2 different Windows machines so we don't think it's
something like AntiVirus getting in the way or similiar.
Thanks,
Rob
^ permalink raw reply [nested|flat] 4+ messages in thread
* [PATCH] Re: BUG #16032: pg_basebackup when running on Windows doesn't clean up on failure correctly
@ 2019-10-06 19:15 Rob Emery <postgresql@mintsoft.net>
parent: PG Bug reporting form <noreply@postgresql.org>
0 siblings, 1 reply; 4+ messages in thread
From: Rob Emery @ 2019-10-06 19:15 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org
Hello,
I've already posted this to pg_hackers, but I'm not sure if it's more
normal to
post proposed fixes against the bug itself; so see attached if needed.
Thanks,
Rob
On 2019-10-01 17:06, PG Bug reporting form wrote:
> The following bug has been logged on the website:
>
> Bug reference: 16032
> Logged by: Rob Emery
> Email address: postgresql@mintsoft.net
> PostgreSQL version: 11.5
> Operating system: Windows
> Description:
>
> Hello,
>
> When running a pg_basebackup in tar mode on windows against a PG 9.5
> box we
> are finding that the backup doesn't get deleted successfully when it
> fails.
> Based on
> https://github.com/postgres/postgres/commit/9083353b15c3cf8e7bbac104a81ad42281178cdf#diff-473d306e41...
> we would expect it to?
>
> To reproduce start running pg_basebackup like so:
>
> PS C:\Users\developer> & "$PgBinPath\pg_basebackup.exe"
> --host="PGSERVER"
> --pgdata="D:\Backups\rofl" --format=tar --wal-method=fetch
> --username="backup_user"
>
> from a psql on the box run:
> `SELECT pg_terminate_backend(pid) FROM pg_stat_replication WHERE state
> =
> 'backup';`
> this kills the backup.
>
> If you run pg_basebackup on a linux box it nicely deletes the base.tar
> and
> the directory, whereas on windows we get the following error:
> ```
> pg_basebackup: could not read COPY data: SSL SYSCALL error: EOF
> detected
> pg_basebackup: removing data directory "D:\Backups\rofl"
> could not remove file or directory "D:\Backups\rofl/base.tar":
> Permission
> denied
> could not remove file or directory "D:\Backups\rofl": Directory not
> empty
> pg_basebackup: failed to remove data directory
> ```
> Once pg_basebackup has exited, the file lock seems to be gone and we
> can
> delete it ourselves,
> it looks like pg_basebackup is contending with itself but only on
> Windows.
>
> We've reproduced this on 2 different Windows machines so we don't think
> it's
> something like AntiVirus getting in the way or similiar.
>
> Thanks,
> Rob
Attachments:
0001-Fix-bug16032-under-windows-when-the-backup-is-aborte.patch (text/x-diff, 1.5K) download
From 148d525c0d30af35abba1b8c5bbe07e4e72ecfec Mon Sep 17 00:00:00 2001
From: Rob Emery <mintsoft@users.noreply.github.com>
Date: Tue, 1 Oct 2019 23:51:52 +0100
Subject: [PATCH] Fix bug16032; under windows when the backup is aborted or
fails, pg_basebackup is unable to cleanup the backup as the filehandles
haven't been released
---
src/bin/pg_basebackup/pg_basebackup.c | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c
index aa68f59965..b6d68e0256 100644
--- a/src/bin/pg_basebackup/pg_basebackup.c
+++ b/src/bin/pg_basebackup/pg_basebackup.c
@@ -1098,6 +1098,17 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
{
fprintf(stderr, _("%s: could not get COPY data stream: %s"),
progname, PQerrorMessage(conn));
+ if (strcmp(basedir, "-") != 0)
+ {
+ /*
+ * File handles could already be closed so we don't care about the result
+ */
+#ifdef HAVE_LIBZ
+ gzclose(ztarfile);
+#else
+ fclose(tarfile);
+#endif
+ }
disconnect_and_exit(1);
}
@@ -1179,6 +1190,17 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
{
fprintf(stderr, _("%s: could not read COPY data: %s"),
progname, PQerrorMessage(conn));
+ if (strcmp(basedir, "-") != 0)
+ {
+ /*
+ * File handles could already be closed so we don't care about the result
+ */
+#ifdef HAVE_LIBZ
+ gzclose(ztarfile);
+#else
+ fclose(tarfile);
+#endif
+ }
disconnect_and_exit(1);
}
--
2.11.0
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: [PATCH] Re: BUG #16032: pg_basebackup when running on Windows doesn't clean up on failure correctly
@ 2019-10-07 01:21 Michael Paquier <michael@paquier.xyz>
parent: Rob Emery <postgresql@mintsoft.net>
0 siblings, 1 reply; 4+ messages in thread
From: Michael Paquier @ 2019-10-07 01:21 UTC (permalink / raw)
To: Rob Emery <postgresql@mintsoft.net>; +Cc: pgsql-bugs@lists.postgresql.org
On Sun, Oct 06, 2019 at 08:15:10PM +0100, Rob Emery wrote:
> Hello,
>
> I've already posted this to pg_hackers, but I'm not sure if it's
> more normal to post proposed fixes against the bug itself; so see
> attached if needed.
Discussing patches on -bugs threads is perfectly fine :)
Regarding your patch, anything living in the middle of processing
(basically calling disconnect_and_exit() in ~11 and exit() for 12~)
would fail into this category. Wouldn't it make sense here to use
atexit() to ensure that the cleanup always happens? I am not sure
that it is a good idea to hope that anything processing the base
backup COPY chunks will remember to clean up those handles in the
event of an error. I am ready to bet that any future code will forget
to add that so we would keep falling into the same trap.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: [PATCH] Re: BUG #16032: pg_basebackup when running on Windows doesn't clean up on failure correctly
@ 2019-10-07 15:52 Rob Emery <postgresql@mintsoft.net>
parent: Michael Paquier <michael@paquier.xyz>
0 siblings, 0 replies; 4+ messages in thread
From: Rob Emery @ 2019-10-07 15:52 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: pgsql-bugs@lists.postgresql.org
Hiya Michael,
> Regarding your patch, anything living in the middle of processing
> (basically calling disconnect_and_exit() in ~11 and exit() for 12~)
> would fail into this category. Wouldn't it make sense here to use
> atexit() to ensure that the cleanup always happens? I am not sure
> that it is a good idea to hope that anything processing the base
> backup COPY chunks will remember to clean up those handles in the
> event of an error. I am ready to bet that any future code will forget
> to add that so we would keep falling into the same trap.
Yes and no, the problem is that under Windows the file cleanup
occurs before the process itself actually ends, as the streaming of
the backup from the COPY is performed in a thread, not a fork.
So, if we attempt to use atexit to cleanup the filehandles,
pg_basebackup will still be attempting to delete the partial backup
before the file handles are cleaned up by the atexit callback.
I hope that makes sense; I agree with your sentiment that there must
be a better way of structuring this.
-Rob
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2019-10-07 15:52 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-10-01 16:06 BUG #16032: pg_basebackup when running on Windows doesn't clean up on failure correctly PG Bug reporting form <noreply@postgresql.org>
2019-10-06 19:15 ` Rob Emery <postgresql@mintsoft.net>
2019-10-07 01:21 ` Re: [PATCH] Re: BUG #16032: pg_basebackup when running on Windows doesn't clean up on failure correctly Michael Paquier <michael@paquier.xyz>
2019-10-07 15:52 ` Re: [PATCH] Re: BUG #16032: pg_basebackup when running on Windows doesn't clean up on failure correctly Rob Emery <postgresql@mintsoft.net>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox