DataDog / dd-sdk-android

Datadog SDK for Android (Compatible with Kotlin and Java)
Apache License 2.0
156 stars 60 forks source link

StrictMode violations on DatadogTree : Timber.Tree #2229

Closed jszmltr closed 1 month ago

jszmltr commented 2 months ago

Describe the bug

There is bunch of StrictMode violations every time DatadogTree.log() is invoked. Are you planning on fixing those anytime soon ?

Reproduction steps

  1. Enabled StrictMode disk read violations:
    StrictMode.setThreadPolicy(
    ThreadPolicy.Builder()
      .detectDiskReads()
      .detectDiskWrites()
      .build()
    )
  2. Attach DatadogTree to Timber.
  3. Log something

Logcat logs

android.os.strictmode.DiskReadViolation
  at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1666)
  at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
  at libcore.io.ForwardingOs.access(ForwardingOs.java:128)
  at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:8054)
  at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:313)
  at java.io.File.exists(File.java:813)
  at com.datadog.android.core.internal.persistence.file.FileExtKt$existsSafe$1.invoke(FileExt.kt:89)
  at com.datadog.android.core.internal.persistence.file.FileExtKt$existsSafe$1.invoke(FileExt.kt:87)
  at com.datadog.android.core.internal.persistence.file.FileExtKt.safeCall(FileExt.kt:37)
  at com.datadog.android.core.internal.persistence.file.FileExtKt.existsSafe(FileExt.kt:87)
  at com.datadog.android.core.internal.persistence.file.batch.BatchFileOrchestrator.isRootDirValid(BatchFileOrchestrator.kt:145)
  at com.datadog.android.core.internal.persistence.file.batch.BatchFileOrchestrator.getRootDir(BatchFileOrchestrator.kt:106)
  at com.datadog.android.core.internal.persistence.ConsentAwareStorage.writeCurrentBatch(ConsentAwareStorage.kt:66)
  at com.datadog.android.core.internal.SdkFeature.withWriteContext(SdkFeature.kt:173)
  at com.datadog.android.api.feature.FeatureScope$DefaultImpls.withWriteContext$default(FeatureScope.kt:34)
  at com.datadog.android.log.internal.logger.DatadogLogHandler.handleLog(DatadogLogHandler.kt:58)
  at com.datadog.android.log.Logger.internalLog$dd_sdk_android_logs_release(Logger.kt:465)
  at com.datadog.android.log.Logger.internalLog$dd_sdk_android_logs_release$default(Logger.kt:452)
  at com.datadog.android.log.Logger.log(Logger.kt:163)
  at com.datadog.android.log.Logger.log$default(Logger.kt:157)
  at com.datadog.android.timber.DatadogTree.log(DatadogTree.kt:51)
android.os.strictmode.DiskReadViolation
  at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1666)
  at java.io.UnixFileSystem.getBooleanAttributes(UnixFileSystem.java:282)
  at java.io.File.isDirectory(File.java:843)
  at com.datadog.android.core.internal.persistence.file.batch.BatchFileOrchestrator.isRootDirValid(BatchFileOrchestrator.kt:146)
  at com.datadog.android.core.internal.persistence.file.batch.BatchFileOrchestrator.getRootDir(BatchFileOrchestrator.kt:106)
  at com.datadog.android.core.internal.persistence.ConsentAwareStorage.writeCurrentBatch(ConsentAwareStorage.kt:66)
  at com.datadog.android.core.internal.SdkFeature.withWriteContext(SdkFeature.kt:173)
  at com.datadog.android.api.feature.FeatureScope$DefaultImpls.withWriteContext$default(FeatureScope.kt:34)
  at com.datadog.android.log.internal.logger.DatadogLogHandler.handleLog(DatadogLogHandler.kt:58)
  at com.datadog.android.log.Logger.internalLog$dd_sdk_android_logs_release(Logger.kt:465)
  at com.datadog.android.log.Logger.internalLog$dd_sdk_android_logs_release$default(Logger.kt:452)
  at com.datadog.android.log.Logger.log(Logger.kt:163)
  at com.datadog.android.log.Logger.log$default(Logger.kt:157)
  at com.datadog.android.timber.DatadogTree.log(DatadogTree.kt:51)
android.os.strictmode.DiskReadViolation
  at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1666)
  at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
  at libcore.io.ForwardingOs.access(ForwardingOs.java:128)
  at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:8054)
  at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:313)
  at java.io.File.canWrite(File.java:788)
  at com.datadog.android.core.internal.persistence.file.FileExtKt$canWriteSafe$1.invoke(FileExt.kt:60)
  at com.datadog.android.core.internal.persistence.file.FileExtKt$canWriteSafe$1.invoke(FileExt.kt:58)
  at com.datadog.android.core.internal.persistence.file.FileExtKt.safeCall(FileExt.kt:37)
  at com.datadog.android.core.internal.persistence.file.FileExtKt.canWriteSafe(FileExt.kt:58)
  at com.datadog.android.core.internal.persistence.file.batch.BatchFileOrchestrator.isRootDirValid(BatchFileOrchestrator.kt:147)
  at com.datadog.android.core.internal.persistence.file.batch.BatchFileOrchestrator.getRootDir(BatchFileOrchestrator.kt:106)
  at com.datadog.android.core.internal.persistence.ConsentAwareStorage.writeCurrentBatch(ConsentAwareStorage.kt:66)
  at com.datadog.android.core.internal.SdkFeature.withWriteContext(SdkFeature.kt:173)
  at com.datadog.android.api.feature.FeatureScope$DefaultImpls.withWriteContext$default(FeatureScope.kt:34)
  at com.datadog.android.log.internal.logger.DatadogLogHandler.handleLog(DatadogLogHandler.kt:58)
  at com.datadog.android.log.Logger.internalLog$dd_sdk_android_logs_release(Logger.kt:465)
  at com.datadog.android.log.Logger.internalLog$dd_sdk_android_logs_release$default(Logger.kt:452)
  at com.datadog.android.log.Logger.log(Logger.kt:163)
  at com.datadog.android.log.Logger.log$default(Logger.kt:157)
  at com.datadog.android.timber.DatadogTree.log(DatadogTree.kt:51)

Expected behavior

There should be no disk read violations. they should be done off main thread.

Affected SDK versions

2.12.1

Latest working SDK version

N/A

Did you confirm if the latest SDK version fixes the bug?

Yes

Kotlin / Java version

2.0.20

Gradle / AGP version

8.10

Other dependencies versions

No response

Device Information

No response

Other relevant information

No response

mariusc83 commented 2 months ago

Hi @jszmltr , thank you for taking the time to report this. Indeed this needs to be offload from the main thread and we are going to open a ticket for it. We will keep you updated but most probably this will be fixed in the next version.