From ebc6cdd08a0d8dac045305839421b726ae5c91b3 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Fri, 29 Jul 2022 17:06:24 +0200 Subject: fix(web/runner): Use correct context ClassLoader for ForkJoinPool This change updates the OpenDC web runner implementation to use the correct context ClassLoader for simulation jobs running inside a ForkJoinPool. By default, the ForkJoinPool will use the system class loader which does not have access to the services needed by the web runner. --- .../src/main/kotlin/org/opendc/web/runner/OpenDCRunner.kt | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/opendc-web/opendc-web-runner/src/main/kotlin/org/opendc/web/runner/OpenDCRunner.kt b/opendc-web/opendc-web-runner/src/main/kotlin/org/opendc/web/runner/OpenDCRunner.kt index 9c9a866d..ccc7f03a 100644 --- a/opendc-web/opendc-web-runner/src/main/kotlin/org/opendc/web/runner/OpenDCRunner.kt +++ b/opendc-web/opendc-web-runner/src/main/kotlin/org/opendc/web/runner/OpenDCRunner.kt @@ -45,6 +45,7 @@ import java.io.File import java.time.Duration import java.util.* import java.util.concurrent.* +import java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory /** * Class to execute the pending jobs via the OpenDC web API. @@ -81,7 +82,7 @@ public class OpenDCRunner( /** * The [ForkJoinPool] that is used to execute the simulation jobs. */ - private val pool = ForkJoinPool(parallelism) + private val pool = ForkJoinPool(parallelism, RunnerThreadFactory(Thread.currentThread().contextClassLoader), null, false) /** * A [ScheduledExecutorService] to manage the heartbeat of simulation jobs as well as tracking the deadline of @@ -303,4 +304,15 @@ public class OpenDCRunner( override fun toString(): String = "WebRunnerTopologyFactory" } } + + /** + * A custom [ForkJoinWorkerThreadFactory] that uses the [ClassLoader] of specified by the runner. + */ + private class RunnerThreadFactory(private val classLoader: ClassLoader) : ForkJoinWorkerThreadFactory { + override fun newThread(pool: ForkJoinPool): ForkJoinWorkerThread = object : ForkJoinWorkerThread(pool) { + init { + contextClassLoader = classLoader + } + } + } } -- cgit v1.2.3