Skip to content

Make Sessions resilient to the FirebaseApp instance being deleted #5238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,24 @@ public synchronized boolean isAutomaticDataCollectionEnabled() {
final boolean dataCollectionEnabled =
crashlyticsDataCollectionEnabled != null
? crashlyticsDataCollectionEnabled
: firebaseApp.isDataCollectionDefaultEnabled();
: isFirebaseDataCollectionDefaultEnabled();
logDataCollectionState(dataCollectionEnabled);
return dataCollectionEnabled;
}

/**
* Determine whether automatic data collection is enabled or disabled by default in all SDKs.
*
* <p>If the FirebaseApp has been deleted, returns false.
*/
private boolean isFirebaseDataCollectionDefaultEnabled() {
try {
return firebaseApp.isDataCollectionDefaultEnabled();
} catch (IllegalStateException ignored) {
return false;
}
}

public synchronized void setCrashlyticsDataCollectionEnabled(@Nullable Boolean enabled) {
if (enabled != null) {
setInManifest = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@
package com.google.firebase.perf;

import com.google.firebase.perf.application.AppStateMonitor;
import com.google.firebase.perf.logging.AndroidLogger;

/**
* FirebasePerformanceInitializer to initialize FirebasePerformance during app cold start
*
* @hide
*/
public final class FirebasePerformanceInitializer implements AppStateMonitor.AppColdStartCallback {
private static final AndroidLogger logger = AndroidLogger.getInstance();

@Override
public void onAppColdStart() {
// Initialize FirebasePerformance when app cold starts.
FirebasePerformance.getInstance();
try {
FirebasePerformance.getInstance();
} catch (IllegalStateException ex) {
logger.info("Failed to get instance of FirebasePerformance: %s", ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,14 @@ internal constructor(
return
}

sessionCoordinator.attemptLoggingSessionEvent(
SessionEvents.startSession(firebaseApp, sessionDetails, sessionSettings, subscribers)
)
try {
val sessionEvent =
SessionEvents.startSession(firebaseApp, sessionDetails, sessionSettings, subscribers)
sessionCoordinator.attemptLoggingSessionEvent(sessionEvent)
} catch (ex: IllegalStateException) {
// This can happen if the app suddenly deletes the instance of FirebaseApp.
Log.i(TAG, "Sessions SDK failed to log Session Start event.", ex)
}
}

/** Calculate whether we should sample events using [sessionSettings] data. */
Expand Down