Skip to content

Add a precondition check for not main thread #6204

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 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -104,7 +104,6 @@ private static String formatId(@NonNull String id) {
@Override
@NonNull
public synchronized InstallIds getInstallIds() {
CrashlyticsWorkers.checkBackgroundThread();
if (!shouldRefresh()) {
return installIds;
}
Expand Down Expand Up @@ -183,7 +182,7 @@ private String readCachedCrashlyticsInstallId(SharedPreferences prefs) {
*/
@NonNull
public FirebaseInstallationId fetchTrueFid(boolean validate) {
CrashlyticsWorkers.checkBackgroundThread(); // This fetch blocks, never do it on main.
CrashlyticsWorkers.checkNotMainThread(); // This fetch blocks, never do it on main.
String fid = null;
String authToken = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.firebase.crashlytics.internal.concurrency

import android.os.Build
import android.os.Looper
import com.google.firebase.crashlytics.internal.Logger
import java.util.concurrent.ExecutorService

Expand Down Expand Up @@ -59,6 +61,12 @@ class CrashlyticsWorkers(
/** When enabled, failed preconditions will cause assertion errors for debugging. */
@JvmStatic var enforcement: Boolean = false

@JvmStatic
fun checkNotMainThread() =
checkThread(::isNotMainThread) {
"Must not be called on a main thread, was called on $threadName."
}

@JvmStatic
fun checkBlockingThread() =
checkThread(::isBlockingThread) {
Expand All @@ -71,6 +79,13 @@ class CrashlyticsWorkers(
"Must be called on a background thread, was called on $threadName."
}

private fun isNotMainThread() =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
!Looper.getMainLooper().isCurrentThread
} else {
Looper.getMainLooper() != Looper.myLooper()
}

private fun isBlockingThread() = threadName.contains("Firebase Blocking Thread #")

private fun isBackgroundThread() = threadName.contains("Firebase Background Thread #")
Expand Down
Loading