From 0023356faa09dafd430a338645fdf877f41df0b5 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 19 May 2021 14:33:09 -0700 Subject: [PATCH] maybePruneOldTraces: bail if modification times change. We could implement the right thing easily with streams, but we're not allowed to use streams in this context, and it doesn't seem worth the bother of doing it any of the hard ways, given that we've only seen this once in the year the code's been doing this. Bug: https://issuetracker.google.com/169836837 Test: treehugger Change-Id: I4ee29916c1b335bebecfe7f72e74ff71e8a24228 --- .../server/am/ActivityManagerService.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index 5122be2b10c4b..011a3568aff96 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -4060,13 +4060,19 @@ public class ActivityManagerService extends IActivityManager.Stub final int max = SystemProperties.getInt("tombstoned.max_anr_count", 64); final long now = System.currentTimeMillis(); - Arrays.sort(files, Comparator.comparingLong(File::lastModified).reversed()); - for (int i = 0; i < files.length; ++i) { - if (i > max || (now - files[i].lastModified()) > DAY_IN_MILLIS) { - if (!files[i].delete()) { - Slog.w(TAG, "Unable to prune stale trace file: " + files[i]); + try { + Arrays.sort(files, Comparator.comparingLong(File::lastModified).reversed()); + for (int i = 0; i < files.length; ++i) { + if (i > max || (now - files[i].lastModified()) > DAY_IN_MILLIS) { + if (!files[i].delete()) { + Slog.w(TAG, "Unable to prune stale trace file: " + files[i]); + } } } + } catch (IllegalArgumentException e) { + // The modification times changed while we were sorting. Bail... + // https://issuetracker.google.com/169836837 + Slog.w(TAG, "tombstone modification times changed while sorting; not pruning", e); } }