This is an automated email from the ASF dual-hosted git repository.
pbacsko pushed a commit to branch branch-1.5
in repository https://gitbox.apache.org/repos/asf/yunikorn-core.git
The following commit(s) were added to refs/heads/branch-1.5 by this push:
new 1c5884bd [YUNIKORN-2553] Enable deadlock detection during unit tests
(#843)
1c5884bd is described below
commit 1c5884bde6bba5698cb35a8b9ccade4a920cf4e8
Author: Peter Bacsko <[email protected]>
AuthorDate: Tue Apr 16 23:52:46 2024 +0200
[YUNIKORN-2553] Enable deadlock detection during unit tests (#843)
Closes: #843
Signed-off-by: Peter Bacsko <[email protected]>
---
Makefile | 3 +++
pkg/locking/locking.go | 19 ++++++++++++++++++-
pkg/locking/locking_test.go | 2 ++
3 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
index 8ba413a9..b0eea917 100644
--- a/Makefile
+++ b/Makefile
@@ -200,6 +200,9 @@ build: commands
# Run the tests after building
.PHONY: test
+test: export DEADLOCK_DETECTION_ENABLED = true
+test: export DEADLOCK_TIMEOUT_SECONDS = 10
+test: export DEADLOCK_EXIT = true
test:
@echo "running unit tests"
@mkdir -p build
diff --git a/pkg/locking/locking.go b/pkg/locking/locking.go
index 598a1ffb..9c0c661a 100644
--- a/pkg/locking/locking.go
+++ b/pkg/locking/locking.go
@@ -33,11 +33,14 @@ import (
const EnvDeadlockDetectionEnabled = "DEADLOCK_DETECTION_ENABLED"
const EnvDeadlockTimeoutSeconds = "DEADLOCK_TIMEOUT_SECONDS"
+const EnvExitOnDeadlock = "DEADLOCK_EXIT"
var once sync.Once
var trackingEnabled atomic.Bool
var timeoutSeconds atomic.Int32
var deadlockDetected atomic.Bool
+var testingMode atomic.Bool
+var exitOnDeadlock bool
type errorBuf struct {
data string
@@ -74,13 +77,27 @@ func reInit() {
godeadlock.Opts.DeadlockTimeout = time.Duration(timeoutSec) *
time.Second
godeadlock.Opts.LogBuf = &errorBuf{}
godeadlock.Opts.OnPotentialDeadlock = onPotentialDeadlock
+ if exitEnv, err := strconv.ParseBool(os.Getenv(EnvExitOnDeadlock)); err
!= nil {
+ exitOnDeadlock = false
+ } else {
+ exitOnDeadlock = exitEnv
+ }
+
if enabled {
- fmt.Fprintf(os.Stderr, "=== Deadlock detection enabled
(timeout: %d seconds) ===\n", timeoutSec)
+ // We want to ensure that we write this before any other
subsystem is initialized, including logging which may also use locks.
+ fmt.Fprintf(os.Stderr, "=== Deadlock detection enabled
(timeout: %d seconds, exit on deadlock: %v) ===\n", timeoutSec, exitOnDeadlock)
}
}
func onPotentialDeadlock() {
deadlockDetected.Store(true)
+ printBufContents()
+ if exitOnDeadlock && !testingMode.Load() {
+ os.Exit(1)
+ }
+}
+
+func printBufContents() {
buf, ok := godeadlock.Opts.LogBuf.(*errorBuf)
buf.Lock()
defer buf.Unlock()
diff --git a/pkg/locking/locking_test.go b/pkg/locking/locking_test.go
index cb62d5d2..34993505 100644
--- a/pkg/locking/locking_test.go
+++ b/pkg/locking/locking_test.go
@@ -34,12 +34,14 @@ import (
func disableTracking() {
os.Unsetenv(EnvDeadlockDetectionEnabled)
os.Unsetenv(EnvDeadlockTimeoutSeconds)
+ testingMode.Store(false)
reInit()
}
func enableTracking() {
os.Setenv(EnvDeadlockDetectionEnabled, "true")
os.Setenv(EnvDeadlockTimeoutSeconds, "1")
+ testingMode.Store(true)
reInit()
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]