Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
}
```

### Fixes

- Avoid StrictMode warnings ([#4724](https://github.com/getsentry/sentry-java/pull/4724))

### Improvements

- Remove internal API status from get/setDistinctId ([#4708](https://github.com/getsentry/sentry-java/pull/4708))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.app.Application;
import android.content.Context;
import android.os.Process;
import android.os.StrictMode;
import android.os.SystemClock;
import io.sentry.ILogger;
import io.sentry.IScopes;
Expand Down Expand Up @@ -93,6 +94,10 @@ public static void init(
@NotNull final Context context,
@NotNull ILogger logger,
@NotNull Sentry.OptionsConfiguration<SentryAndroidOptions> configuration) {
final @NotNull StrictMode.ThreadPolicy oldPolicy = StrictMode.getThreadPolicy();
final @NotNull StrictMode.VmPolicy oldVmPolicy = StrictMode.getVmPolicy();
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.LAX);
StrictMode.setVmPolicy(StrictMode.VmPolicy.LAX);
try (final @NotNull ISentryLifecycleToken ignored = staticLock.acquire()) {
Sentry.init(
OptionsContainer.create(SentryAndroidOptions.class),
Expand Down Expand Up @@ -209,6 +214,9 @@ public static void init(
logger.log(SentryLevel.FATAL, "Fatal error during SentryAndroid.init(...)", e);

throw new RuntimeException("Failed to initialize Sentry's SDK", e);
} finally {
StrictMode.setThreadPolicy(oldPolicy);
StrictMode.setVmPolicy(oldVmPolicy);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.sentry.util.HintUtils;
import io.sentry.util.Objects;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -157,18 +158,18 @@ public static boolean hasStartupCrashMarker(final @NotNull SentryOptions options

final File lastAnrMarker = new File(cacheDirPath, LAST_ANR_REPORT);
try {
if (lastAnrMarker.exists() && lastAnrMarker.canRead()) {
final String content = FileUtils.readText(lastAnrMarker);
// we wrapped into try-catch already
//noinspection ConstantConditions
return content.equals("null") ? null : Long.parseLong(content.trim());
} else {
final String content = FileUtils.readText(lastAnrMarker);
// we wrapped into try-catch already
//noinspection ConstantConditions
return content.equals("null") ? null : Long.parseLong(content.trim());
} catch (Throwable e) {
if (e instanceof FileNotFoundException) {
options
.getLogger()
.log(DEBUG, "Last ANR marker does not exist. %s.", lastAnrMarker.getAbsolutePath());
} else {
options.getLogger().log(ERROR, "Error reading last ANR marker", e);
}
} catch (Throwable e) {
options.getLogger().log(ERROR, "Error reading last ANR marker", e);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ private CpuInfoUtils() {}
if (!cpuDir.getName().matches("cpu[0-9]+")) continue;
File cpuMaxFreqFile = new File(cpuDir, CPUINFO_MAX_FREQ_PATH);

if (!cpuMaxFreqFile.exists() || !cpuMaxFreqFile.canRead()) continue;

long khz;
try {
String content = FileUtils.readText(cpuMaxFreqFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.sentry.uitest.android

import android.os.StrictMode
import androidx.lifecycle.Lifecycle
import androidx.test.core.app.launchActivity
import androidx.test.ext.junit.runners.AndroidJUnit4
Expand Down Expand Up @@ -269,6 +270,13 @@ class SdkInitTests : BaseUiTest() {
assertDefaultIntegrations()
}

@Test
fun initNotThrowStrictMode() {
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().detectAll().penaltyDeath().build())
StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build())
initSentry()
}

private fun assertDefaultIntegrations() {
val integrations =
mutableListOf(
Expand Down
24 changes: 6 additions & 18 deletions sentry/src/main/java/io/sentry/DirectoryProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,22 @@ public void processDirectory(final @NotNull File directory) {
try {
logger.log(SentryLevel.DEBUG, "Processing dir. %s", directory.getAbsolutePath());

if (!directory.exists()) {
final File[] filteredListFiles = directory.listFiles((d, name) -> isRelevantFileName(name));
if (filteredListFiles == null) {
logger.log(
SentryLevel.WARNING,
"Directory '%s' doesn't exist. No cached events to send.",
SentryLevel.ERROR,
"Cache dir %s is null or is not a directory.",
directory.getAbsolutePath());
return;
}
if (!directory.isDirectory()) {
logger.log(
SentryLevel.ERROR, "Cache dir %s is not a directory.", directory.getAbsolutePath());
return;
}

final File[] listFiles = directory.listFiles();
if (listFiles == null) {
logger.log(SentryLevel.ERROR, "Cache dir %s is null.", directory.getAbsolutePath());
return;
}

final File[] filteredListFiles = directory.listFiles((d, name) -> isRelevantFileName(name));

logger.log(
SentryLevel.DEBUG,
"Processing %d items from cache dir %s",
filteredListFiles != null ? filteredListFiles.length : 0,
filteredListFiles.length,
directory.getAbsolutePath());

for (File file : listFiles) {
for (File file : filteredListFiles) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I found we were filtering the files in the directory, but then looped over all the files anyway
Was it by design or by accident?
And is there any risk in changing this?

// it ignores .sentry-native database folder and new ones that might come up
if (!file.isFile()) {
logger.log(SentryLevel.DEBUG, "File %s is not a File.", file.getAbsolutePath());
Expand Down
15 changes: 7 additions & 8 deletions sentry/src/main/java/io/sentry/cache/EnvelopeCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,18 +336,17 @@ public void discard(final @NotNull SentryEnvelope envelope) {
Objects.requireNonNull(envelope, "Envelope is required.");

final File envelopeFile = getEnvelopeFile(envelope);
if (envelopeFile.exists()) {
if (envelopeFile.delete()) {
options
.getLogger()
.log(DEBUG, "Discarding envelope from cache: %s", envelopeFile.getAbsolutePath());

if (!envelopeFile.delete()) {
options
.getLogger()
.log(ERROR, "Failed to delete envelope: %s", envelopeFile.getAbsolutePath());
}
} else {
options.getLogger().log(DEBUG, "Envelope was not cached: %s", envelopeFile.getAbsolutePath());
options
.getLogger()
.log(
DEBUG,
"Envelope was not cached or could not be deleted: %s",
envelopeFile.getAbsolutePath());
}
}

Expand Down
11 changes: 6 additions & 5 deletions sentry/src/test/java/io/sentry/EnvelopeSenderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class EnvelopeSenderTest {
val sut = fixture.getSut()
sut.processDirectory(File("i don't exist"))
verify(fixture.logger)!!.log(
eq(SentryLevel.WARNING),
eq("Directory '%s' doesn't exist. No cached events to send."),
eq(SentryLevel.ERROR),
eq("Cache dir %s is null or is not a directory."),
any<Any>(),
)
verifyNoMoreInteractions(fixture.scopes)
Expand All @@ -79,14 +79,14 @@ class EnvelopeSenderTest {
sut.processDirectory(testFile)
verify(fixture.logger)!!.log(
eq(SentryLevel.ERROR),
eq("Cache dir %s is not a directory."),
eq("Cache dir %s is null or is not a directory."),
any<Any>(),
)
verifyNoMoreInteractions(fixture.scopes)
}

@Test
fun `when directory has non event files, processDirectory logs that`() {
fun `when directory has non event files, processDirectory skips them`() {
val sut = fixture.getSut()
val testFile =
File(
Expand All @@ -96,7 +96,8 @@ class EnvelopeSenderTest {
testFile.deleteOnExit()
verify(fixture.logger)!!.log(
eq(SentryLevel.DEBUG),
eq("File '%s' doesn't match extension expected."),
eq("Processing %d items from cache dir %s"),
eq(0),
any<Any>(),
)
verify(fixture.scopes, never())!!.captureEnvelope(any(), anyOrNull())
Expand Down
Loading