Skip to content

Commit c971c48

Browse files
committed
Remove race in main thread stealing due to reading and writing to pipe from same thread
1 parent e12038d commit c971c48

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

erts/emulator/beam/erl_drv_thread.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ erl_drv_thread_join(ErlDrvTid tid, void **respp)
698698

699699
#if defined(__DARWIN__) && defined(USE_THREADS) && defined(ERTS_SMP)
700700
extern int erts_darwin_main_thread_pipe[2];
701+
extern int erts_darwin_main_thread_result_pipe[2];
701702

702703

703704
int
@@ -709,7 +710,7 @@ erl_drv_stolen_main_thread_join(ErlDrvTid tid, void **respp)
709710
x = &dummy;
710711
else
711712
x = respp;
712-
read(erts_darwin_main_thread_pipe[0],x,sizeof(void *));
713+
read(erts_darwin_main_thread_result_pipe[0],x,sizeof(void *));
713714
return 0;
714715
}
715716

erts/emulator/sys/unix/sys.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -2997,10 +2997,12 @@ init_smp_sig_notify(void)
29972997
#ifdef __DARWIN__
29982998

29992999
int erts_darwin_main_thread_pipe[2];
3000+
int erts_darwin_main_thread_result_pipe[2];
30003001

3001-
static void initialize_darwin_main_thread_pipe(void)
3002+
static void initialize_darwin_main_thread_pipes(void)
30023003
{
3003-
if (pipe(erts_darwin_main_thread_pipe) < 0) {
3004+
if (pipe(erts_darwin_main_thread_pipe) < 0 ||
3005+
pipe(erts_darwin_main_thread_result_pipe) < 0) {
30043006
erl_exit(1,"Fatal error initializing Darwin main thread stealing");
30053007
}
30063008
}
@@ -3011,7 +3013,7 @@ erts_sys_main_thread(void)
30113013
{
30123014
erts_thread_disable_fpe();
30133015
#ifdef __DARWIN__
3014-
initialize_darwin_main_thread_pipe();
3016+
initialize_darwin_main_thread_pipes();
30153017
#endif
30163018
/* Become signal receiver thread... */
30173019
#ifdef ERTS_ENABLE_LOCK_CHECK
@@ -3039,7 +3041,7 @@ erts_sys_main_thread(void)
30393041
read(erts_darwin_main_thread_pipe[0],&func,sizeof(void* (*)(void*)));
30403042
read(erts_darwin_main_thread_pipe[0],&arg, sizeof(void*));
30413043
resp = (*func)(arg);
3042-
write(erts_darwin_main_thread_pipe[1],&resp,sizeof(void *));
3044+
write(erts_darwin_main_thread_result_pipe[1],&resp,sizeof(void *));
30433045
}
30443046
#else
30453047
#ifdef DEBUG

0 commit comments

Comments
 (0)