Skip to content

Commit

Permalink
apacheGH-2787: Deadlock in JenaSystem.init()
Browse files Browse the repository at this point in the history
- printing an error to System.err, with a detailed message
  -> unable to fix the deadlock itself, that is all I came up with
- added integration tests for JenaSystem.init(), also for the parallel case.
  • Loading branch information
arne-bdt committed Oct 23, 2024
1 parent 4247422 commit f2c8baa
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
23 changes: 23 additions & 0 deletions jena-core/src/main/java/org/apache/jena/sys/JenaSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* All initialization should be concurrent and thread-safe. In particular,
Expand Down Expand Up @@ -67,6 +69,7 @@ public JenaSystem() { }
*/
public static boolean DEBUG_INIT = false ;
private static volatile boolean initialized = false ;
private static ThreadLocal<Boolean> isInitializing = ThreadLocal.withInitial(() -> false);

/** Output a debugging message if DEBUG_INIT is set */
public static void logLifecycle(String fmt, Object ...args) {
Expand All @@ -80,17 +83,37 @@ 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 deadlock while waiting on the Class initalization monitor . Please call JenaSystem.init() before working with multiple threads.");
}, 5, java.util.concurrent.TimeUnit.SECONDS);

setup();
if ( DEBUG_INIT )
singleton.debug(DEBUG_INIT);
singleton.initialize();
singleton.debug(false);
// Last so overlapping initialization waits on the synchronized
initialized = true;
future.cancel(true);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 { }
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit f2c8baa

Please sign in to comment.