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 3 commits
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,26 @@
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(
"FirebaseApp is not initialized. Firebase Performance will not be collecting any "
+ "performance metrics until initialized. %s",
ex);
}
}
}
4 changes: 4 additions & 0 deletions firebase-sessions/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

* [fixed] Made Sessions more resilient to the FirebaseApp instance being deleted.

# 1.0.1

* [fixed] Fixed NPE when no version name is
set ([#5195](//github.com/firebase/firebase-android-sdk/issues/5195)).
* [fixed] Populate DataCollectionStatus fields for Crashlytics and Perf.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ internal constructor(
val appContext = firebaseApp.applicationContext.applicationContext
if (appContext is Application) {
appContext.registerActivityLifecycleCallbacks(sessionInitiator.activityLifecycleCallbacks)

firebaseApp.addLifecycleEventListener { _, _ ->
Log.w(TAG, "FirebaseApp instance deleted. Sessions library will not collect session data.")
appContext.unregisterActivityLifecycleCallbacks(sessionInitiator.activityLifecycleCallbacks)
}
} else {
Log.e(
TAG,
Expand Down Expand Up @@ -138,9 +143,18 @@ 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,
"FirebaseApp is not initialized. Sessions library will not collect session data.",
ex
)
}
}

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