Skip to content

Commit

Permalink
handle oom error as IOException
Browse files Browse the repository at this point in the history
  • Loading branch information
themiswang committed Oct 21, 2024
1 parent b6d0107 commit c2b80cd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public void writeUserData(String sessionId, String userId) {
writer.flush();
} catch (Exception e) {
Logger.getLogger().w("Error serializing user metadata.", e);
} catch (OutOfMemoryError e) {
Logger.getLogger().w("OOM serializing user metadata.", e);
} finally {
CommonUtils.closeOrLog(writer, "Failed to close user metadata file.");
}
Expand All @@ -86,6 +88,9 @@ public String readUserId(String sessionId) {
} catch (Exception e) {
Logger.getLogger().w("Error deserializing user metadata.", e);
safeDeleteCorruptFile(f);
} catch (OutOfMemoryError e) {
Logger.getLogger().w("OOM deserializing user metadata.", e);
safeDeleteCorruptFile(f);
} finally {
CommonUtils.closeOrLog(is, "Failed to close user metadata file.");
}
Expand All @@ -108,6 +113,9 @@ public void writeKeyData(String sessionId, Map<String, String> keyData, boolean
} catch (Exception e) {
Logger.getLogger().w("Error serializing key/value metadata.", e);
safeDeleteCorruptFile(f);
} catch (OutOfMemoryError e) {
Logger.getLogger().w("OOM serializing key/value metadata.", e);
safeDeleteCorruptFile(f);
} finally {
CommonUtils.closeOrLog(writer, "Failed to close key/value metadata file.");
}
Expand All @@ -132,6 +140,9 @@ Map<String, String> readKeyData(String sessionId, boolean isInternal) {
} catch (Exception e) {
Logger.getLogger().w("Error deserializing user metadata.", e);
safeDeleteCorruptFile(f);
} catch (OutOfMemoryError e) {
Logger.getLogger().w("OOM deserializing user metadata.", e);
safeDeleteCorruptFile(f);
} finally {
CommonUtils.closeOrLog(is, "Failed to close user metadata file.");
}
Expand All @@ -155,6 +166,9 @@ public List<RolloutAssignment> readRolloutsState(String sessionId) {
} catch (Exception e) {
Logger.getLogger().w("Error deserializing rollouts state.", e);
safeDeleteCorruptFile(f);
} catch (OutOfMemoryError e) {
Logger.getLogger().w("OOM deserializing rollouts state.", e);
safeDeleteCorruptFile(f);
} finally {
CommonUtils.closeOrLog(is, "Failed to close rollouts state file.");
}
Expand All @@ -177,6 +191,9 @@ public void writeRolloutState(String sessionId, List<RolloutAssignment> rollouts
} catch (Exception e) {
Logger.getLogger().w("Error serializing rollouts state.", e);
safeDeleteCorruptFile(f);
} catch (OutOfMemoryError e) {
Logger.getLogger().w("OOM serializing rollouts state.", e);
safeDeleteCorruptFile(f);
} finally {
CommonUtils.closeOrLog(writer, "Failed to close rollouts state file.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ private void doWriteToLog(long timestamp, String msg) {
}
} catch (IOException e) {
Logger.getLogger().e("There was a problem writing to the Crashlytics log.", e);
} catch (OutOfMemoryError e) {
Logger.getLogger().e("OOM when writing to the Crashlytics log.", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,10 @@ private static int oldestEventFileFirst(@NonNull File f1, @NonNull File f2) {
private static void writeTextFile(File file, String text) throws IOException {
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), UTF_8)) {
writer.write(text);
} catch (OutOfMemoryError e) {
Logger.getLogger()
.w("Write text file out of memory.", e);
throw new IOException("out of memory");
}
}

Expand All @@ -421,6 +425,10 @@ private static void writeTextFile(File file, String text, long lastModifiedTimes
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), UTF_8)) {
writer.write(text);
file.setLastModified(convertTimestampFromSecondsToMs(lastModifiedTimestampSeconds));
} catch (OutOfMemoryError e) {
Logger.getLogger()
.w("Write text file out of memory.", e);
throw new IOException("out of memory");
}
}

Expand All @@ -434,6 +442,10 @@ private static String readTextFile(@NonNull File file) throws IOException {
bos.write(readBuffer, 0, read);
}
return new String(bos.toByteArray(), UTF_8);
} catch (OutOfMemoryError e) {
Logger.getLogger()
.w("Read text file out of memory.", e);
throw new IOException("out of memory");
}
}

Expand Down

0 comments on commit c2b80cd

Please sign in to comment.