Skip to content

Commit 23e5cc1

Browse files
committed
Better diagnostics when LockFreedomTestEnvironment fails to shutdown
Prints stack-trace of a thread that fails to shutdown.
1 parent 8ea41d1 commit 23e5cc1

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/LockFreedomTestEnvironment.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public open class LockFreedomTestEnvironment(
9494
}
9595

9696
private fun complete() {
97+
val activeNonPausedThreads: MutableMap<TestThread, Array<StackTraceElement>> = mutableMapOf()
9798
val shutdownDeadline = System.currentTimeMillis() + STALL_LIMIT_MS
9899
try {
99100
completed = true
@@ -106,21 +107,30 @@ public open class LockFreedomTestEnvironment(
106107
while (System.currentTimeMillis() < shutdownDeadline) {
107108
// Check all threads while shutting down:
108109
// All terminated threads are considered to make progress for the purpose of resuming stalled ones
109-
var hasActiveNonPausedThread = false
110+
activeNonPausedThreads.clear()
110111
for (t in threads) {
111112
when {
112113
!t.isAlive -> t.makeProgress(getPausedEpoch()) // not alive - makes progress
113114
t.index.inv() == status.get() -> {} // active, paused -- skip
114-
else -> hasActiveNonPausedThread = true
115+
else -> {
116+
val stackTrace = t.stackTrace
117+
if (t.isAlive) activeNonPausedThreads[t] = stackTrace
118+
}
115119
}
116120
}
117-
if (!hasActiveNonPausedThread) break
121+
if (activeNonPausedThreads.isEmpty()) break
118122
checkStalled()
119123
Thread.sleep(SHUTDOWN_CHECK_MS)
120124
}
125+
activeNonPausedThreads.forEach { (t, stackTrack) ->
126+
println("=== $t had failed to shutdown in time")
127+
stackTrack.forEach { println("\tat $it") }
128+
}
121129
} finally {
122130
shutdown(shutdownDeadline)
123131
}
132+
// if no other exception was throws & we had threads that did not shut down -- still fails
133+
if (activeNonPausedThreads.isNotEmpty()) error("Some threads had failed to shutdown in time")
124134
}
125135

126136
private fun shutdown(shutdownDeadline: Long) {

0 commit comments

Comments
 (0)