aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSon Luong Ngoc <sluongng@gmail.com>2024-04-30 12:40:55 +0200
committerGitHub <noreply@github.com>2024-04-30 10:40:55 +0000
commit54c451550c520d4a26f491fa5f52a97b2dcfa0d7 (patch)
tree9bbc393371732ff9c45b8125c63a0f27f2721729
parent54d8f4872aed67ec8e19ea46a94be6d94d1f0070 (diff)
downloadbazelbuild-rules_go-54c451550c520d4a26f491fa5f52a97b2dcfa0d7.tar.gz
bzltestutil: restore timeout signal handler (#3929)
In https://github.com/bazelbuild/rules_go/pull/3920, a new mechanism to handle test timeout was introduced. However this broke existing tests that use SIGTERM inside. Restore the original behavior.
-rw-r--r--go/tools/builders/generate_test_main.go18
1 files changed, 16 insertions, 2 deletions
diff --git a/go/tools/builders/generate_test_main.go b/go/tools/builders/generate_test_main.go
index f5b5fcce..83001fbd 100644
--- a/go/tools/builders/generate_test_main.go
+++ b/go/tools/builders/generate_test_main.go
@@ -245,9 +245,23 @@ func main() {
// we set -test.timeout according to the TEST_TIMEOUT, we need to ignore the signal so the test has
// time to properly produce the output (e.g. stack trace). It will be killed by Bazel after the grace
// period (15s) expires.
+ //
// If TEST_TIMEOUT is not set (e.g., when the test binary is run by Delve for debugging), we don't
- // ignore SIGTERM so it can be properly terminated.
- signal.Ignore(syscall.SIGTERM)
+ // ignore SIGTERM so it can be properly terminated. (1)
+ // We do not panic (like native go test does) because users may legitimately want to use SIGTERM
+ // in tests.
+ //
+ // signal.Notify is used to ensure that there is a no-op signal handler registered.
+ // Avoid using signal.Ignore here: despite the name, it's only used to unregister handlers that
+ // were previously registered by signal.Notify. See (2) for more information.
+ //
+ // (1): https://github.com/golang/go/blob/e816eb50140841c524fd07ecb4eaa078954eb47c/src/testing/testing.go#L2351
+ // (2): https://github.com/bazelbuild/rules_go/pull/3929
+ c := make(chan os.Signal, 1)
+ signal.Notify(c, syscall.SIGTERM)
+ go func() {
+ <-c
+ }()
}
{{if not .TestMain}}