Source file src/os/signal/signal_test.go

     1  // Copyright 2009 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  //go:build unix
     6  
     7  package signal
     8  
     9  import (
    10  	"bytes"
    11  	"context"
    12  	"errors"
    13  	"flag"
    14  	"fmt"
    15  	"internal/testenv"
    16  	"os"
    17  	"os/exec"
    18  	"runtime"
    19  	"runtime/trace"
    20  	"strconv"
    21  	"strings"
    22  	"sync"
    23  	"syscall"
    24  	"testing"
    25  	"time"
    26  )
    27  
    28  // settleTime is an upper bound on how long we expect signals to take to be
    29  // delivered. Lower values make the test faster, but also flakier — especially
    30  // on heavily loaded systems.
    31  //
    32  // The current value is set based on flakes observed in the Go builders.
    33  var settleTime = 100 * time.Millisecond
    34  
    35  // fatalWaitingTime is an absurdly long time to wait for signals to be
    36  // delivered but, using it, we (hopefully) eliminate test flakes on the
    37  // build servers. See #46736 for discussion.
    38  var fatalWaitingTime = 30 * time.Second
    39  
    40  func init() {
    41  	if testenv.Builder() == "solaris-amd64-oraclerel" {
    42  		// The solaris-amd64-oraclerel builder has been observed to time out in
    43  		// TestNohup even with a 250ms settle time.
    44  		//
    45  		// Use a much longer settle time on that builder to try to suss out whether
    46  		// the test is flaky due to builder slowness (which may mean we need a
    47  		// longer GO_TEST_TIMEOUT_SCALE) or due to a dropped signal (which may
    48  		// instead need a test-skip and upstream bug filed against the Solaris
    49  		// kernel).
    50  		//
    51  		// See https://golang.org/issue/33174.
    52  		settleTime = 5 * time.Second
    53  	} else if runtime.GOOS == "linux" && strings.HasPrefix(runtime.GOARCH, "ppc64") {
    54  		// Older linux kernels seem to have some hiccups delivering the signal
    55  		// in a timely manner on ppc64 and ppc64le. When running on a
    56  		// ppc64le/ubuntu 16.04/linux 4.4 host the time can vary quite
    57  		// substantially even on an idle system. 5 seconds is twice any value
    58  		// observed when running 10000 tests on such a system.
    59  		settleTime = 5 * time.Second
    60  	} else if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
    61  		if scale, err := strconv.Atoi(s); err == nil {
    62  			settleTime *= time.Duration(scale)
    63  		}
    64  	}
    65  }
    66  
    67  func waitSig(t *testing.T, c <-chan os.Signal, sig os.Signal) {
    68  	t.Helper()
    69  	waitSig1(t, c, sig, false)
    70  }
    71  func waitSigAll(t *testing.T, c <-chan os.Signal, sig os.Signal) {
    72  	t.Helper()
    73  	waitSig1(t, c, sig, true)
    74  }
    75  
    76  func waitSig1(t *testing.T, c <-chan os.Signal, sig os.Signal, all bool) {
    77  	t.Helper()
    78  
    79  	// Sleep multiple times to give the kernel more tries to
    80  	// deliver the signal.
    81  	start := time.Now()
    82  	timer := time.NewTimer(settleTime / 10)
    83  	defer timer.Stop()
    84  	// If the caller notified for all signals on c, filter out SIGURG,
    85  	// which is used for runtime preemption and can come at unpredictable times.
    86  	// General user code should filter out all unexpected signals instead of just
    87  	// SIGURG, but since os/signal is tightly coupled to the runtime it seems
    88  	// appropriate to be stricter here.
    89  	for time.Since(start) < fatalWaitingTime {
    90  		select {
    91  		case s := <-c:
    92  			if s == sig {
    93  				return
    94  			}
    95  			if !all || s != syscall.SIGURG {
    96  				t.Fatalf("signal was %v, want %v", s, sig)
    97  			}
    98  		case <-timer.C:
    99  			timer.Reset(settleTime / 10)
   100  		}
   101  	}
   102  	t.Fatalf("timeout after %v waiting for %v", fatalWaitingTime, sig)
   103  }
   104  
   105  // quiesce waits until we can be reasonably confident that all pending signals
   106  // have been delivered by the OS.
   107  func quiesce() {
   108  	// The kernel will deliver a signal as a thread returns
   109  	// from a syscall. If the only active thread is sleeping,
   110  	// and the system is busy, the kernel may not get around
   111  	// to waking up a thread to catch the signal.
   112  	// We try splitting up the sleep to give the kernel
   113  	// many chances to deliver the signal.
   114  	start := time.Now()
   115  	for time.Since(start) < settleTime {
   116  		time.Sleep(settleTime / 10)
   117  	}
   118  }
   119  
   120  // Test that basic signal handling works.
   121  func TestSignal(t *testing.T) {
   122  	// Ask for SIGHUP
   123  	c := make(chan os.Signal, 1)
   124  	Notify(c, syscall.SIGHUP)
   125  	defer Stop(c)
   126  
   127  	// Send this process a SIGHUP
   128  	t.Logf("sighup...")
   129  	syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
   130  	waitSig(t, c, syscall.SIGHUP)
   131  
   132  	// Ask for everything we can get. The buffer size has to be
   133  	// more than 1, since the runtime might send SIGURG signals.
   134  	// Using 10 is arbitrary.
   135  	c1 := make(chan os.Signal, 10)
   136  	Notify(c1)
   137  	// Stop relaying the SIGURG signals. See #49724
   138  	Reset(syscall.SIGURG)
   139  	defer Stop(c1)
   140  
   141  	// Send this process a SIGWINCH
   142  	t.Logf("sigwinch...")
   143  	syscall.Kill(syscall.Getpid(), syscall.SIGWINCH)
   144  	waitSigAll(t, c1, syscall.SIGWINCH)
   145  
   146  	// Send two more SIGHUPs, to make sure that
   147  	// they get delivered on c1 and that not reading
   148  	// from c does not block everything.
   149  	t.Logf("sighup...")
   150  	syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
   151  	waitSigAll(t, c1, syscall.SIGHUP)
   152  	t.Logf("sighup...")
   153  	syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
   154  	waitSigAll(t, c1, syscall.SIGHUP)
   155  
   156  	// The first SIGHUP should be waiting for us on c.
   157  	waitSig(t, c, syscall.SIGHUP)
   158  }
   159  
   160  func TestStress(t *testing.T) {
   161  	dur := 3 * time.Second
   162  	if testing.Short() {
   163  		dur = 100 * time.Millisecond
   164  	}
   165  	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(4))
   166  
   167  	sig := make(chan os.Signal, 1)
   168  	Notify(sig, syscall.SIGUSR1)
   169  
   170  	go func() {
   171  		stop := time.After(dur)
   172  		for {
   173  			select {
   174  			case <-stop:
   175  				// Allow enough time for all signals to be delivered before we stop
   176  				// listening for them.
   177  				quiesce()
   178  				Stop(sig)
   179  				// According to its documentation, “[w]hen Stop returns, it in
   180  				// guaranteed that c will receive no more signals.” So we can safely
   181  				// close sig here: if there is a send-after-close race here, that is a
   182  				// bug in Stop and we would like to detect it.
   183  				close(sig)
   184  				return
   185  
   186  			default:
   187  				syscall.Kill(syscall.Getpid(), syscall.SIGUSR1)
   188  				runtime.Gosched()
   189  			}
   190  		}
   191  	}()
   192  
   193  	for range sig {
   194  		// Receive signals until the sender closes sig.
   195  	}
   196  }
   197  
   198  func testCancel(t *testing.T, ignore bool) {
   199  	// Ask to be notified on c1 when a SIGWINCH is received.
   200  	c1 := make(chan os.Signal, 1)
   201  	Notify(c1, syscall.SIGWINCH)
   202  	defer Stop(c1)
   203  
   204  	// Ask to be notified on c2 when a SIGHUP is received.
   205  	c2 := make(chan os.Signal, 1)
   206  	Notify(c2, syscall.SIGHUP)
   207  	defer Stop(c2)
   208  
   209  	// Send this process a SIGWINCH and wait for notification on c1.
   210  	syscall.Kill(syscall.Getpid(), syscall.SIGWINCH)
   211  	waitSig(t, c1, syscall.SIGWINCH)
   212  
   213  	// Send this process a SIGHUP and wait for notification on c2.
   214  	syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
   215  	waitSig(t, c2, syscall.SIGHUP)
   216  
   217  	// Ignore, or reset the signal handlers for, SIGWINCH and SIGHUP.
   218  	// Either way, this should undo both calls to Notify above.
   219  	if ignore {
   220  		Ignore(syscall.SIGWINCH, syscall.SIGHUP)
   221  		// Don't bother deferring a call to Reset: it is documented to undo Notify,
   222  		// but its documentation says nothing about Ignore, and (as of the time of
   223  		// writing) it empirically does not undo an Ignore.
   224  	} else {
   225  		Reset(syscall.SIGWINCH, syscall.SIGHUP)
   226  	}
   227  
   228  	// Send this process a SIGWINCH. It should be ignored.
   229  	syscall.Kill(syscall.Getpid(), syscall.SIGWINCH)
   230  
   231  	// If ignoring, Send this process a SIGHUP. It should be ignored.
   232  	if ignore {
   233  		syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
   234  	}
   235  
   236  	quiesce()
   237  
   238  	select {
   239  	case s := <-c1:
   240  		t.Errorf("unexpected signal %v", s)
   241  	default:
   242  		// nothing to read - good
   243  	}
   244  
   245  	select {
   246  	case s := <-c2:
   247  		t.Errorf("unexpected signal %v", s)
   248  	default:
   249  		// nothing to read - good
   250  	}
   251  
   252  	// One or both of the signals may have been blocked for this process
   253  	// by the calling process.
   254  	// Discard any queued signals now to avoid interfering with other tests.
   255  	Notify(c1, syscall.SIGWINCH)
   256  	Notify(c2, syscall.SIGHUP)
   257  	quiesce()
   258  }
   259  
   260  // Test that Reset cancels registration for listed signals on all channels.
   261  func TestReset(t *testing.T) {
   262  	testCancel(t, false)
   263  }
   264  
   265  // Test that Ignore cancels registration for listed signals on all channels.
   266  func TestIgnore(t *testing.T) {
   267  	testCancel(t, true)
   268  }
   269  
   270  // Test that Ignored correctly detects changes to the ignored status of a signal.
   271  func TestIgnored(t *testing.T) {
   272  	// Ask to be notified on SIGWINCH.
   273  	c := make(chan os.Signal, 1)
   274  	Notify(c, syscall.SIGWINCH)
   275  
   276  	// If we're being notified, then the signal should not be ignored.
   277  	if Ignored(syscall.SIGWINCH) {
   278  		t.Errorf("expected SIGWINCH to not be ignored.")
   279  	}
   280  	Stop(c)
   281  	Ignore(syscall.SIGWINCH)
   282  
   283  	// We're no longer paying attention to this signal.
   284  	if !Ignored(syscall.SIGWINCH) {
   285  		t.Errorf("expected SIGWINCH to be ignored when explicitly ignoring it.")
   286  	}
   287  
   288  	Reset()
   289  }
   290  
   291  var checkSighupIgnored = flag.Bool("check_sighup_ignored", false, "if true, TestDetectNohup will fail if SIGHUP is not ignored.")
   292  
   293  // Test that Ignored(SIGHUP) correctly detects whether it is being run under nohup.
   294  func TestDetectNohup(t *testing.T) {
   295  	if *checkSighupIgnored {
   296  		if !Ignored(syscall.SIGHUP) {
   297  			t.Fatal("SIGHUP is not ignored.")
   298  		} else {
   299  			t.Log("SIGHUP is ignored.")
   300  		}
   301  	} else {
   302  		defer Reset()
   303  		// Ugly: ask for SIGHUP so that child will not have no-hup set
   304  		// even if test is running under nohup environment.
   305  		// We have no intention of reading from c.
   306  		c := make(chan os.Signal, 1)
   307  		Notify(c, syscall.SIGHUP)
   308  		if out, err := testenv.Command(t, testenv.Executable(t), "-test.run=^TestDetectNohup$", "-check_sighup_ignored").CombinedOutput(); err == nil {
   309  			t.Errorf("ran test with -check_sighup_ignored and it succeeded: expected failure.\nOutput:\n%s", out)
   310  		}
   311  		Stop(c)
   312  
   313  		// Again, this time with nohup, assuming we can find it.
   314  		_, err := os.Stat("/usr/bin/nohup")
   315  		if err != nil {
   316  			t.Skip("cannot find nohup; skipping second half of test")
   317  		}
   318  		Ignore(syscall.SIGHUP)
   319  		os.Remove("nohup.out")
   320  		out, err := testenv.Command(t, "/usr/bin/nohup", testenv.Executable(t), "-test.run=^TestDetectNohup$", "-check_sighup_ignored").CombinedOutput()
   321  
   322  		data, _ := os.ReadFile("nohup.out")
   323  		os.Remove("nohup.out")
   324  		if err != nil {
   325  			// nohup doesn't work on new LUCI darwin builders due to the
   326  			// type of launchd service the test run under. See
   327  			// https://go.dev/issue/63875.
   328  			if runtime.GOOS == "darwin" && strings.Contains(string(out), "nohup: can't detach from console: Inappropriate ioctl for device") {
   329  				t.Skip("Skipping nohup test due to darwin builder limitation. See https://go.dev/issue/63875.")
   330  			}
   331  
   332  			t.Errorf("ran test with -check_sighup_ignored under nohup and it failed: expected success.\nError: %v\nOutput:\n%s%s", err, out, data)
   333  		}
   334  	}
   335  }
   336  
   337  var (
   338  	sendUncaughtSighup = flag.Int("send_uncaught_sighup", 0, "send uncaught SIGHUP during TestStop")
   339  	dieFromSighup      = flag.Bool("die_from_sighup", false, "wait to die from uncaught SIGHUP")
   340  )
   341  
   342  // Test that Stop cancels the channel's registrations.
   343  func TestStop(t *testing.T) {
   344  	sigs := []syscall.Signal{
   345  		syscall.SIGWINCH,
   346  		syscall.SIGHUP,
   347  		syscall.SIGUSR1,
   348  	}
   349  
   350  	for _, sig := range sigs {
   351  		t.Run(fmt.Sprint(sig), func(t *testing.T) {
   352  			// When calling Notify with a specific signal,
   353  			// independent signals should not interfere with each other,
   354  			// and we end up needing to wait for signals to quiesce a lot.
   355  			// Test the three different signals concurrently.
   356  			t.Parallel()
   357  
   358  			// If the signal is not ignored, send the signal before registering a
   359  			// channel to verify the behavior of the default Go handler.
   360  			// If it's SIGWINCH or SIGUSR1 we should not see it.
   361  			// If it's SIGHUP, maybe we'll die. Let the flag tell us what to do.
   362  			mayHaveBlockedSignal := false
   363  			if !Ignored(sig) && (sig != syscall.SIGHUP || *sendUncaughtSighup == 1) {
   364  				syscall.Kill(syscall.Getpid(), sig)
   365  				quiesce()
   366  
   367  				// We don't know whether sig is blocked for this process; see
   368  				// https://golang.org/issue/38165. Assume that it could be.
   369  				mayHaveBlockedSignal = true
   370  			}
   371  
   372  			// Ask for signal
   373  			c := make(chan os.Signal, 1)
   374  			Notify(c, sig)
   375  
   376  			// Send this process the signal again.
   377  			syscall.Kill(syscall.Getpid(), sig)
   378  			waitSig(t, c, sig)
   379  
   380  			if mayHaveBlockedSignal {
   381  				// We may have received a queued initial signal in addition to the one
   382  				// that we sent after Notify. If so, waitSig may have observed that
   383  				// initial signal instead of the second one, and we may need to wait for
   384  				// the second signal to clear. Do that now.
   385  				quiesce()
   386  				select {
   387  				case <-c:
   388  				default:
   389  				}
   390  			}
   391  
   392  			// Stop watching for the signal and send it again.
   393  			// If it's SIGHUP, maybe we'll die. Let the flag tell us what to do.
   394  			Stop(c)
   395  			if sig != syscall.SIGHUP || *sendUncaughtSighup == 2 {
   396  				syscall.Kill(syscall.Getpid(), sig)
   397  				quiesce()
   398  
   399  				select {
   400  				case s := <-c:
   401  					t.Errorf("unexpected signal %v", s)
   402  				default:
   403  					// nothing to read - good
   404  				}
   405  
   406  				// If we're going to receive a signal, it has almost certainly been
   407  				// received by now. However, it may have been blocked for this process —
   408  				// we don't know. Explicitly unblock it and wait for it to clear now.
   409  				Notify(c, sig)
   410  				quiesce()
   411  				Stop(c)
   412  			}
   413  		})
   414  	}
   415  }
   416  
   417  // Test that when run under nohup, an uncaught SIGHUP does not kill the program.
   418  func TestNohup(t *testing.T) {
   419  	// When run without nohup, the test should crash on an uncaught SIGHUP.
   420  	// When run under nohup, the test should ignore uncaught SIGHUPs,
   421  	// because the runtime is not supposed to be listening for them.
   422  	// Either way, TestStop should still be able to catch them when it wants them
   423  	// and then when it stops wanting them, the original behavior should resume.
   424  	//
   425  	// send_uncaught_sighup=1 sends the SIGHUP before starting to listen for SIGHUPs.
   426  	// send_uncaught_sighup=2 sends the SIGHUP after no longer listening for SIGHUPs.
   427  	//
   428  	// Both should fail without nohup and succeed with nohup.
   429  
   430  	t.Run("uncaught", func(t *testing.T) {
   431  		// Ugly: ask for SIGHUP so that child will not have no-hup set
   432  		// even if test is running under nohup environment.
   433  		// We have no intention of reading from c.
   434  		c := make(chan os.Signal, 1)
   435  		Notify(c, syscall.SIGHUP)
   436  		t.Cleanup(func() { Stop(c) })
   437  
   438  		var subTimeout time.Duration
   439  		if deadline, ok := t.Deadline(); ok {
   440  			subTimeout = time.Until(deadline)
   441  			subTimeout -= subTimeout / 10 // Leave 10% headroom for propagating output.
   442  		}
   443  		for i := 1; i <= 2; i++ {
   444  			t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
   445  				t.Parallel()
   446  
   447  				args := []string{
   448  					"-test.v",
   449  					"-test.run=^TestStop$",
   450  					"-send_uncaught_sighup=" + strconv.Itoa(i),
   451  					"-die_from_sighup",
   452  				}
   453  				if subTimeout != 0 {
   454  					args = append(args, fmt.Sprintf("-test.timeout=%v", subTimeout))
   455  				}
   456  				out, err := testenv.Command(t, testenv.Executable(t), args...).CombinedOutput()
   457  
   458  				if err == nil {
   459  					t.Errorf("ran test with -send_uncaught_sighup=%d and it succeeded: expected failure.\nOutput:\n%s", i, out)
   460  				} else {
   461  					t.Logf("test with -send_uncaught_sighup=%d failed as expected.\nError: %v\nOutput:\n%s", i, err, out)
   462  				}
   463  			})
   464  		}
   465  	})
   466  
   467  	t.Run("nohup", func(t *testing.T) {
   468  		// Skip the nohup test below when running in tmux on darwin, since nohup
   469  		// doesn't work correctly there. See issue #5135.
   470  		if runtime.GOOS == "darwin" && os.Getenv("TMUX") != "" {
   471  			t.Skip("Skipping nohup test due to running in tmux on darwin")
   472  		}
   473  
   474  		// Again, this time with nohup, assuming we can find it.
   475  		_, err := exec.LookPath("nohup")
   476  		if err != nil {
   477  			t.Skip("cannot find nohup; skipping second half of test")
   478  		}
   479  
   480  		var subTimeout time.Duration
   481  		if deadline, ok := t.Deadline(); ok {
   482  			subTimeout = time.Until(deadline)
   483  			subTimeout -= subTimeout / 10 // Leave 10% headroom for propagating output.
   484  		}
   485  		for i := 1; i <= 2; i++ {
   486  			t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
   487  				t.Parallel()
   488  
   489  				// POSIX specifies that nohup writes to a file named nohup.out if standard
   490  				// output is a terminal. However, for an exec.Cmd, standard output is
   491  				// not a terminal — so we don't need to read or remove that file (and,
   492  				// indeed, cannot even create it if the current user is unable to write to
   493  				// GOROOT/src, such as when GOROOT is installed and owned by root).
   494  
   495  				args := []string{
   496  					os.Args[0],
   497  					"-test.v",
   498  					"-test.run=^TestStop$",
   499  					"-send_uncaught_sighup=" + strconv.Itoa(i),
   500  				}
   501  				if subTimeout != 0 {
   502  					args = append(args, fmt.Sprintf("-test.timeout=%v", subTimeout))
   503  				}
   504  				out, err := testenv.Command(t, "nohup", args...).CombinedOutput()
   505  
   506  				if err != nil {
   507  					// nohup doesn't work on new LUCI darwin builders due to the
   508  					// type of launchd service the test run under. See
   509  					// https://go.dev/issue/63875.
   510  					if runtime.GOOS == "darwin" && strings.Contains(string(out), "nohup: can't detach from console: Inappropriate ioctl for device") {
   511  						// TODO(go.dev/issue/63799): A false-positive in vet reports a
   512  						// t.Skip here as invalid. Switch back to t.Skip once fixed.
   513  						t.Logf("Skipping nohup test due to darwin builder limitation. See https://go.dev/issue/63875.")
   514  						return
   515  					}
   516  
   517  					t.Errorf("ran test with -send_uncaught_sighup=%d under nohup and it failed: expected success.\nError: %v\nOutput:\n%s", i, err, out)
   518  				} else {
   519  					t.Logf("ran test with -send_uncaught_sighup=%d under nohup.\nOutput:\n%s", i, out)
   520  				}
   521  			})
   522  		}
   523  	})
   524  }
   525  
   526  // Test that SIGCONT works (issue 8953).
   527  func TestSIGCONT(t *testing.T) {
   528  	c := make(chan os.Signal, 1)
   529  	Notify(c, syscall.SIGCONT)
   530  	defer Stop(c)
   531  	syscall.Kill(syscall.Getpid(), syscall.SIGCONT)
   532  	waitSig(t, c, syscall.SIGCONT)
   533  }
   534  
   535  // Test race between stopping and receiving a signal (issue 14571).
   536  func TestAtomicStop(t *testing.T) {
   537  	if os.Getenv("GO_TEST_ATOMIC_STOP") != "" {
   538  		atomicStopTestProgram(t)
   539  		t.Fatal("atomicStopTestProgram returned")
   540  	}
   541  
   542  	testenv.MustHaveExec(t)
   543  
   544  	// Call Notify for SIGINT before starting the child process.
   545  	// That ensures that SIGINT is not ignored for the child.
   546  	// This is necessary because if SIGINT is ignored when a
   547  	// Go program starts, then it remains ignored, and closing
   548  	// the last notification channel for SIGINT will switch it
   549  	// back to being ignored. In that case the assumption of
   550  	// atomicStopTestProgram, that it will either die from SIGINT
   551  	// or have it be reported, breaks down, as there is a third
   552  	// option: SIGINT might be ignored.
   553  	cs := make(chan os.Signal, 1)
   554  	Notify(cs, syscall.SIGINT)
   555  	defer Stop(cs)
   556  
   557  	const execs = 10
   558  	for i := 0; i < execs; i++ {
   559  		timeout := "0"
   560  		if deadline, ok := t.Deadline(); ok {
   561  			timeout = time.Until(deadline).String()
   562  		}
   563  		cmd := testenv.Command(t, testenv.Executable(t), "-test.run=^TestAtomicStop$", "-test.timeout="+timeout)
   564  		cmd.Env = append(os.Environ(), "GO_TEST_ATOMIC_STOP=1")
   565  		out, err := cmd.CombinedOutput()
   566  		if err == nil {
   567  			if len(out) > 0 {
   568  				t.Logf("iteration %d: output %s", i, out)
   569  			}
   570  		} else {
   571  			t.Logf("iteration %d: exit status %q: output: %s", i, err, out)
   572  		}
   573  
   574  		lost := bytes.Contains(out, []byte("lost signal"))
   575  		if lost {
   576  			t.Errorf("iteration %d: lost signal", i)
   577  		}
   578  
   579  		// The program should either die due to SIGINT,
   580  		// or exit with success without printing "lost signal".
   581  		if err == nil {
   582  			if len(out) > 0 && !lost {
   583  				t.Errorf("iteration %d: unexpected output", i)
   584  			}
   585  		} else {
   586  			if ee, ok := err.(*exec.ExitError); !ok {
   587  				t.Errorf("iteration %d: error (%v) has type %T; expected exec.ExitError", i, err, err)
   588  			} else if ws, ok := ee.Sys().(syscall.WaitStatus); !ok {
   589  				t.Errorf("iteration %d: error.Sys (%v) has type %T; expected syscall.WaitStatus", i, ee.Sys(), ee.Sys())
   590  			} else if !ws.Signaled() || ws.Signal() != syscall.SIGINT {
   591  				t.Errorf("iteration %d: got exit status %v; expected SIGINT", i, ee)
   592  			}
   593  		}
   594  	}
   595  }
   596  
   597  // atomicStopTestProgram is run in a subprocess by TestAtomicStop.
   598  // It tries to trigger a signal delivery race. This function should
   599  // either catch a signal or die from it.
   600  func atomicStopTestProgram(t *testing.T) {
   601  	// This test won't work if SIGINT is ignored here.
   602  	if Ignored(syscall.SIGINT) {
   603  		fmt.Println("SIGINT is ignored")
   604  		os.Exit(1)
   605  	}
   606  
   607  	const tries = 10
   608  
   609  	timeout := 2 * time.Second
   610  	if deadline, ok := t.Deadline(); ok {
   611  		// Give each try an equal slice of the deadline, with one slice to spare for
   612  		// cleanup.
   613  		timeout = time.Until(deadline) / (tries + 1)
   614  	}
   615  
   616  	pid := syscall.Getpid()
   617  	printed := false
   618  	for i := 0; i < tries; i++ {
   619  		cs := make(chan os.Signal, 1)
   620  		Notify(cs, syscall.SIGINT)
   621  
   622  		var wg sync.WaitGroup
   623  		wg.Add(1)
   624  		go func() {
   625  			defer wg.Done()
   626  			Stop(cs)
   627  		}()
   628  
   629  		syscall.Kill(pid, syscall.SIGINT)
   630  
   631  		// At this point we should either die from SIGINT or
   632  		// get a notification on cs. If neither happens, we
   633  		// dropped the signal. It is given 2 seconds to
   634  		// deliver, as needed for gccgo on some loaded test systems.
   635  
   636  		select {
   637  		case <-cs:
   638  		case <-time.After(timeout):
   639  			if !printed {
   640  				fmt.Print("lost signal on tries:")
   641  				printed = true
   642  			}
   643  			fmt.Printf(" %d", i)
   644  		}
   645  
   646  		wg.Wait()
   647  	}
   648  	if printed {
   649  		fmt.Print("\n")
   650  	}
   651  
   652  	os.Exit(0)
   653  }
   654  
   655  func TestTime(t *testing.T) {
   656  	// Test that signal works fine when we are in a call to get time,
   657  	// which on some platforms is using VDSO. See issue #34391.
   658  	dur := 3 * time.Second
   659  	if testing.Short() {
   660  		dur = 100 * time.Millisecond
   661  	}
   662  	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(4))
   663  
   664  	sig := make(chan os.Signal, 1)
   665  	Notify(sig, syscall.SIGUSR1)
   666  
   667  	stop := make(chan struct{})
   668  	go func() {
   669  		for {
   670  			select {
   671  			case <-stop:
   672  				// Allow enough time for all signals to be delivered before we stop
   673  				// listening for them.
   674  				quiesce()
   675  				Stop(sig)
   676  				// According to its documentation, “[w]hen Stop returns, it in
   677  				// guaranteed that c will receive no more signals.” So we can safely
   678  				// close sig here: if there is a send-after-close race, that is a bug in
   679  				// Stop and we would like to detect it.
   680  				close(sig)
   681  				return
   682  
   683  			default:
   684  				syscall.Kill(syscall.Getpid(), syscall.SIGUSR1)
   685  				runtime.Gosched()
   686  			}
   687  		}
   688  	}()
   689  
   690  	done := make(chan struct{})
   691  	go func() {
   692  		for range sig {
   693  			// Receive signals until the sender closes sig.
   694  		}
   695  		close(done)
   696  	}()
   697  
   698  	t0 := time.Now()
   699  	for t1 := t0; t1.Sub(t0) < dur; t1 = time.Now() {
   700  	} // hammering on getting time
   701  
   702  	close(stop)
   703  	<-done
   704  }
   705  
   706  var (
   707  	checkNotifyContext = flag.Bool("check_notify_ctx", false, "if true, TestNotifyContext will fail if SIGINT is not received.")
   708  	ctxNotifyTimes     = flag.Int("ctx_notify_times", 1, "number of times a SIGINT signal should be received")
   709  )
   710  
   711  func TestNotifyContextNotifications(t *testing.T) {
   712  	if *checkNotifyContext {
   713  		ctx, _ := NotifyContext(context.Background(), syscall.SIGINT)
   714  		// We want to make sure not to be calling Stop() internally on NotifyContext() when processing a received signal.
   715  		// Being able to wait for a number of received system signals allows us to do so.
   716  		var wg sync.WaitGroup
   717  		n := *ctxNotifyTimes
   718  		wg.Add(n)
   719  		for i := 0; i < n; i++ {
   720  			go func() {
   721  				syscall.Kill(syscall.Getpid(), syscall.SIGINT)
   722  				wg.Done()
   723  			}()
   724  		}
   725  		wg.Wait()
   726  		<-ctx.Done()
   727  		if got, want := context.Cause(ctx).Error(), "interrupt signal received"; got != want {
   728  			t.Errorf("context.Cause(ctx) = %q, want %q", got, want)
   729  		}
   730  		fmt.Println("received SIGINT")
   731  		// Sleep to give time to simultaneous signals to reach the process.
   732  		// These signals must be ignored given stop() is not called on this code.
   733  		// We want to guarantee a SIGINT doesn't cause a premature termination of the program.
   734  		time.Sleep(settleTime)
   735  		return
   736  	}
   737  
   738  	t.Parallel()
   739  	testCases := []struct {
   740  		name string
   741  		n    int // number of times a SIGINT should be notified.
   742  	}{
   743  		{"once", 1},
   744  		{"multiple", 10},
   745  	}
   746  	for _, tc := range testCases {
   747  		t.Run(tc.name, func(t *testing.T) {
   748  			t.Parallel()
   749  
   750  			var subTimeout time.Duration
   751  			if deadline, ok := t.Deadline(); ok {
   752  				timeout := time.Until(deadline)
   753  				if timeout < 2*settleTime {
   754  					t.Fatalf("starting test with less than %v remaining", 2*settleTime)
   755  				}
   756  				subTimeout = timeout - (timeout / 10) // Leave 10% headroom for cleaning up subprocess.
   757  			}
   758  
   759  			args := []string{
   760  				"-test.v",
   761  				"-test.run=^TestNotifyContextNotifications$",
   762  				"-check_notify_ctx",
   763  				fmt.Sprintf("-ctx_notify_times=%d", tc.n),
   764  			}
   765  			if subTimeout != 0 {
   766  				args = append(args, fmt.Sprintf("-test.timeout=%v", subTimeout))
   767  			}
   768  			out, err := testenv.Command(t, testenv.Executable(t), args...).CombinedOutput()
   769  			if err != nil {
   770  				t.Errorf("ran test with -check_notify_ctx_notification and it failed with %v.\nOutput:\n%s", err, out)
   771  			}
   772  			if want := []byte("received SIGINT\n"); !bytes.Contains(out, want) {
   773  				t.Errorf("got %q, wanted %q", out, want)
   774  			}
   775  		})
   776  	}
   777  }
   778  
   779  func TestNotifyContextStop(t *testing.T) {
   780  	Ignore(syscall.SIGHUP)
   781  	if !Ignored(syscall.SIGHUP) {
   782  		t.Errorf("expected SIGHUP to be ignored when explicitly ignoring it.")
   783  	}
   784  
   785  	parent, cancelParent := context.WithCancel(context.Background())
   786  	defer cancelParent()
   787  	c, stop := NotifyContext(parent, syscall.SIGHUP)
   788  	defer stop()
   789  
   790  	// If we're being notified, then the signal should not be ignored.
   791  	if Ignored(syscall.SIGHUP) {
   792  		t.Errorf("expected SIGHUP to not be ignored.")
   793  	}
   794  
   795  	if want, got := "signal.NotifyContext(context.Background.WithCancel, [hangup])", fmt.Sprint(c); want != got {
   796  		t.Errorf("c.String() = %q, wanted %q", got, want)
   797  	}
   798  
   799  	stop()
   800  	<-c.Done()
   801  	if got := c.Err(); got != context.Canceled {
   802  		t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
   803  	}
   804  	if got := context.Cause(c); got != context.Canceled {
   805  		t.Errorf("context.Cause(c.Err()) = %q, want %q", got, context.Canceled)
   806  	}
   807  }
   808  
   809  func TestNotifyContextCancelParent(t *testing.T) {
   810  	parent, cancelParent := context.WithCancelCause(context.Background())
   811  	parentCause := errors.New("parent canceled")
   812  	defer cancelParent(parentCause)
   813  	c, stop := NotifyContext(parent, syscall.SIGINT)
   814  	defer stop()
   815  
   816  	if want, got := "signal.NotifyContext(context.Background.WithCancel, [interrupt])", fmt.Sprint(c); want != got {
   817  		t.Errorf("c.String() = %q, want %q", got, want)
   818  	}
   819  
   820  	cancelParent(parentCause)
   821  	<-c.Done()
   822  	if got := c.Err(); got != context.Canceled {
   823  		t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
   824  	}
   825  	if got := context.Cause(c); got != parentCause {
   826  		t.Errorf("context.Cause(c) = %q, want %q", got, parentCause)
   827  	}
   828  }
   829  
   830  func TestNotifyContextPrematureCancelParent(t *testing.T) {
   831  	parent, cancelParent := context.WithCancelCause(context.Background())
   832  	parentCause := errors.New("parent canceled")
   833  	defer cancelParent(parentCause)
   834  
   835  	cancelParent(parentCause) // Prematurely cancel context before calling NotifyContext.
   836  	c, stop := NotifyContext(parent, syscall.SIGINT)
   837  	defer stop()
   838  
   839  	if want, got := "signal.NotifyContext(context.Background.WithCancel, [interrupt])", fmt.Sprint(c); want != got {
   840  		t.Errorf("c.String() = %q, want %q", got, want)
   841  	}
   842  
   843  	<-c.Done()
   844  	if got := c.Err(); got != context.Canceled {
   845  		t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
   846  	}
   847  	if got := context.Cause(c); got != parentCause {
   848  		t.Errorf("context.Cause(c) = %q, want %q", got, parentCause)
   849  	}
   850  }
   851  
   852  func TestNotifyContextSimultaneousStop(t *testing.T) {
   853  	c, stop := NotifyContext(context.Background(), syscall.SIGINT)
   854  	defer stop()
   855  
   856  	if want, got := "signal.NotifyContext(context.Background, [interrupt])", fmt.Sprint(c); want != got {
   857  		t.Errorf("c.String() = %q, want %q", got, want)
   858  	}
   859  
   860  	var wg sync.WaitGroup
   861  	n := 10
   862  	wg.Add(n)
   863  	for i := 0; i < n; i++ {
   864  		go func() {
   865  			stop()
   866  			wg.Done()
   867  		}()
   868  	}
   869  	wg.Wait()
   870  	<-c.Done()
   871  	if got := c.Err(); got != context.Canceled {
   872  		t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
   873  	}
   874  }
   875  
   876  func TestNotifyContextStringer(t *testing.T) {
   877  	parent, cancelParent := context.WithCancel(context.Background())
   878  	defer cancelParent()
   879  	c, stop := NotifyContext(parent, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
   880  	defer stop()
   881  
   882  	want := `signal.NotifyContext(context.Background.WithCancel, [hangup interrupt terminated])`
   883  	if got := fmt.Sprint(c); got != want {
   884  		t.Errorf("c.String() = %q, want %q", got, want)
   885  	}
   886  }
   887  
   888  // #44193 test signal handling while stopping and starting the world.
   889  func TestSignalTrace(t *testing.T) {
   890  	done := make(chan struct{})
   891  	quit := make(chan struct{})
   892  	c := make(chan os.Signal, 1)
   893  	Notify(c, syscall.SIGHUP)
   894  
   895  	// Source and sink for signals busy loop unsynchronized with
   896  	// trace starts and stops. We are ultimately validating that
   897  	// signals and runtime.(stop|start)TheWorldGC are compatible.
   898  	go func() {
   899  		defer close(done)
   900  		defer Stop(c)
   901  		pid := syscall.Getpid()
   902  		for {
   903  			select {
   904  			case <-quit:
   905  				return
   906  			default:
   907  				syscall.Kill(pid, syscall.SIGHUP)
   908  			}
   909  			waitSig(t, c, syscall.SIGHUP)
   910  		}
   911  	}()
   912  
   913  	for i := 0; i < 100; i++ {
   914  		buf := new(bytes.Buffer)
   915  		if err := trace.Start(buf); err != nil {
   916  			t.Fatalf("[%d] failed to start tracing: %v", i, err)
   917  		}
   918  		trace.Stop()
   919  		size := buf.Len()
   920  		if size == 0 {
   921  			t.Fatalf("[%d] trace is empty", i)
   922  		}
   923  	}
   924  	close(quit)
   925  	<-done
   926  }
   927  

View as plain text