Skip to content

Commit

Permalink
Use a real out of memory error in die with dignity (#77039)
Browse files Browse the repository at this point in the history
Today when testing dying with dignity, we simply throw an
OutOfMemoryError. We know this should not get caught by any intermediate
code and end up in the uncaught exception handler. This allows us to
test that this exception handler is able to successfully kill the
VM. However, it is on the table to no longer use the uncaught exception
handler, but instead the built-in support for ExitOnOutOfMemoryError. A
fake OutOfMemoryError would not be processed by this handler, so to
prepare the way, we switch to using a real OutOfMemoryError.
  • Loading branch information
jasontedor authored Sep 1, 2021
1 parent 429beba commit 3c589ef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void testDieWithDignity() throws Exception {
".*ElasticsearchUncaughtExceptionHandler.*",
".*javaRestTest-0.*",
".*fatal error in thread \\[Thread-\\d+\\], exiting.*",
".*java.lang.OutOfMemoryError: die with dignity.*"
".*java.lang.OutOfMemoryError: Requested array size exceeds VM limit.*"
)) {
fatalErrorInThreadExiting = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
package org.elasticsearch;

import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Randomness;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestStatus;

import java.util.List;

Expand All @@ -32,7 +36,23 @@ public String getName() {

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
throw new OutOfMemoryError("die with dignity");
return channel -> {
/*
* This is to force the size of the array to be non-deterministic so that a sufficiently smart compiler can not optimize away
* getting the length of the array to a constant.
*/
final int length = Randomness.get().nextBoolean() ? Integer.MAX_VALUE - 1 : Integer.MAX_VALUE;
final long[] array = new long[length];
// this is to force the array to be consumed so that it can not be optimized away by a sufficiently smart compiler
try (XContentBuilder builder = channel.newBuilder()) {
builder.startObject();
{
builder.field("length", array.length);
}
builder.endObject();
channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder));
}
};
}

}

0 comments on commit 3c589ef

Please sign in to comment.