Skip to content

Commit 120d7e1

Browse files
committed
On win32, loop when opening files if sharing- och lock-violation errors
occur. Hopefully, this will make it possible to recover from broken antivirus and/or backup software that locks our files.
1 parent ef6bac3 commit 120d7e1

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/port/open.c

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
*
77
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
88
*
9-
* $PostgreSQL: pgsql/src/port/open.c,v 1.22 2007/11/30 11:16:43 mha Exp $
9+
* $PostgreSQL: pgsql/src/port/open.c,v 1.23 2007/12/20 20:27:53 mha Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
1313

1414
#ifdef WIN32
1515

16-
#include "c.h"
16+
#ifndef FRONTEND
17+
#include "postgres.h"
18+
#else
19+
#include "postgres_fe.h"
20+
#endif
1721

1822
#include <windows.h>
1923
#include <fcntl.h>
@@ -58,8 +62,9 @@ int
5862
pgwin32_open(const char *fileName, int fileFlags,...)
5963
{
6064
int fd;
61-
HANDLE h;
65+
HANDLE h = INVALID_HANDLE_VALUE;
6266
SECURITY_ATTRIBUTES sa;
67+
int loops = 0;
6368

6469
/* Check that we can handle the request */
6570
assert((fileFlags & ((O_RDONLY | O_WRONLY | O_RDWR) | O_APPEND |
@@ -71,7 +76,7 @@ pgwin32_open(const char *fileName, int fileFlags,...)
7176
sa.bInheritHandle = TRUE;
7277
sa.lpSecurityDescriptor = NULL;
7378

74-
if ((h = CreateFile(fileName,
79+
while ((h = CreateFile(fileName,
7580
/* cannot use O_RDONLY, as it == 0 */
7681
(fileFlags & O_RDWR) ? (GENERIC_WRITE | GENERIC_READ) :
7782
((fileFlags & O_WRONLY) ? GENERIC_WRITE : GENERIC_READ),
@@ -88,7 +93,32 @@ pgwin32_open(const char *fileName, int fileFlags,...)
8893
((fileFlags & O_DSYNC) ? FILE_FLAG_WRITE_THROUGH : 0),
8994
NULL)) == INVALID_HANDLE_VALUE)
9095
{
91-
_dosmaperr(GetLastError());
96+
/*
97+
* Sharing violation or locking error can indicate antivirus, backup
98+
* or similar software that's locking the file. Try again for 30 seconds
99+
* before giving up.
100+
*/
101+
DWORD err = GetLastError();
102+
if (err == ERROR_SHARING_VIOLATION ||
103+
err == ERROR_LOCK_VIOLATION)
104+
{
105+
pg_usleep(100000);
106+
loops++;
107+
108+
#ifndef FRONTEND
109+
if (loops == 50)
110+
ereport(LOG,
111+
(errmsg("could not open file \"%s\": %s", fileName,
112+
(err == ERROR_SHARING_VIOLATION)?_("sharing violation"):_("lock violation")),
113+
errdetail("Continuing to retry for 30 seconds."),
114+
errhint("You may have antivirus, backup or similar software interfering with the database.")));
115+
#endif
116+
117+
if (loops < 300)
118+
continue;
119+
}
120+
121+
_dosmaperr(err);
92122
return -1;
93123
}
94124

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy