Skip to content

Make a best effort attempt to flush reports at crash time #4112

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 4 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -134,6 +134,29 @@ public static <T> T awaitEvenIfOnMainThread(Task<T> task)
}
}

/** Invokes latch.await(timeout, unit) uninterruptibly. */
public static boolean awaitUninterruptibly(CountDownLatch latch, long timeout, TimeUnit unit) {
boolean interrupted = false;
try {
long remainingNanos = unit.toNanos(timeout);
long end = System.nanoTime() + remainingNanos;

while (true) {
try {
// CountDownLatch treats negative timeouts just like zero.
return latch.await(remainingNanos, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
interrupted = true;
remainingNanos = end - System.nanoTime();
}
}
} finally {
if (interrupted) {
Thread.currentThread().interrupt();
}
}
}

/**
* ExecutorService that is used exclusively by the awaitEvenIfOnMainThread function. If the
* Continuation which counts down the latch is called on the same thread which is waiting on the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@

package com.google.firebase.crashlytics.internal.send;

import android.annotation.SuppressLint;
import com.google.android.datatransport.Event;
import com.google.android.datatransport.Priority;
import com.google.android.datatransport.Transport;
import com.google.android.datatransport.runtime.ForcedSender;
import com.google.android.gms.tasks.TaskCompletionSource;
import com.google.firebase.crashlytics.internal.Logger;
import com.google.firebase.crashlytics.internal.common.CrashlyticsReportWithSessionId;
import com.google.firebase.crashlytics.internal.common.OnDemandCounter;
import com.google.firebase.crashlytics.internal.common.Utils;
import com.google.firebase.crashlytics.internal.model.CrashlyticsReport;
import com.google.firebase.crashlytics.internal.settings.Settings;
import java.util.Locale;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -115,6 +120,18 @@ TaskCompletionSource<CrashlyticsReportWithSessionId> enqueueReport(
}
}

@SuppressLint("DiscouragedApi") // best effort only
public void flushScheduledReportsIfAble() {
CountDownLatch latch = new CountDownLatch(1);
new Thread(
() -> {
ForcedSender.sendBlockingWithPriority(transport, Priority.HIGHEST);
latch.countDown();
})
.start();
Utils.awaitUninterruptibly(latch, 2, TimeUnit.SECONDS);
}

/** Send the report to Crashlytics through Google DataTransport. */
private void sendReport(
CrashlyticsReportWithSessionId reportWithSessionId,
Expand All @@ -128,6 +145,7 @@ private void sendReport(
tcs.trySetException(error);
return;
}
flushScheduledReportsIfAble();
tcs.trySetResult(reportWithSessionId);
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.google.android.datatransport.runtime;

import android.annotation.SuppressLint;
import androidx.annotation.Discouraged;
import androidx.annotation.WorkerThread;
import com.google.android.datatransport.Priority;
import com.google.android.datatransport.Transport;

@Discouraged(
message =
"TransportRuntime is not a realtime delivery system, don't use unless you absolutely must.")
public final class ForcedSender {
@WorkerThread
public static void sendBlockingWithPriority(Transport<?> transport, Priority priority) {
@SuppressLint("DiscouragedApi")
TransportContext context = getTransportContextOrThrow(transport).withPriority(priority);
TransportRuntime.getInstance().getUploader().upload(context, 1, () -> {});
}

private static TransportContext getTransportContextOrThrow(Transport<?> transport) {
if (transport instanceof TransportImpl) {
return ((TransportImpl<?>) transport).getTransportContext();
}
throw new IllegalArgumentException("Expected instance of TransportImpl.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ public void schedule(Event<T> event, TransportScheduleCallback callback) {
.build(),
callback);
}

TransportContext getTransportContext() {
return transportContext;
}
}
4 changes: 2 additions & 2 deletions transport/transport-runtime/transport-runtime.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ thirdPartyLicenses {

dependencies {
implementation project(':transport:transport-api')
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.annotation:annotation:1.3.0'
implementation 'javax.inject:javax.inject:1'
implementation project(":encoders:firebase-encoders")
implementation project(":encoders:firebase-encoders-proto")
Expand Down Expand Up @@ -132,4 +132,4 @@ dependencies {
androidTestImplementation 'org.mockito:mockito-android:2.25.0'

androidTestAnnotationProcessor 'com.google.dagger:dagger-compiler:2.27'
}
}