Skip to content

Commit

Permalink
fix: Cancel crash reports after flushing startup crashes
Browse files Browse the repository at this point in the history
Internally FileStore retains a Collection of files which have previously been found for a request,
to prevent duplicate reports being sent. This puts the onus on the calling code to cancel or delete
the files once the request has been completed. This change ensures that the storedFiles are only
requested once, then filtered for crashes on launch, and then all files are cancelled after a
reasonable amount of time has passed regardless of delivery, which reset the internal FileStore
state. flushAsync is then called which will flush any remaining reports that have not yet been
delivered
  • Loading branch information
fractalwrench committed May 24, 2018
1 parent ed4d196 commit 955ea7f
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions sdk/src/main/java/com/bugsnag/android/ErrorStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ public int compare(File lhs, File rhs) {
}

void flushOnLaunch() {
final List<File> crashReports = findLaunchCrashReports();
List<File> storedFiles = findStoredFiles();
final List<File> crashReports = findLaunchCrashReports(storedFiles);

if (crashReports.isEmpty() || config.getLaunchCrashThresholdMs() == 0) {
flushAsync(); // if disabled or no startup crash, flush async
} else {
if (!crashReports.isEmpty() && config.getLaunchCrashThresholdMs() != 0) {

// Block the main thread for a 2 second interval as the app may crash very soon.
// The request itself will run in a background thread and will continue after the 2
Expand Down Expand Up @@ -83,6 +82,9 @@ public void run() {
}
Logger.info("Continuing with Bugsnag initialisation");
}

cancelQueuedFiles(storedFiles); // cancel all previously found files
flushAsync(); // flush any remaining errors async that weren't delivered
}

/**
Expand Down Expand Up @@ -130,7 +132,7 @@ private void flushErrorReport(File errorFile) {
} catch (DeliveryFailureException exception) {
cancelQueuedFiles(Collections.singleton(errorFile));
Logger.warn("Could not send previously saved error(s)"
+ " to Bugsnag, will try again later", exception);
+ " to Bugsnag, will try again later", exception);
} catch (Exception exception) {
deleteStoredFiles(Collections.singleton(errorFile));
Logger.warn("Problem sending unsent error from disk", exception);
Expand All @@ -141,8 +143,7 @@ boolean isLaunchCrashReport(File file) {
return file.getName().endsWith("_startupcrash.json");
}

private List<File> findLaunchCrashReports() {
Collection<File> storedFiles = findStoredFiles();
private List<File> findLaunchCrashReports(Collection<File> storedFiles) {
List<File> launchCrashes = new ArrayList<>();

for (File file : storedFiles) {
Expand Down

0 comments on commit 955ea7f

Please sign in to comment.