Skip to content

Commit 81b052c

Browse files
committed
On Windows, wait a little to see if ERROR_ACCESS_DENIED goes away.
Attempting to open a file fails with ERROR_ACCESS_DENIED if the file is flagged for deletion but not yet actually gone (another in a long list of reasons why Windows is broken, if you ask me). This seems likely to explain a lot of irreproducible failures we see in the buildfarm. This state generally persists for only a millisecond or so, so just wait a bit and retry. If it's a real permissions problem, we'll eventually give up and report it as such. If it's the pending deletion case, we'll see file-not-found and report that after the deletion completes, and the caller will treat that in an appropriate way. In passing, rejigger the existing retry logic for some other error cases so that we don't uselessly wait an extra time when we're not going to retry anymore. Alexander Lakhin (with cosmetic tweaks by me). Back-patch to all supported branches, since this seems like a pretty safe change and the problem is definitely real. Discussion: https://postgr.es/m/16161-7a985d2f1bbe8f71@postgresql.org
1 parent c965c42 commit 81b052c

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/port/open.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,14 @@ pgwin32_open(const char *fileName, int fileFlags,...)
9494
{
9595
/*
9696
* Sharing violation or locking error can indicate antivirus, backup
97-
* or similar software that's locking the file. Try again for 30
98-
* seconds before giving up.
97+
* or similar software that's locking the file. Wait a bit and try
98+
* again, giving up after 30 seconds.
9999
*/
100100
DWORD err = GetLastError();
101101

102102
if (err == ERROR_SHARING_VIOLATION ||
103103
err == ERROR_LOCK_VIOLATION)
104104
{
105-
pg_usleep(100000);
106-
loops++;
107-
108105
#ifndef FRONTEND
109106
if (loops == 50)
110107
ereport(LOG,
@@ -115,7 +112,27 @@ pgwin32_open(const char *fileName, int fileFlags,...)
115112
#endif
116113

117114
if (loops < 300)
115+
{
116+
pg_usleep(100000);
117+
loops++;
118+
continue;
119+
}
120+
}
121+
122+
/*
123+
* ERROR_ACCESS_DENIED can be returned if the file is deleted but not
124+
* yet gone (Windows NT status code is STATUS_DELETE_PENDING). Wait a
125+
* bit and try again, giving up after 1 second (since this condition
126+
* should never persist very long).
127+
*/
128+
if (err == ERROR_ACCESS_DENIED)
129+
{
130+
if (loops < 10)
131+
{
132+
pg_usleep(100000);
133+
loops++;
118134
continue;
135+
}
119136
}
120137

121138
_dosmaperr(err);

0 commit comments

Comments
 (0)