sentry-java icon indicating copy to clipboard operation
sentry-java copied to clipboard

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

Open AntonVoronkin opened this issue 2 years ago • 10 comments

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.

AntonVoronkin avatar Aug 24 '23 15:08 AntonVoronkin

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)
    }
}

markushi avatar Aug 28 '23 06:08 markushi

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

AntonVoronkin avatar Aug 28 '23 14:08 AntonVoronkin

@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!

romtsn avatar Aug 28 '23 15:08 romtsn

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

AntonVoronkin avatar Aug 29 '23 08:08 AntonVoronkin

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

romtsn avatar Aug 29 '23 08:08 romtsn

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 avatar Oct 19 '23 09:10 stefanosiano

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

stefanosiano avatar Oct 19 '23 11:10 stefanosiano

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

romtsn avatar Nov 21 '23 07:11 romtsn

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://[email protected]/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 avatar Nov 29 '23 14:11 markushi

Let's try without even packaging the NDK

markushi avatar Nov 29 '23 14:11 markushi