Skip to content

Commit 6d7547c

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 91fca4b commit 6d7547c

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
@@ -111,17 +111,14 @@ pgwin32_open(const char *fileName, int fileFlags,...)
111111
{
112112
/*
113113
* Sharing violation or locking error can indicate antivirus, backup
114-
* or similar software that's locking the file. Try again for 30
115-
* seconds before giving up.
114+
* or similar software that's locking the file. Wait a bit and try
115+
* again, giving up after 30 seconds.
116116
*/
117117
DWORD err = GetLastError();
118118

119119
if (err == ERROR_SHARING_VIOLATION ||
120120
err == ERROR_LOCK_VIOLATION)
121121
{
122-
pg_usleep(100000);
123-
loops++;
124-
125122
#ifndef FRONTEND
126123
if (loops == 50)
127124
ereport(LOG,
@@ -132,7 +129,27 @@ pgwin32_open(const char *fileName, int fileFlags,...)
132129
#endif
133130

134131
if (loops < 300)
132+
{
133+
pg_usleep(100000);
134+
loops++;
135+
continue;
136+
}
137+
}
138+
139+
/*
140+
* ERROR_ACCESS_DENIED can be returned if the file is deleted but not
141+
* yet gone (Windows NT status code is STATUS_DELETE_PENDING). Wait a
142+
* bit and try again, giving up after 1 second (since this condition
143+
* should never persist very long).
144+
*/
145+
if (err == ERROR_ACCESS_DENIED)
146+
{
147+
if (loops < 10)
148+
{
149+
pg_usleep(100000);
150+
loops++;
135151
continue;
152+
}
136153
}
137154

138155
_dosmaperr(err);

0 commit comments

Comments
 (0)