diff --git a/jena-core/src/main/java/org/apache/jena/sys/JenaSystem.java b/jena-core/src/main/java/org/apache/jena/sys/JenaSystem.java index 09871f83b96..dcaab122413 100644 --- a/jena-core/src/main/java/org/apache/jena/sys/JenaSystem.java +++ b/jena-core/src/main/java/org/apache/jena/sys/JenaSystem.java @@ -22,6 +22,8 @@ import org.apache.jena.base.module.SubsystemRegistry; import org.apache.jena.base.module.SubsystemRegistryServiceLoader; +import java.util.concurrent.Executors; + /** Jena "system" - simple controls for ensuring components are loaded and initialized. *

* All initialization should be concurrent and thread-safe. In particular, @@ -67,6 +69,7 @@ public JenaSystem() { } */ public static boolean DEBUG_INIT = false ; private static volatile boolean initialized = false ; + private static ThreadLocal isInitializing = ThreadLocal.withInitial(() -> false); /** Output a debugging message if DEBUG_INIT is set */ public static void logLifecycle(String fmt, Object ...args) { @@ -80,10 +83,29 @@ public static void init() { // Once jena is initialized, all calls are an immediate return. if ( initialized ) return ; + + // Avoid recursive initialization. + if( isInitializing.get() ) + return ; + + // Set the flag to prevent recursive initialization. + isInitializing.set(true); + // Overlapping attempts to perform initialization will block on the synchronized. synchronized(JenaSystem.class) { if ( initialized ) return ; + + // Timeout if initialization takes too long. + var future = Executors.newScheduledThreadPool(1).schedule( + () -> { + if (initialized) + return; + + // This typically is a deadlock situation. The only thing we can do here is to print a message to System.err. + System.err.println("Timeout while waiting for JenaSystem.init(), possibly due to a deadlock while waiting on the class initialization monitor. Please ensure that JenaSystem.init() is called before working with multiple threads."); + }, 5, java.util.concurrent.TimeUnit.SECONDS); + setup(); if ( DEBUG_INIT ) singleton.debug(DEBUG_INIT); @@ -91,6 +113,7 @@ public static void init() { singleton.debug(false); // Last so overlapping initialization waits on the synchronized initialized = true; + future.cancel(true); } } diff --git a/jena-integration-tests/src/test/java/org/apache/jena/sys/TS_Sys.java b/jena-integration-tests/src/test/java/org/apache/jena/sys/TS_Sys.java new file mode 100644 index 00000000000..9826dd48d10 --- /dev/null +++ b/jena-integration-tests/src/test/java/org/apache/jena/sys/TS_Sys.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jena.sys; + +import org.apache.jena.sparql.exec.http.*; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses( + { TestJenaSystem.class + }) + +public class TS_Sys { } diff --git a/jena-integration-tests/src/test/java/org/apache/jena/sys/TestJenaSystem.java b/jena-integration-tests/src/test/java/org/apache/jena/sys/TestJenaSystem.java new file mode 100644 index 00000000000..1e885787ce1 --- /dev/null +++ b/jena-integration-tests/src/test/java/org/apache/jena/sys/TestJenaSystem.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jena.sys; + +import org.apache.jena.rdf.model.ModelFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.stream.IntStream; + +import static org.junit.jupiter.api.Assertions.*; + +public class TestJenaSystem { + + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + private final PrintStream originalErr = System.err; + + @Before + public void setUpStreams() { + System.setOut(new PrintStream(outContent)); + System.setErr(new PrintStream(errContent)); + } + + @After + public void restoreStreams() { + System.setOut(originalOut); + System.setErr(originalErr); + } + + @Test + public void testInit() { + assertDoesNotThrow(() -> JenaSystem.init()); + + assertEquals("", errContent.toString(), "System.err should be empty"); + } + + @Test + public void testInitParallel() throws ExecutionException, InterruptedException { + + // it is mandatory to init JenaSystem before running anything in parallel + JenaSystem.init(); + + var pool = Executors.newFixedThreadPool(8); + + var futures = IntStream.range(0, 16) + .mapToObj(i -> pool.submit(() -> { + if (i % 2 == 0) + ModelFactory.createDefaultModel(); + else + JenaSystem.init(); + + return i; + })) + .toList(); + + assertTimeoutPreemptively( + Duration.of(5, ChronoUnit.SECONDS), + () -> { + for (var future : futures) { + System.out.println(future.get()); + } + }); + + var output = outContent.toString(); + + assertEquals(16, output.lines().count()); + } +}