Skip to content

Commit cc44f43

Browse files
authored
Merge pull request #348 from code-dot-org/molly/exit-on-memory-outage
Force system exit on out of memory error
2 parents 32d5055 + 292077f commit cc44f43

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

org-code-javabuilder/lib/src/main/java/org/code/javabuilder/ExceptionHandler.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ public void handle(Throwable e) {
3232
return;
3333
}
3434

35+
// We should also exit on an OutOfMemoryError.
36+
// This means something is wrong with our memory usage
37+
// and is not likely to be recoverable.
38+
if (e instanceof OutOfMemoryError) {
39+
final InternalServerException error =
40+
new InternalServerException(InternalExceptionKey.UNKNOWN_ERROR, e);
41+
LoggerUtils.logSevereError(error);
42+
LambdaUtils.safelySendMessage(this.outputAdapter, error.getExceptionMessage(), true);
43+
this.systemExitHelper.exit(LambdaErrorCodes.OUT_OF_MEMORY_ERROR_CODE);
44+
return;
45+
}
46+
3547
// Internal server exceptions are caused by us (essentially an HTTP 5xx error). Log and notify
3648
// the user.
3749
if (e instanceof InternalServerException || e instanceof InternalServerRuntimeException) {

org-code-javabuilder/lib/src/main/java/org/code/javabuilder/LambdaErrorCodes.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ private LambdaErrorCodes() {
77

88
public static final int TEMP_DIRECTORY_CLEANUP_ERROR_CODE = 10;
99
public static final int LOW_DISK_SPACE_ERROR_CODE = 50;
10+
public static final int OUT_OF_MEMORY_ERROR_CODE = 60;
1011
}

org-code-javabuilder/lib/src/main/java/org/code/javabuilder/LambdaRequestHandler.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ public class LambdaRequestHandler implements RequestHandler<Map<String, String>,
4343
private static final int TIMEOUT_WARNING_MS = 20000;
4444
private static final int TIMEOUT_CLEANUP_BUFFER_MS = 5000;
4545
private static final String LAMBDA_ID = UUID.randomUUID().toString();
46-
// This is an error code we made up to signal that available disk space is less than 50%.
47-
// It may be used in tracking on Cloud Watch.
48-
private static final int LOW_DISK_SPACE_ERROR_CODE = 50;
4946
private static final String CONTENT_BUCKET_NAME = System.getenv("CONTENT_BUCKET_NAME");
5047
private static final String CONTENT_BUCKET_URL = System.getenv("CONTENT_BUCKET_URL");
5148
private static final String API_ENDPOINT = System.getenv("API_ENDPOINT");
@@ -314,7 +311,7 @@ private void shutDown(
314311
if ((double) f.getUsableSpace() / f.getTotalSpace() < 0.5) {
315312
// The current project holds a lock on too many resources. Force the JVM to quit in
316313
// order to release the resources for the next use of the container.
317-
System.exit(LOW_DISK_SPACE_ERROR_CODE);
314+
System.exit(LambdaErrorCodes.LOW_DISK_SPACE_ERROR_CODE);
318315
}
319316

320317
this.isSessionInitialized = false;

org-code-javabuilder/protocol/src/main/java/org/code/protocol/LoggerUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ public static void logTrackingException(Throwable e) {
108108
Logger.getLogger(MAIN_LOGGER).warning(eventData.toString());
109109
}
110110

111+
public static void logInfo(String info) {
112+
Logger.getLogger(MAIN_LOGGER).info(info);
113+
}
114+
111115
private static void sendDiskSpaceLogs(String type) {
112116
File f = Paths.get(System.getProperty("java.io.tmpdir")).toFile();
113117
JSONObject eventData = new JSONObject();

0 commit comments

Comments
 (0)