Skip to content

Commit 51b4885

Browse files
committed
Here's a patch implementing the "thread method" to workaround the bug
with socket calls in signal handlers (APC) on Win32. See details in mail to pgsql-hackers-win32 a couple of minutes ago. Magnus Hagander
1 parent b8fd675 commit 51b4885

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.376 2004/03/23 01:23:48 tgl Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.377 2004/03/24 04:04:51 momjian Exp $
4141
*
4242
* NOTES
4343
*
@@ -2016,6 +2016,36 @@ reaper(SIGNAL_ARGS)
20162016
errno = save_errno;
20172017
}
20182018

2019+
2020+
#ifdef WIN32
2021+
/*
2022+
* On WIN32, we cannot use socket functions inside
2023+
* an APC (signal handler). If we do, select() will return
2024+
* with incorrect return values, causing the postmaster to
2025+
* enter a blocking accept(). We work around this by
2026+
* running it on a separate thread. We still block the main
2027+
* thread until it is done, so we don't scribble over any
2028+
* data from the wrong thread (pgstat functions aqre not
2029+
* thread safe).
2030+
*/
2031+
static DWORD WINAPI win32_pgstat_beterm_thread(LPVOID param)
2032+
{
2033+
pgstat_beterm((int)param);
2034+
return 0;
2035+
}
2036+
2037+
static void win32_pgstat_beterm(int pid) {
2038+
HANDLE beterm_thread = CreateThread(NULL, 64*1024, win32_pgstat_beterm_thread, (LPVOID)pid, 0, NULL);
2039+
if (!beterm_thread)
2040+
ereport(FATAL,
2041+
(errmsg_internal("failed to create beterm sender thread: %i", (int)GetLastError())));
2042+
if (WaitForSingleObject(beterm_thread,INFINITE) != WAIT_OBJECT_0)
2043+
ereport(FATAL,
2044+
(errmsg_internal("failed to wait for beterm sender thread: %i", (int)GetLastError())));
2045+
CloseHandle(beterm_thread);
2046+
}
2047+
#endif
2048+
20192049
/*
20202050
* CleanupProc -- cleanup after terminated backend.
20212051
*
@@ -2069,7 +2099,11 @@ CleanupProc(int pid,
20692099
else if (pid == BgWriterPID)
20702100
BgWriterPID = 0;
20712101
else
2102+
#ifndef WIN32
20722103
pgstat_beterm(pid);
2104+
#else
2105+
win32_pgstat_beterm(pid);
2106+
#endif
20732107

20742108
return;
20752109
}

0 commit comments

Comments
 (0)