From a0ba3eae5085e9f5fdb8c78d6a639ad37ff7e205 Mon Sep 17 00:00:00 2001 From: arne-bdt Date: Wed, 23 Oct 2024 16:03:29 +0200 Subject: [PATCH] GH-2787: Deadlock in JenaSystem.init() - reinstated early return from JenaSystem.init() from before https://github.com/apache/jena/commit/8654c07f23a892c03a7367981d814c3c8b094f62 - simplified init using "initialization-on-demand holder idiom" - added integration tests for JenaSystem.init(), also for the parallel case. --- .../java/org/apache/jena/sys/JenaSystem.java | 28 +++++-- .../test/java/org/apache/jena/sys/TS_Sys.java | 30 +++++++ .../org/apache/jena/sys/TestJenaSystem.java | 79 +++++++++++++++++++ 3 files changed, 129 insertions(+), 8 deletions(-) create mode 100644 jena-integration-tests/src/test/java/org/apache/jena/sys/TS_Sys.java create mode 100644 jena-integration-tests/src/test/java/org/apache/jena/sys/TestJenaSystem.java 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..c9a0b8038e2 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 @@ -78,22 +78,34 @@ public static void logLifecycle(String fmt, Object ...args) { public static void init() { // Once jena is initialized, all calls are an immediate return. - if ( initialized ) - return ; - // Overlapping attempts to perform initialization will block on the synchronized. - synchronized(JenaSystem.class) { - if ( initialized ) - return ; + if(initialized) { + return; + } + + // Access the initialized flag to trigger class loading + initialized = LazyHolder.IS_INITIALIZED; + } + + /** + * Initialization-on-demand holder idiom + * @see Initialization-on-demand holder idiom + * + */ + private static class LazyHolder { + public static final boolean IS_INITIALIZED = initialize(); + + private static boolean initialize() { + initialized = true; // Set early to avoid blocking on static initialization. setup(); if ( DEBUG_INIT ) singleton.debug(DEBUG_INIT); singleton.initialize(); singleton.debug(false); - // Last so overlapping initialization waits on the synchronized - initialized = true; + return true; } } + public static void shutdown() { singleton.shutdown(); } /** The level 0 subsystem - inserted without using the Registry load function. 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..4e035843113 --- /dev/null +++ b/jena-integration-tests/src/test/java/org/apache/jena/sys/TestJenaSystem.java @@ -0,0 +1,79 @@ +/* + * 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.Test; + + +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.util.HashSet; +import java.util.concurrent.Executors; +import java.util.stream.IntStream; + +import static org.junit.jupiter.api.Assertions.*; + +public class TestJenaSystem { + + /** + * Test that the first initialization of JenaSystem is successful. + * The name is chosen so that it is executed first. + */ + @Test + public void testFirstInitParallel() { + + 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(); + + var intSet = new HashSet(); + assertTimeoutPreemptively( + Duration.of(5, ChronoUnit.SECONDS), + () -> { + for (var future : futures) { + intSet.add(future.get()); + } + }); + + assertEquals(16, intSet.size()); + } + + /** + * Test that the first initialization of JenaSystem is successful. + * This test is placed in the integration tests module to ensure that the initialization + * is successful when multiple modules are loaded. + */ + @Test + public void testLaterInit() { + assertDoesNotThrow(() -> JenaSystem.init()); + } + + +}