Skip to content

Disallow reconfiguring the executor in newFixedThreadPoolContext #4364

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

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion kotlinx-coroutines-core/jvm/src/ThreadPoolDispatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ public actual fun newFixedThreadPoolContext(nThreads: Int, name: String): Execut
t.isDaemon = true
t
}
return executor.asCoroutineDispatcher()
return Executors.unconfigurableExecutorService(executor).asCoroutineDispatcher()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package kotlinx.coroutines

import kotlinx.coroutines.internal.LocalAtomicInt
import kotlinx.coroutines.testing.*
import java.util.concurrent.ScheduledThreadPoolExecutor
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.test.*

class MultithreadedDispatchersJvmTest: TestBase() {
/** Tests that the executor created in [newFixedThreadPoolContext] can not leak and be reconfigured. */
@Test
fun testExecutorReconfiguration() {
newFixedThreadPoolContext(1, "test").apply {
(executor as? ScheduledThreadPoolExecutor)?.corePoolSize = 2
}.use { ctx ->
val atomicInt = LocalAtomicInt(0)
repeat(100) {
ctx.dispatch(EmptyCoroutineContext, Runnable {
val entered = atomicInt.incrementAndGet()
Thread.yield() // allow other tasks to run
try {
check(entered == 1) { "Expected only one thread to be used, observed $entered" }
} finally {
atomicInt.decrementAndGet()
}
})
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import kotlin.coroutines.*

internal fun newSingleThreadContext(name: String): ExecutorCoroutineDispatcher = ClosedAfterGuideTestDispatcher(1, name)

internal fun newFixedThreadPoolContext(nThreads: Int, name: String): ExecutorCoroutineDispatcher =
ClosedAfterGuideTestDispatcher(nThreads, name)

private class ClosedAfterGuideTestDispatcher(
private val nThreads: Int,
private val name: String
Expand Down
3 changes: 3 additions & 0 deletions test-utils/jvm/src/FieldWalker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ object FieldWalker {
element is AtomicLongFieldUpdater<*> -> {
/* filter it out here to suppress its subclasses too */
}
element is ExecutorService && type.name == "java.util.concurrent.Executors\$DelegatedExecutorService" -> {
/* can't access anything in the executor */
}
// All the other classes are reflectively scanned
else -> fields(type, statics).forEach { field ->
push(field.get(element), visited, stack) { Ref.FieldRef(element, field.name) }
Expand Down