Skip to content
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

refactor(test): improving EmbeddedRuntime exception handling #4685

Merged
merged 1 commit into from
Dec 19, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.monitor.ConsoleMonitor;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.system.ConfigurationExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.SystemExtension;
import org.eclipse.edc.spi.system.configuration.Config;
import org.eclipse.edc.spi.system.configuration.ConfigFactory;
import org.jetbrains.annotations.NotNull;

import java.net.URL;
import java.net.URLClassLoader;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -70,54 +71,51 @@ private EmbeddedRuntime(MultiSourceServiceLocator serviceLocator, String name, M
@Override
public void boot(boolean addShutdownHook) {
var monitor = super.createMonitor();
try {
monitor.info("Starting runtime %s".formatted(name));

// Temporarily inject system properties.
var savedProperties = (Properties) System.getProperties().clone();
properties.forEach(System::setProperty);
monitor.info("Starting runtime %s".formatted(name));

var runtimeException = new AtomicReference<Exception>();
var latch = new CountDownLatch(1);
serviceLocator.registerSystemExtension(ConfigurationExtension.class, (ConfigurationExtension) () -> ConfigFactory.fromMap(properties));

runtimeThread = executorService.submit(() -> {
try {
var classLoader = URLClassLoader.newInstance(classPathEntries);
var runtimeThrowable = new AtomicReference<Throwable>();
var latch = new CountDownLatch(1);

Thread.currentThread().setContextClassLoader(classLoader);
runtimeThread = executorService.submit(() -> {
try {
var classLoader = URLClassLoader.newInstance(classPathEntries);

super.boot(false);
Thread.currentThread().setContextClassLoader(classLoader);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just saw this now. Technically this should reset the previous classloader for the TCCL in a finally block (on error) or when the runtime shuts down. Probably for another PR.

Copy link
Member Author

@ndr-brt ndr-brt Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be necessary as this code runs in a separated thread, so this classloader assignment is done only for the specific thread

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's currently working like this:

Thread.currentThread().setContextClassLoader(classLoader);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that could be a problem. Something to fix in another PR.


latch.countDown();
super.boot(false);

isRunning.set(true);
} catch (Exception e) {
runtimeException.set(e);
throw new EdcException(e);
}
});
latch.countDown();

if (!latch.await(20, SECONDS)) {
throw new EdcException("Failed to start EDC runtime", runtimeException.get());
isRunning.set(true);
} catch (Throwable e) {
runtimeThrowable.set(e);
}
});

monitor.info("Runtime %s started".formatted(name));
// Restore system properties.
System.setProperties(savedProperties);
} catch (Exception e) {
throw new EdcException(e);
try {
if (!latch.await(20, SECONDS)) {
throw new EdcException("Failed to start EDC runtime", runtimeThrowable.get());
}
} catch (InterruptedException e) {
throw new EdcException("Failed to start EDC runtime", runtimeThrowable.get());
}

monitor.info("Runtime %s started".formatted(name));
}

@Override
public void shutdown() {
serviceLocator.clearSystemExtensions();
super.shutdown();
if (isRunning()) {
super.shutdown();
isRunning.set(false);
}
if (runtimeThread != null && !runtimeThread.isDone()) {
runtimeThread.cancel(true);
}
isRunning.set(false);
}

@Override
Expand Down
Loading