Skip to content

Commit 2502b01

Browse files
committed
bzltestutil: restore timeout signal handler
In bazel-contrib#3920, a new mechanism to handle test timeout was introduced. However this broke existing tests that use SIGTERM inside. Restore the original behavior.
1 parent 54d8f48 commit 2502b01

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

go/tools/builders/generate_test_main.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,23 @@ func main() {
245245
// we set -test.timeout according to the TEST_TIMEOUT, we need to ignore the signal so the test has
246246
// time to properly produce the output (e.g. stack trace). It will be killed by Bazel after the grace
247247
// period (15s) expires.
248+
//
248249
// If TEST_TIMEOUT is not set (e.g., when the test binary is run by Delve for debugging), we don't
249-
// ignore SIGTERM so it can be properly terminated.
250-
signal.Ignore(syscall.SIGTERM)
250+
// ignore SIGTERM so it can be properly terminated. (1)
251+
// We do not panic (like native go test does) because users may legitimately want to use SIGTERM
252+
// in tests.
253+
//
254+
// signal.Notify is used to ensure that there is a no-op signal handler registered.
255+
// Avoid using signal.Ignore here: despite the name, it's only used to unregistered handler(s) that
256+
// were previously registered by signal.Notify. See (2) for more information.
257+
//
258+
// (1): https://github.com/golang/go/blob/e816eb50140841c524fd07ecb4eaa078954eb47c/src/testing/testing.go#L2351
259+
// (2): https://github.com/bazelbuild/rules_go/pull/3929
260+
c := make(chan os.Signal, 1)
261+
signal.Notify(c, syscall.SIGTERM)
262+
go func() {
263+
<-c
264+
}()
251265
}
252266
253267
{{if not .TestMain}}

0 commit comments

Comments
 (0)