Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
### Improvements

- Make user interaction tracing faster and do fewer allocations ([#4347](https://github.com/getsentry/sentry-java/pull/4347))
- Pre-load modules on a background thread upon SDK init ([#4348](https://github.com/getsentry/sentry-java/pull/4348))

## 8.8.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.io.InputStream;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

Expand All @@ -21,6 +23,12 @@ public final class AssetsModulesLoader extends ModulesLoader {
public AssetsModulesLoader(final @NotNull Context context, final @NotNull ILogger logger) {
super(logger);
this.context = ContextUtils.getApplicationContext(context);

// pre-load modules on a bg thread to avoid doing so on the main thread in case of a crash/error
final @NotNull ExecutorService executorService = Executors.newSingleThreadExecutor();
//noinspection Convert2MethodRef
executorService.submit(() -> getOrLoadModules());
executorService.shutdown();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.sentry.internal.modules;

import io.sentry.ILogger;
import io.sentry.ISentryLifecycleToken;
import io.sentry.SentryLevel;
import io.sentry.util.AutoClosableReentrantLock;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -21,18 +23,23 @@ public abstract class ModulesLoader implements IModulesLoader {

public static final String EXTERNAL_MODULES_FILENAME = "sentry-external-modules.txt";
protected final @NotNull ILogger logger;
private @Nullable Map<String, String> cachedModules = null;

private final @NotNull AutoClosableReentrantLock modulesLock = new AutoClosableReentrantLock();
private volatile @Nullable Map<String, String> cachedModules = null;

public ModulesLoader(final @NotNull ILogger logger) {
this.logger = logger;
}

@Override
public @Nullable Map<String, String> getOrLoadModules() {
if (cachedModules != null) {
return cachedModules;
if (cachedModules == null) {
try (final @NotNull ISentryLifecycleToken ignored = modulesLock.acquire()) {
if (cachedModules == null) {
cachedModules = loadModules();
}
}
}
cachedModules = loadModules();
return cachedModules;
}

Expand Down
Loading