From e79b57d6c40a25393b2d831b244b19548e23b8a4 Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Mon, 11 Mar 2019 10:52:00 -0700 Subject: os/signal: lazily start signal watch loop only on Notify By lazily starting the signal watch loop only on Notify, we are able to have deadlock detection even when "os/signal" is imported. Thanks to Ian Lance Taylor for the solution and discussion. With this change in, fix a runtime gorountine count test that assumed that os/signal.init would unconditionally start the signal watching goroutine, but alas no more. Fixes #21576. Change-Id: I6eecf82a887f59f2ec8897f1bcd67ca311ca42ff Reviewed-on: https://go-review.googlesource.com/c/go/+/101036 Run-TryBot: Emmanuel Odeke TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/os/signal/signal.go | 15 +++++++++++++++ src/os/signal/signal_plan9.go | 3 ++- src/os/signal/signal_unix.go | 3 ++- 3 files changed, 19 insertions(+), 2 deletions(-) (limited to 'src/os') diff --git a/src/os/signal/signal.go b/src/os/signal/signal.go index a0eba0d50f..136dd9cc97 100644 --- a/src/os/signal/signal.go +++ b/src/os/signal/signal.go @@ -92,6 +92,15 @@ func Ignored(sig os.Signal) bool { return sn >= 0 && signalIgnored(sn) } +var ( + // watchSignalLoopOnce guards calling the conditionally + // initialized watchSignalLoop. If watchSignalLoop is non-nil, + // it will be run in a goroutine lazily once Notify is invoked. + // See Issue 21576. + watchSignalLoopOnce sync.Once + watchSignalLoop func() +) + // Notify causes package signal to relay incoming signals to c. // If no signals are provided, all incoming signals will be relayed to c. // Otherwise, just the provided signals will. @@ -113,6 +122,12 @@ func Notify(c chan<- os.Signal, sig ...os.Signal) { panic("os/signal: Notify using nil channel") } + watchSignalLoopOnce.Do(func() { + if watchSignalLoop != nil { + go watchSignalLoop() + } + }) + handlers.Lock() defer handlers.Unlock() diff --git a/src/os/signal/signal_plan9.go b/src/os/signal/signal_plan9.go index a1eb68855e..8408607c7f 100644 --- a/src/os/signal/signal_plan9.go +++ b/src/os/signal/signal_plan9.go @@ -20,7 +20,8 @@ func signal_recv() string func init() { signal_enable(0) // first call - initialize - go loop() + + watchSignalLoop = loop } func loop() { diff --git a/src/os/signal/signal_unix.go b/src/os/signal/signal_unix.go index 7fa634f15a..0bbf41bfde 100644 --- a/src/os/signal/signal_unix.go +++ b/src/os/signal/signal_unix.go @@ -26,7 +26,8 @@ func loop() { func init() { signal_enable(0) // first call - initialize - go loop() + + watchSignalLoop = loop } const ( -- cgit v1.3-5-g9baa From a7042249abdba39a7c8dce35661b62094eb97117 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Thu, 3 Oct 2019 14:06:08 -0700 Subject: os/exec: simplify doc wording for cmd.StdoutPipe and cmd.StderrPipe The existing text was hard to parse. Shorten the sentences and simplify the text. Change-Id: Ic16f486925090ea303c04e70969e5a4b27a60896 Reviewed-on: https://go-review.googlesource.com/c/go/+/198758 Reviewed-by: Ian Lance Taylor --- src/os/exec/exec.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/os') diff --git a/src/os/exec/exec.go b/src/os/exec/exec.go index 17ef003eca..19c7e2406a 100644 --- a/src/os/exec/exec.go +++ b/src/os/exec/exec.go @@ -606,8 +606,8 @@ func (c *closeOnce) close() { // standard output when the command starts. // // Wait will close the pipe after seeing the command exit, so most callers -// need not close the pipe themselves; however, an implication is that -// it is incorrect to call Wait before all reads from the pipe have completed. +// need not close the pipe themselves. It is thus incorrect to call Wait +// before all reads from the pipe have completed. // For the same reason, it is incorrect to call Run when using StdoutPipe. // See the example for idiomatic usage. func (c *Cmd) StdoutPipe() (io.ReadCloser, error) { @@ -631,8 +631,8 @@ func (c *Cmd) StdoutPipe() (io.ReadCloser, error) { // standard error when the command starts. // // Wait will close the pipe after seeing the command exit, so most callers -// need not close the pipe themselves; however, an implication is that -// it is incorrect to call Wait before all reads from the pipe have completed. +// need not close the pipe themselves. It is thus incorrect to call Wait +// before all reads from the pipe have completed. // For the same reason, it is incorrect to use Run when using StderrPipe. // See the StdoutPipe example for idiomatic usage. func (c *Cmd) StderrPipe() (io.ReadCloser, error) { -- cgit v1.3-5-g9baa