Skip to content

Commit 368c401

Browse files
runtime: block signals in needm before allocating M
Otherwise, if a signal occurs just after we allocated the M, we can deadlock if the signal handler needs to allocate an M itself. Fixes #42207 Change-Id: I76f44547f419e8b1c14cbf49bf602c6e645d8c14 Reviewed-on: https://go-review.googlesource.com/c/go/+/265759 Trust: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 94f3762 commit 368c401

File tree

7 files changed

+127
-17
lines changed

7 files changed

+127
-17
lines changed

src/runtime/crash_unix_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,12 @@ func TestSignalM(t *testing.T) {
358358
t.Fatalf("signal sent to M %d, but received on M %d", want, got)
359359
}
360360
}
361+
362+
// Issue #42207.
363+
func TestNeedmDeadlock(t *testing.T) {
364+
output := runTestProg(t, "testprogcgo", "NeedmDeadlock")
365+
want := "OK\n"
366+
if output != want {
367+
t.Fatalf("want %s, got %s\n", want, output)
368+
}
369+
}

src/runtime/os_js.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func mpreinit(mp *m) {
5959
}
6060

6161
//go:nosplit
62-
func msigsave(mp *m) {
62+
func sigsave(p *sigset) {
6363
}
6464

6565
//go:nosplit

src/runtime/os_plan9.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func mpreinit(mp *m) {
184184
mp.errstr = (*byte)(mallocgc(_ERRMAX, nil, true))
185185
}
186186

187-
func msigsave(mp *m) {
187+
func sigsave(p *sigset) {
188188
}
189189

190190
func msigrestore(sigmask sigset) {

src/runtime/os_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ func mpreinit(mp *m) {
873873
}
874874

875875
//go:nosplit
876-
func msigsave(mp *m) {
876+
func sigsave(p *sigset) {
877877
}
878878

879879
//go:nosplit

src/runtime/proc.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ func schedinit() {
598598
typelinksinit() // uses maps, activeModules
599599
itabsinit() // uses activeModules
600600

601-
msigsave(_g_.m)
601+
sigsave(&_g_.m.sigmask)
602602
initSigmask = _g_.m.sigmask
603603

604604
goargs()
@@ -1707,6 +1707,18 @@ func needm() {
17071707
exit(1)
17081708
}
17091709

1710+
// Save and block signals before getting an M.
1711+
// The signal handler may call needm itself,
1712+
// and we must avoid a deadlock. Also, once g is installed,
1713+
// any incoming signals will try to execute,
1714+
// but we won't have the sigaltstack settings and other data
1715+
// set up appropriately until the end of minit, which will
1716+
// unblock the signals. This is the same dance as when
1717+
// starting a new m to run Go code via newosproc.
1718+
var sigmask sigset
1719+
sigsave(&sigmask)
1720+
sigblock()
1721+
17101722
// Lock extra list, take head, unlock popped list.
17111723
// nilokay=false is safe here because of the invariant above,
17121724
// that the extra list always contains or will soon contain
@@ -1724,14 +1736,8 @@ func needm() {
17241736
extraMCount--
17251737
unlockextra(mp.schedlink.ptr())
17261738

1727-
// Save and block signals before installing g.
1728-
// Once g is installed, any incoming signals will try to execute,
1729-
// but we won't have the sigaltstack settings and other data
1730-
// set up appropriately until the end of minit, which will
1731-
// unblock the signals. This is the same dance as when
1732-
// starting a new m to run Go code via newosproc.
1733-
msigsave(mp)
1734-
sigblock()
1739+
// Store the original signal mask for use by minit.
1740+
mp.sigmask = sigmask
17351741

17361742
// Install g (= m->g0) and set the stack bounds
17371743
// to match the current stack. We don't actually know
@@ -3676,7 +3682,7 @@ func beforefork() {
36763682
// a signal handler before exec if a signal is sent to the process
36773683
// group. See issue #18600.
36783684
gp.m.locks++
3679-
msigsave(gp.m)
3685+
sigsave(&gp.m.sigmask)
36803686
sigblock()
36813687

36823688
// This function is called before fork in syscall package.

src/runtime/signal_unix.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,15 +1031,15 @@ func sigfwdgo(sig uint32, info *siginfo, ctx unsafe.Pointer) bool {
10311031
return true
10321032
}
10331033

1034-
// msigsave saves the current thread's signal mask into mp.sigmask.
1034+
// sigsave saves the current thread's signal mask into *p.
10351035
// This is used to preserve the non-Go signal mask when a non-Go
10361036
// thread calls a Go function.
10371037
// This is nosplit and nowritebarrierrec because it is called by needm
10381038
// which may be called on a non-Go thread with no g available.
10391039
//go:nosplit
10401040
//go:nowritebarrierrec
1041-
func msigsave(mp *m) {
1042-
sigprocmask(_SIG_SETMASK, nil, &mp.sigmask)
1041+
func sigsave(p *sigset) {
1042+
sigprocmask(_SIG_SETMASK, nil, p)
10431043
}
10441044

10451045
// msigrestore sets the current thread's signal mask to sigmask.
@@ -1111,7 +1111,7 @@ func minitSignalStack() {
11111111
// thread's signal mask. When this is called all signals have been
11121112
// blocked for the thread. This starts with m.sigmask, which was set
11131113
// either from initSigmask for a newly created thread or by calling
1114-
// msigsave if this is a non-Go thread calling a Go function. It
1114+
// sigsave if this is a non-Go thread calling a Go function. It
11151115
// removes all essential signals from the mask, thus causing those
11161116
// signals to not be blocked. Then it sets the thread's signal mask.
11171117
// After this is called the thread can receive signals.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !plan9,!windows
6+
7+
package main
8+
9+
// This is for issue #42207.
10+
// During a call to needm we could get a SIGCHLD signal
11+
// which would itself call needm, causing a deadlock.
12+
13+
/*
14+
#include <signal.h>
15+
#include <pthread.h>
16+
#include <sched.h>
17+
#include <unistd.h>
18+
19+
extern void GoNeedM();
20+
21+
#define SIGNALERS 10
22+
23+
static void* needmSignalThread(void* p) {
24+
pthread_t* pt = (pthread_t*)(p);
25+
int i;
26+
27+
for (i = 0; i < 100; i++) {
28+
if (pthread_kill(*pt, SIGCHLD) < 0) {
29+
return NULL;
30+
}
31+
usleep(1);
32+
}
33+
return NULL;
34+
}
35+
36+
// We don't need many calls, as the deadlock is only likely
37+
// to occur the first couple of times that needm is called.
38+
// After that there will likely be an extra M available.
39+
#define CALLS 10
40+
41+
static void* needmCallbackThread(void* p) {
42+
int i;
43+
44+
for (i = 0; i < SIGNALERS; i++) {
45+
sched_yield(); // Help the signal threads get started.
46+
}
47+
for (i = 0; i < CALLS; i++) {
48+
GoNeedM();
49+
}
50+
return NULL;
51+
}
52+
53+
static void runNeedmSignalThread() {
54+
int i;
55+
pthread_t caller;
56+
pthread_t s[SIGNALERS];
57+
58+
pthread_create(&caller, NULL, needmCallbackThread, NULL);
59+
for (i = 0; i < SIGNALERS; i++) {
60+
pthread_create(&s[i], NULL, needmSignalThread, &caller);
61+
}
62+
for (i = 0; i < SIGNALERS; i++) {
63+
pthread_join(s[i], NULL);
64+
}
65+
pthread_join(caller, NULL);
66+
}
67+
*/
68+
import "C"
69+
70+
import (
71+
"fmt"
72+
"os"
73+
"time"
74+
)
75+
76+
func init() {
77+
register("NeedmDeadlock", NeedmDeadlock)
78+
}
79+
80+
//export GoNeedM
81+
func GoNeedM() {
82+
}
83+
84+
func NeedmDeadlock() {
85+
// The failure symptom is that the program hangs because of a
86+
// deadlock in needm, so set an alarm.
87+
go func() {
88+
time.Sleep(5 * time.Second)
89+
fmt.Println("Hung for 5 seconds")
90+
os.Exit(1)
91+
}()
92+
93+
C.runNeedmSignalThread()
94+
fmt.Println("OK")
95+
}

0 commit comments

Comments
 (0)