Skip to content

Commit 59892b1

Browse files
committed
On Windows, retry process creation if we fail to reserve shared memory.
We've heard occasional reports of backend launch failing because pgwin32_ReserveSharedMemoryRegion() fails, indicating that something has already used that address space in the child process. It's not very clear what, given that we disable ASLR in Windows builds, but suspicion falls on antivirus products. It'd be better if we didn't have to disable ASLR, anyway. So let's try to ameliorate the problem by retrying the process launch after such a failure, up to 100 times. Patch by me, based on previous work by Amit Kapila and others. This is a longstanding issue, so back-patch to all supported branches. Discussion: https://postgr.es/m/CAA4eK1+R6hSx6t_yvwtx+NRzneVp+MRqXAdGJZChcau8Uij-8g@mail.gmail.com
1 parent 1b2e875 commit 59892b1

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4347,6 +4347,7 @@ internal_forkexec(int argc, char *argv[], Port *port)
43474347
static pid_t
43484348
internal_forkexec(int argc, char *argv[], Port *port)
43494349
{
4350+
int retry_count = 0;
43504351
STARTUPINFO si;
43514352
PROCESS_INFORMATION pi;
43524353
int i;
@@ -4364,6 +4365,9 @@ internal_forkexec(int argc, char *argv[], Port *port)
43644365
Assert(strncmp(argv[1], "--fork", 6) == 0);
43654366
Assert(argv[2] == NULL);
43664367

4368+
/* Resume here if we need to retry */
4369+
retry:
4370+
43674371
/* Set up shared memory for parameter passing */
43684372
ZeroMemory(&sa, sizeof(sa));
43694373
sa.nLength = sizeof(sa);
@@ -4455,22 +4459,26 @@ internal_forkexec(int argc, char *argv[], Port *port)
44554459

44564460
/*
44574461
* Reserve the memory region used by our main shared memory segment before
4458-
* we resume the child process.
4462+
* we resume the child process. Normally this should succeed, but if ASLR
4463+
* is active then it might sometimes fail due to the stack or heap having
4464+
* gotten mapped into that range. In that case, just terminate the
4465+
* process and retry.
44594466
*/
44604467
if (!pgwin32_ReserveSharedMemoryRegion(pi.hProcess))
44614468
{
4462-
/*
4463-
* Failed to reserve the memory, so terminate the newly created
4464-
* process and give up.
4465-
*/
4469+
/* pgwin32_ReserveSharedMemoryRegion already made a log entry */
44664470
if (!TerminateProcess(pi.hProcess, 255))
44674471
ereport(LOG,
44684472
(errmsg_internal("could not terminate process that failed to reserve memory: error code %lu",
44694473
GetLastError())));
44704474
CloseHandle(pi.hProcess);
44714475
CloseHandle(pi.hThread);
4472-
return -1; /* logging done made by
4473-
* pgwin32_ReserveSharedMemoryRegion() */
4476+
if (++retry_count < 100)
4477+
goto retry;
4478+
ereport(LOG,
4479+
(errmsg("giving up after too many tries to reserve shared memory"),
4480+
errhint("This might be caused by ASLR or antivirus software.")));
4481+
return -1;
44744482
}
44754483

44764484
/*

0 commit comments

Comments
 (0)