Skip to content

Commit db972c8

Browse files
committed
Prevent a null Capture.logger instance inside onBeforeReportSend callback
1 parent 8bba767 commit db972c8

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

platform/jvm/capture/src/main/kotlin/io/bitdrift/capture/Capture.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,10 @@ object Capture {
689689

690690
default.set(LoggerState.Started(loggerImpl))
691691

692+
// Must be initialized right after the logger state is set to avoid a null
693+
// Capture.logger() reference when onBeforeSend callbacks are triggered.
694+
loggerImpl.initIssueReporter()
695+
692696
val sdkConfiguredDuration =
693697
SdkConfiguredDuration(
694698
wholeStartDuration = startSdkTimer.elapsedNow(),

platform/jvm/capture/src/main/kotlin/io/bitdrift/capture/LoggerImpl.kt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ internal class LoggerImpl(
9898
internal val webViewConfiguration: WebViewConfiguration? = configuration.webViewConfiguration
9999

100100
private val metadataProvider: MetadataProvider
101+
private val sdkDirectory: String
101102
private val batteryMonitor = BatteryMonitor(context)
102103
private val powerMonitor = PowerMonitor(context)
103104
private val diskUsageMonitor: DiskUsageMonitor
@@ -162,7 +163,7 @@ internal class LoggerImpl(
162163
okHttpClient = sharedOkHttpClient,
163164
)
164165

165-
val sdkDirectory = SdkDirectory.getPath(context)
166+
sdkDirectory = SdkDirectory.getPath(context)
166167

167168
val localErrorReporter =
168169
errorReporter ?: ErrorReporterService(
@@ -286,14 +287,6 @@ internal class LoggerImpl(
286287

287288
previousRunInfoResolver.initLegacyWatcher(sdkDirectory)
288289

289-
// issue reporter needs to be initialized after appExitLogger and the jniLogger
290-
issueReporter?.init(
291-
activityManager = activityManager,
292-
sdkDirectory = sdkDirectory,
293-
clientAttributes = clientAttributes,
294-
completedReportsProcessor = this,
295-
)
296-
297290
startDebugOperationsAsNeeded(context)
298291
}
299292

@@ -684,6 +677,19 @@ internal class LoggerImpl(
684677

685678
internal fun getPreviousRunInfo(): PreviousRunInfo? = previousRunInfoResolver.get(activityManager)
686679

680+
/**
681+
* Initializes the issue reporter. Must be called immediately right after the LoggerImpl is created
682+
* so we can guarantee that any calls to Capture.Logger.* are valid within `onBeforeReportSend`
683+
*/
684+
internal fun initIssueReporter() {
685+
issueReporter?.init(
686+
activityManager = activityManager,
687+
sdkDirectory = sdkDirectory,
688+
clientAttributes = clientAttributes,
689+
completedReportsProcessor = this,
690+
)
691+
}
692+
687693
private fun startDebugOperationsAsNeeded(context: Context) {
688694
if (!BuildTypeChecker.isDebuggable(context)) {
689695
return

0 commit comments

Comments
 (0)