Skip to content

Commit 95282db

Browse files
committed
SentryAndroid.init now sets strictMode to Lax and reset it after it finishes
Removed few useless IO calls
1 parent 23d6b12 commit 95282db

File tree

7 files changed

+44
-41
lines changed

7 files changed

+44
-41
lines changed

sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.app.Application;
55
import android.content.Context;
66
import android.os.Process;
7+
import android.os.StrictMode;
78
import android.os.SystemClock;
89
import io.sentry.ILogger;
910
import io.sentry.IScopes;
@@ -93,6 +94,10 @@ public static void init(
9394
@NotNull final Context context,
9495
@NotNull ILogger logger,
9596
@NotNull Sentry.OptionsConfiguration<SentryAndroidOptions> configuration) {
97+
final @NotNull StrictMode.ThreadPolicy oldPolicy = StrictMode.getThreadPolicy();
98+
final @NotNull StrictMode.VmPolicy oldVmPolicy = StrictMode.getVmPolicy();
99+
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.LAX);
100+
StrictMode.setVmPolicy(StrictMode.VmPolicy.LAX);
96101
try (final @NotNull ISentryLifecycleToken ignored = staticLock.acquire()) {
97102
Sentry.init(
98103
OptionsContainer.create(SentryAndroidOptions.class),
@@ -210,6 +215,9 @@ public static void init(
210215

211216
throw new RuntimeException("Failed to initialize Sentry's SDK", e);
212217
}
218+
219+
StrictMode.setThreadPolicy(oldPolicy);
220+
StrictMode.setVmPolicy(oldVmPolicy);
213221
}
214222

215223
/**

sentry-android-core/src/main/java/io/sentry/android/core/cache/AndroidEnvelopeCache.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.sentry.util.HintUtils;
2020
import io.sentry.util.Objects;
2121
import java.io.File;
22+
import java.io.FileNotFoundException;
2223
import java.io.FileOutputStream;
2324
import java.io.OutputStream;
2425
import org.jetbrains.annotations.ApiStatus;
@@ -157,18 +158,18 @@ public static boolean hasStartupCrashMarker(final @NotNull SentryOptions options
157158

158159
final File lastAnrMarker = new File(cacheDirPath, LAST_ANR_REPORT);
159160
try {
160-
if (lastAnrMarker.exists() && lastAnrMarker.canRead()) {
161-
final String content = FileUtils.readText(lastAnrMarker);
162-
// we wrapped into try-catch already
163-
//noinspection ConstantConditions
164-
return content.equals("null") ? null : Long.parseLong(content.trim());
165-
} else {
161+
final String content = FileUtils.readText(lastAnrMarker);
162+
// we wrapped into try-catch already
163+
//noinspection ConstantConditions
164+
return content.equals("null") ? null : Long.parseLong(content.trim());
165+
} catch (Throwable e) {
166+
if (e instanceof FileNotFoundException) {
166167
options
167168
.getLogger()
168169
.log(DEBUG, "Last ANR marker does not exist. %s.", lastAnrMarker.getAbsolutePath());
170+
} else {
171+
options.getLogger().log(ERROR, "Error reading last ANR marker", e);
169172
}
170-
} catch (Throwable e) {
171-
options.getLogger().log(ERROR, "Error reading last ANR marker", e);
172173
}
173174
return null;
174175
}

sentry-android-core/src/main/java/io/sentry/android/core/internal/util/CpuInfoUtils.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ private CpuInfoUtils() {}
5151
if (!cpuDir.getName().matches("cpu[0-9]+")) continue;
5252
File cpuMaxFreqFile = new File(cpuDir, CPUINFO_MAX_FREQ_PATH);
5353

54-
if (!cpuMaxFreqFile.exists() || !cpuMaxFreqFile.canRead()) continue;
55-
5654
long khz;
5755
try {
5856
String content = FileUtils.readText(cpuMaxFreqFile);

sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/SdkInitTests.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.sentry.uitest.android
22

3+
import android.os.StrictMode
34
import androidx.lifecycle.Lifecycle
45
import androidx.test.core.app.launchActivity
56
import androidx.test.ext.junit.runners.AndroidJUnit4
@@ -269,6 +270,13 @@ class SdkInitTests : BaseUiTest() {
269270
assertDefaultIntegrations()
270271
}
271272

273+
@Test
274+
fun initNotThrowStrictMode() {
275+
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().detectAll().penaltyDeath().build())
276+
StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build())
277+
initSentry()
278+
}
279+
272280
private fun assertDefaultIntegrations() {
273281
val integrations =
274282
mutableListOf(

sentry/src/main/java/io/sentry/DirectoryProcessor.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,22 @@ public void processDirectory(final @NotNull File directory) {
4040
try {
4141
logger.log(SentryLevel.DEBUG, "Processing dir. %s", directory.getAbsolutePath());
4242

43-
if (!directory.exists()) {
43+
final File[] filteredListFiles = directory.listFiles((d, name) -> isRelevantFileName(name));
44+
if (filteredListFiles == null) {
4445
logger.log(
45-
SentryLevel.WARNING,
46-
"Directory '%s' doesn't exist. No cached events to send.",
46+
SentryLevel.ERROR,
47+
"Cache dir %s is null or is not a directory.",
4748
directory.getAbsolutePath());
4849
return;
4950
}
50-
if (!directory.isDirectory()) {
51-
logger.log(
52-
SentryLevel.ERROR, "Cache dir %s is not a directory.", directory.getAbsolutePath());
53-
return;
54-
}
55-
56-
final File[] listFiles = directory.listFiles();
57-
if (listFiles == null) {
58-
logger.log(SentryLevel.ERROR, "Cache dir %s is null.", directory.getAbsolutePath());
59-
return;
60-
}
61-
62-
final File[] filteredListFiles = directory.listFiles((d, name) -> isRelevantFileName(name));
6351

6452
logger.log(
6553
SentryLevel.DEBUG,
6654
"Processing %d items from cache dir %s",
67-
filteredListFiles != null ? filteredListFiles.length : 0,
55+
filteredListFiles.length,
6856
directory.getAbsolutePath());
6957

70-
for (File file : listFiles) {
58+
for (File file : filteredListFiles) {
7159
// it ignores .sentry-native database folder and new ones that might come up
7260
if (!file.isFile()) {
7361
logger.log(SentryLevel.DEBUG, "File %s is not a File.", file.getAbsolutePath());

sentry/src/main/java/io/sentry/cache/EnvelopeCache.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,18 +336,17 @@ public void discard(final @NotNull SentryEnvelope envelope) {
336336
Objects.requireNonNull(envelope, "Envelope is required.");
337337

338338
final File envelopeFile = getEnvelopeFile(envelope);
339-
if (envelopeFile.exists()) {
339+
if (envelopeFile.delete()) {
340340
options
341341
.getLogger()
342342
.log(DEBUG, "Discarding envelope from cache: %s", envelopeFile.getAbsolutePath());
343-
344-
if (!envelopeFile.delete()) {
345-
options
346-
.getLogger()
347-
.log(ERROR, "Failed to delete envelope: %s", envelopeFile.getAbsolutePath());
348-
}
349343
} else {
350-
options.getLogger().log(DEBUG, "Envelope was not cached: %s", envelopeFile.getAbsolutePath());
344+
options
345+
.getLogger()
346+
.log(
347+
DEBUG,
348+
"Envelope was not cached or could not be deleted: %s",
349+
envelopeFile.getAbsolutePath());
351350
}
352351
}
353352

sentry/src/test/java/io/sentry/EnvelopeSenderTest.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ class EnvelopeSenderTest {
6161
val sut = fixture.getSut()
6262
sut.processDirectory(File("i don't exist"))
6363
verify(fixture.logger)!!.log(
64-
eq(SentryLevel.WARNING),
65-
eq("Directory '%s' doesn't exist. No cached events to send."),
64+
eq(SentryLevel.ERROR),
65+
eq("Cache dir %s is null or is not a directory."),
6666
any<Any>(),
6767
)
6868
verifyNoMoreInteractions(fixture.scopes)
@@ -79,14 +79,14 @@ class EnvelopeSenderTest {
7979
sut.processDirectory(testFile)
8080
verify(fixture.logger)!!.log(
8181
eq(SentryLevel.ERROR),
82-
eq("Cache dir %s is not a directory."),
82+
eq("Cache dir %s is null or is not a directory."),
8383
any<Any>(),
8484
)
8585
verifyNoMoreInteractions(fixture.scopes)
8686
}
8787

8888
@Test
89-
fun `when directory has non event files, processDirectory logs that`() {
89+
fun `when directory has non event files, processDirectory skips them`() {
9090
val sut = fixture.getSut()
9191
val testFile =
9292
File(
@@ -96,7 +96,8 @@ class EnvelopeSenderTest {
9696
testFile.deleteOnExit()
9797
verify(fixture.logger)!!.log(
9898
eq(SentryLevel.DEBUG),
99-
eq("File '%s' doesn't match extension expected."),
99+
eq("Processing %d items from cache dir %s"),
100+
eq(0),
100101
any<Any>(),
101102
)
102103
verify(fixture.scopes, never())!!.captureEnvelope(any(), anyOrNull())

0 commit comments

Comments
 (0)