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
This commit is contained in:
Elliott Hughes
2021-05-19 14:33:09 -07:00
parent c4099ac074
commit 0023356faa

View File

@@ -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);
}
}