Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjusted test to detect and fail when reproducing the bug #3437

Open
wants to merge 2 commits into
base: series/3.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions tests/jvm/src/test/scala/cats/effect/IOPlatformSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -324,22 +324,44 @@ trait IOPlatformSpecification { self: BaseSpec with ScalaCheck =>
}

"safely detect hard-blocked threads even while blockers are being created" in {
val (compute, shutdown) =
IORuntime.createWorkStealingComputeThreadPool(blockedThreadDetectionEnabled = true)
0.until(10) foreach { _ =>
val prefix = "safe-detection-compute-worker"
val (compute, shutdown) =
IORuntime.createWorkStealingComputeThreadPool(
threadPrefix = prefix,
blockedThreadDetectionEnabled = true)

implicit val runtime: IORuntime =
IORuntime.builder().setCompute(compute, shutdown).build()
implicit val runtime: IORuntime =
IORuntime.builder().setCompute(compute, shutdown).build()

try {
val test = for {
_ <- IO.unit.foreverM.start.replicateA_(200)
_ <- 0.until(200).toList.parTraverse_(_ => IO.blocking(()))
} yield ok // we can't actually test this directly because the symptom is vaporizing a worker
try {
val before = {
val threads = new Array[Thread](Thread.activeCount())
Thread.enumerate(threads)

test.unsafeRunSync()
} finally {
runtime.shutdown()
threads.filter(_.getName().startsWith(prefix)).toList
}

val test = for {
_ <- IO.unit.foreverM.start.replicateA_(200)
_ <- 0.until(200).toList.parTraverse_(_ => IO.blocking(()))
} yield ()

test.unsafeRunSync()

/*
* There's a chance here that we lose a worker but we don't detect it. This can happen if
* a worker in `before` converts to a blocker, then is replaced by a new thread (which
* *isn't* in `before`), and then *that* thread is the one murdered by the NPE. The odds
* of this are much higher on machines with fewer physical threads.
*/
before must contain((_: Thread).isAlive()).foreach
} finally {
runtime.shutdown()
}
}

ok
}

// this test ensures that the parkUntilNextSleeper bit works
Expand Down