fix_open_for_unlink.patch

text/x-patch

Filename: fix_open_for_unlink.patch
Type: text/x-patch
Part: 1
Message: Re: BUG #16161: pg_ctl stop fails sometimes (on Windows)

Patch

Format: unified
File+
src/port/open.c 15 0
diff --git a/src/port/open.c b/src/port/open.c
index f37afc7512..dad328f000 100644
--- a/src/port/open.c
+++ b/src/port/open.c
@@ -135,6 +135,21 @@ pgwin32_open(const char *fileName, int fileFlags,...)
 				continue;
 		}
 
+		/*
+		 * ERROR_ACCESS_DENIED can be returned while the file is deleted
+		 * (Windows NT status code is STATUS_DELETE_PENDING).
+		 * https://stackoverflow.com/questions/3764072/
+		 * Try again for 1 second to check whether the "access denied"
+		 * condition persists.
+		 */
+		if (err == ERROR_ACCESS_DENIED) {
+			if (loops < 10) {
+				pg_usleep(100000);
+				loops++;
+				continue;
+			}
+		}
+
 		_dosmaperr(err);
 		return -1;
 	}