Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit894459e

Browse files
committed
On Darwin, detect and report a multithreaded postmaster.
Darwin --enable-nls builds use a substitute setlocale() that may start athread. Buildfarm member orangutan experienced BackendList corruptionon account of different postmaster threads executing signal handlerssimultaneously. Furthermore, a multithreaded postmaster risks undefinedbehavior from sigprocmask() and fork(). Emit LOG messages about theproblem and its workaround. Back-patch to 9.0 (all supported versions).
1 parent6fdba8c commit894459e

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

‎configure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11366,7 +11366,7 @@ fi
1136611366
LIBS_including_readline="$LIBS"
1136711367
LIBS=`echo"$LIBS"| sed -e's/-ledit//g' -e's/-lreadline//g'`
1136811368

11369-
forac_funcin cbrt dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll pstat readlink setproctitle setsid shm_open sigprocmask symlink sync_file_range towlower utime utimes wcstombs wcstombs_l
11369+
forac_funcin cbrt dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll pstatpthread_is_threaded_npreadlink setproctitle setsid shm_open sigprocmask symlink sync_file_range towlower utime utimes wcstombs wcstombs_l
1137011370
do:
1137111371
as_ac_var=`$as_echo"ac_cv_func_$ac_func"|$as_tr_sh`
1137211372
ac_fn_c_check_func"$LINENO""$ac_func""$as_ac_var"

‎configure.in

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

1268-
AC_CHECK_FUNCS([cbrt dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll pstat readlink setproctitle setsid shm_open sigprocmask symlink sync_file_range towlower utime utimes wcstombs wcstombs_l])
1268+
AC_CHECK_FUNCS([cbrt dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll pstatpthread_is_threaded_npreadlink setproctitle setsid shm_open sigprocmask symlink sync_file_range towlower utime utimes wcstombs wcstombs_l])
12691269

12701270
AC_REPLACE_FUNCS(fseeko)
12711271
case $host_os in

‎src/backend/postmaster/postmaster.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@
8787
#include<dns_sd.h>
8888
#endif
8989

90+
#ifdefHAVE_PTHREAD_IS_THREADED_NP
91+
#include<pthread.h>
92+
#endif
93+
9094
#include"access/transam.h"
9195
#include"access/xlog.h"
9296
#include"bootstrap/bootstrap.h"
@@ -1200,6 +1204,24 @@ PostmasterMain(int argc, char *argv[])
12001204
*/
12011205
RemovePgTempFiles();
12021206

1207+
#ifdefHAVE_PTHREAD_IS_THREADED_NP
1208+
1209+
/*
1210+
* On Darwin, libintl replaces setlocale() with a version that calls
1211+
* CFLocaleCopyCurrent() when its second argument is "" and every relevant
1212+
* environment variable is unset or empty. CFLocaleCopyCurrent() makes
1213+
* the process multithreaded. The postmaster calls sigprocmask() and
1214+
* calls fork() without an immediate exec(), both of which have undefined
1215+
* behavior in a multithreaded program. A multithreaded postmaster is the
1216+
* normal case on Windows, which offers neither fork() nor sigprocmask().
1217+
*/
1218+
if (pthread_is_threaded_np()!=0)
1219+
ereport(LOG,
1220+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1221+
errmsg("postmaster became multithreaded during startup"),
1222+
errhint("Set the LC_ALL environment variable to a valid locale.")));
1223+
#endif
1224+
12031225
/*
12041226
* Remember postmaster startup time
12051227
*/
@@ -1657,6 +1679,15 @@ ServerLoop(void)
16571679
last_touch_time=now;
16581680
}
16591681

1682+
#ifdefHAVE_PTHREAD_IS_THREADED_NP
1683+
1684+
/*
1685+
* With assertions enabled, check regularly for appearance of
1686+
* additional threads. All builds check at start and exit.
1687+
*/
1688+
Assert(pthread_is_threaded_np()==0);
1689+
#endif
1690+
16601691
/*
16611692
* If we already sent SIGQUIT to children and they are slow to shut
16621693
* down, it's time to send them SIGKILL. This doesn't happen
@@ -4745,6 +4776,18 @@ SubPostmasterMain(int argc, char *argv[])
47454776
staticvoid
47464777
ExitPostmaster(intstatus)
47474778
{
4779+
#ifdefHAVE_PTHREAD_IS_THREADED_NP
4780+
4781+
/*
4782+
* There is no known cause for a postmaster to become multithreaded after
4783+
* startup. Recheck to account for the possibility of unknown causes.
4784+
*/
4785+
if (pthread_is_threaded_np()!=0)
4786+
ereport(LOG,
4787+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
4788+
errmsg("postmaster became multithreaded")));
4789+
#endif
4790+
47484791
/* should cleanup shared memory and kill all backends */
47494792

47504793
/*

‎src/common/exec.c

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

557557
/* don't set LC_ALL in the backend */
558558
if (strcmp(app,PG_TEXTDOMAIN("postgres"))!=0)
559+
{
559560
setlocale(LC_ALL,"");
560561

562+
/*
563+
* One could make a case for reproducing here PostmasterMain()'s test
564+
* for whether the process is multithreaded. Unlike the postmaster,
565+
* no frontend program calls sigprocmask() or otherwise provides for
566+
* mutual exclusion between signal handlers. While frontends using
567+
* fork(), if multithreaded, are formally exposed to undefined
568+
* behavior, we have not witnessed a concrete bug. Therefore,
569+
* complaining about multithreading here may be mere pedantry.
570+
*/
571+
}
572+
561573
if (find_my_exec(argv0,my_exec_path)<0)
562574
return;
563575

‎src/include/pg_config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@
394394
/* Define to 1 if the PS_STRINGS thing exists. */
395395
#undef HAVE_PS_STRINGS
396396

397+
/* Define to 1 if you have the `pthread_is_threaded_np' function. */
398+
#undef HAVE_PTHREAD_IS_THREADED_NP
399+
397400
/* Define to 1 if you have the <pwd.h> header file. */
398401
#undef HAVE_PWD_H
399402

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp