getsentry / sentry-java

A Sentry SDK for Java, Android and other JVM languages.
https://docs.sentry.io/
MIT License
1.16k stars 435 forks source link

The application crashes with StrictMode error after sentry initialization on android api 31, 32, 33. #2905

Open AntonVoronkin opened 1 year ago

AntonVoronkin commented 1 year ago

Integration

sentry-android

Build System

Gradle

AGP Version

7.4.1

Proguard

Enabled

Version

6.27.0

Steps to Reproduce

The application crashes with StrictMode error after sentry initialization on android api 31, 32, 33. StrictMode policy for detect the SQL leaked, the closable objects leaked and detecting network operations.

Apparently, the issue is related to "android.appcompat:appcompat-resources:1.4.2 and android.appcompat:appcompat:1.4.2" entries present in the sentry-external-modules.txt file.

Sentry plugin version: 3.11.1; sentry version: 6.27.0 (6.28.0 dont' fix this problem); targetAndroidSdkVersion 31; minAndroidSdkVersion 24

Error example:

StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
                                                                                                        at android.os.StrictMode$AndroidCloseGuardReporter.report(StrictMode.java:1987)
                                                                                                        at dalvik.system.CloseGuard.warnIfOpen(CloseGuard.java:336)
                                                                                                        at sun.nio.ch.FileChannelImpl.finalize(FileChannelImpl.java:175)
                                                                                                        at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:319)
                                                                                                        at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:306)
                                                                                                        at java.lang.Daemons$Daemon.run(Daemons.java:140)
                                                                                                        at java.lang.Thread.run(Thread.java:1012)
                                                                                                    Caused by: java.lang.Throwable: Explicit termination method 'close' not called
                                                                                                        at dalvik.system.CloseGuard.openWithCallSite(CloseGuard.java:288)
                                                                                                        at dalvik.system.CloseGuard.open(CloseGuard.java:257)
                                                                                                        at sun.nio.ch.FileChannelImpl.<init>(FileChannelImpl.java:110)
                                                                                                        at sun.nio.ch.FileChannelImpl.open(FileChannelImpl.java:120)
                                                                                                        at java.io.FileInputStream.getChannel(FileInputStream.java:475)
                                                                                                        at androidx.core.graphics.TypefaceCompatUtil.mmap(TypefaceCompatUtil.java:115)
                                                                                                        at androidx.emoji2.text.FontRequestEmojiCompatConfig$FontRequestMetadataLoader.createMetadata(FontRequestEmojiCompatConfig.java:381)
                                                                                                        at androidx.emoji2.text.FontRequestEmojiCompatConfig$FontRequestMetadataLoader$$ExternalSyntheticLambda0.run(Unknown Source:2)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
                                                                                                        at java.lang.Thread.run(Thread.java:1012) 

Expected Result

There are no memory leaks.

Actual Result

There are memory leaks.

markushi commented 1 year ago

Hey @AntonVoronkin, thanks for reporting this issue! This is most likely due to our File I/O instrumentation feature, which wraps uses of FileInputStream. This also means the root cause is not the sentry SDK but rather the wrapped implementation, in your case androidx.core.graphics.TypefaceCompatUtil.java.

Could you try to build an app without Sentry instrumentation enabled? Even if disabled you should still experience the StrictMode crash.

app/build.gradle

import io.sentry.android.gradle.extensions.InstrumentationFeature
sentry {
    // other config...

    tracingInstrumentation {
      enabled.set(false)
    }
}
AntonVoronkin commented 1 year ago

I completely removed sentry (including gradle file), but the problem remained.

romtsn commented 1 year ago

@AntonVoronkin thanks for confirming, I'm gonna close the issue then as it seems to be unrelated to Sentry, but feel free to comment here again if you find out the problem is in our SDK. Thank you!

AntonVoronkin commented 1 year ago

I'm sorry, yesterday I got too caught up and gave the wrong answer. If the sentry is removed completely, the issue does NOT occur. @romtsn

romtsn commented 1 year ago

@AntonVoronkin alright we'll investigate on our side and come back, thanks

stefanosiano commented 1 year ago

There is a PR with some improvement related to Closeable resources. However, we have to investigate the native layer, yet: removing it makes this issue disappear.

stefanosiano commented 1 year ago

Note: there are no specific steps to reproduce this issue. Just bundle sentry-native inside, and after a while you should get it

romtsn commented 11 months ago

@stefanosiano maybe it's worth a try to reproduce this again with the recent fix in sentry-native?

markushi commented 11 months ago

After some more testing this is happening for me even when the NDK is disabled.

sentry sample app

StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
    .detectLeakedClosableObjects()
    .penaltyLog().penaltyDeath()
    .build());

SentryAndroid.init(
  this,
  options -> {
    options.setDsn("https://1053864c67cc410aa1ffc9701bd6f93d@o447951.ingest.sentry.io/5428559");
    options.setEnableNdk(false);
  });

results in

StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. Callsite: close
    at android.os.StrictMode$AndroidCloseGuardReporter.report(StrictMode.java:1992)
    at dalvik.system.CloseGuard.warnIfOpen(CloseGuard.java:347)
    at sun.nio.fs.UnixSecureDirectoryStream.finalize(UnixSecureDirectoryStream.java:580)
    at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:291)
    at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:278)
    at java.lang.Daemons$Daemon.run(Daemons.java:139)
    at java.lang.Thread.run(Thread.java:920)
2023-11-29 15:43:05.201 29115-29127 System.err              io.sentry.samples.android            W  StrictMode VmPolicy violation with POLICY_DEATH; shutting down.
markushi commented 11 months ago

Let's try without even packaging the NDK