Skip to content

Commit 5ca4e44

Browse files
committed
On Darwin, detect and report a multithreaded postmaster.
Darwin --enable-nls builds use a substitute setlocale() that may start a thread. Buildfarm member orangutan experienced BackendList corruption on account of different postmaster threads executing signal handlers simultaneously. Furthermore, a multithreaded postmaster risks undefined behavior from sigprocmask() and fork(). Emit LOG messages about the problem and its workaround. Back-patch to 9.0 (all supported versions).
1 parent 603eb79 commit 5ca4e44

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

configure

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19302,7 +19302,8 @@ LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
1930219302

1930319303

1930419304

19305-
for ac_func in cbrt dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll pstat readlink setproctitle setsid sigprocmask symlink towlower utime utimes waitpid wcstombs wcstombs_l
19305+
19306+
for ac_func in cbrt dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll pstat pthread_is_threaded_np readlink setproctitle setsid sigprocmask symlink towlower utime utimes waitpid wcstombs wcstombs_l
1930619307
do
1930719308
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
1930819309
{ $as_echo "$as_me:$LINENO: checking for $ac_func" >&5

configure.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ PGAC_FUNC_GETTIMEOFDAY_1ARG
12411241
LIBS_including_readline="$LIBS"
12421242
LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
12431243

1244-
AC_CHECK_FUNCS([cbrt dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll pstat readlink setproctitle setsid sigprocmask symlink towlower utime utimes waitpid wcstombs wcstombs_l])
1244+
AC_CHECK_FUNCS([cbrt dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll pstat pthread_is_threaded_np readlink setproctitle setsid sigprocmask symlink towlower utime utimes waitpid wcstombs wcstombs_l])
12451245

12461246
AC_REPLACE_FUNCS(fseeko)
12471247
case $host_os in

src/backend/postmaster/postmaster.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191
#include <dns_sd.h>
9292
#endif
9393

94+
#ifdef HAVE_PTHREAD_IS_THREADED_NP
95+
#include <pthread.h>
96+
#endif
97+
9498
#include "access/transam.h"
9599
#include "access/xlog.h"
96100
#include "bootstrap/bootstrap.h"
@@ -1105,6 +1109,24 @@ PostmasterMain(int argc, char *argv[])
11051109
*/
11061110
RemovePgTempFiles();
11071111

1112+
#ifdef HAVE_PTHREAD_IS_THREADED_NP
1113+
1114+
/*
1115+
* On Darwin, libintl replaces setlocale() with a version that calls
1116+
* CFLocaleCopyCurrent() when its second argument is "" and every relevant
1117+
* environment variable is unset or empty. CFLocaleCopyCurrent() makes
1118+
* the process multithreaded. The postmaster calls sigprocmask() and
1119+
* calls fork() without an immediate exec(), both of which have undefined
1120+
* behavior in a multithreaded program. A multithreaded postmaster is the
1121+
* normal case on Windows, which offers neither fork() nor sigprocmask().
1122+
*/
1123+
if (pthread_is_threaded_np() != 0)
1124+
ereport(LOG,
1125+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1126+
errmsg("postmaster became multithreaded during startup"),
1127+
errhint("Set the LC_ALL environment variable to a valid locale.")));
1128+
#endif
1129+
11081130
/*
11091131
* Remember postmaster startup time
11101132
*/
@@ -1441,6 +1463,15 @@ ServerLoop(void)
14411463
TouchSocketLockFile();
14421464
last_touch_time = now;
14431465
}
1466+
1467+
#ifdef HAVE_PTHREAD_IS_THREADED_NP
1468+
1469+
/*
1470+
* With assertions enabled, check regularly for appearance of
1471+
* additional threads. All builds check at start and exit.
1472+
*/
1473+
Assert(pthread_is_threaded_np() == 0);
1474+
#endif
14441475
}
14451476
}
14461477

@@ -4211,6 +4242,18 @@ SubPostmasterMain(int argc, char *argv[])
42114242
static void
42124243
ExitPostmaster(int status)
42134244
{
4245+
#ifdef HAVE_PTHREAD_IS_THREADED_NP
4246+
4247+
/*
4248+
* There is no known cause for a postmaster to become multithreaded after
4249+
* startup. Recheck to account for the possibility of unknown causes.
4250+
*/
4251+
if (pthread_is_threaded_np() != 0)
4252+
ereport(LOG,
4253+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
4254+
errmsg("postmaster became multithreaded")));
4255+
#endif
4256+
42144257
/* should cleanup shared memory and kill all backends */
42154258

42164259
/*

src/include/pg_config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,9 @@
387387
/* Define if you have POSIX threads libraries and header files. */
388388
#undef HAVE_PTHREAD
389389

390+
/* Define to 1 if you have the `pthread_is_threaded_np' function. */
391+
#undef HAVE_PTHREAD_IS_THREADED_NP
392+
390393
/* Define to 1 if you have the <pwd.h> header file. */
391394
#undef HAVE_PWD_H
392395

src/port/exec.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,20 @@ set_pglocale_pgservice(const char *argv0, const char *app)
571571

572572
/* don't set LC_ALL in the backend */
573573
if (strcmp(app, PG_TEXTDOMAIN("postgres")) != 0)
574+
{
574575
setlocale(LC_ALL, "");
575576

577+
/*
578+
* One could make a case for reproducing here PostmasterMain()'s test
579+
* for whether the process is multithreaded. Unlike the postmaster,
580+
* no frontend program calls sigprocmask() or otherwise provides for
581+
* mutual exclusion between signal handlers. While frontends using
582+
* fork(), if multithreaded, are formally exposed to undefined
583+
* behavior, we have not witnessed a concrete bug. Therefore,
584+
* complaining about multithreading here may be mere pedantry.
585+
*/
586+
}
587+
576588
if (find_my_exec(argv0, my_exec_path) < 0)
577589
return;
578590

0 commit comments

Comments
 (0)