Skip to content

Commit 6e7e891

Browse files
authored
ensure the XContentBuilder is always closed in RestBuilderListener
There may be cases where the XContentBuilder is not used and therefore it never gets closed, which can cause a leak of bytes. This change moves the creation of the builder into a try with resources block and adds an assertion to verify that we always consume the bytes in our code; the try-with resources provides protections against memory leaks caused by plugins, which do not test this.
1 parent ef192ff commit 6e7e891

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

core/src/main/java/org/elasticsearch/common/xcontent/XContentGenerator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,9 @@ public interface XContentGenerator extends Closeable, Flushable {
9494

9595
void copyCurrentStructure(XContentParser parser) throws IOException;
9696

97+
/**
98+
* Returns {@code true} if this XContentGenerator has been closed. A closed generator can not do any more output.
99+
*/
100+
boolean isClosed();
101+
97102
}

core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,4 +419,8 @@ public void close() throws IOException {
419419
generator.close();
420420
}
421421

422+
@Override
423+
public boolean isClosed() {
424+
return generator.isClosed();
425+
}
422426
}

core/src/main/java/org/elasticsearch/rest/action/RestBuilderListener.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,22 @@ public RestBuilderListener(RestChannel channel) {
3434

3535
@Override
3636
public final RestResponse buildResponse(Response response) throws Exception {
37-
return buildResponse(response, channel.newBuilder());
37+
try (XContentBuilder builder = channel.newBuilder()) {
38+
final RestResponse restResponse = buildResponse(response, builder);
39+
assert assertBuilderClosed(builder);
40+
return restResponse;
41+
}
3842
}
3943

4044
/**
41-
* Builds a response to send back over the channel.
45+
* Builds a response to send back over the channel. Implementors should ensure that they close the provided {@link XContentBuilder}
46+
* using the {@link XContentBuilder#close()} method.
4247
*/
4348
public abstract RestResponse buildResponse(Response response, XContentBuilder builder) throws Exception;
49+
50+
// pkg private method that we can override for testing
51+
boolean assertBuilderClosed(XContentBuilder xContentBuilder) {
52+
assert xContentBuilder.generator().isClosed() : "callers should ensure the XContentBuilder is closed themselves";
53+
return true;
54+
}
4455
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.rest.action;
21+
22+
import org.elasticsearch.common.bytes.BytesArray;
23+
import org.elasticsearch.common.xcontent.XContentBuilder;
24+
import org.elasticsearch.rest.BytesRestResponse;
25+
import org.elasticsearch.rest.RestResponse;
26+
import org.elasticsearch.rest.RestStatus;
27+
import org.elasticsearch.test.ESTestCase;
28+
import org.elasticsearch.test.rest.FakeRestChannel;
29+
import org.elasticsearch.test.rest.FakeRestRequest;
30+
import org.elasticsearch.transport.TransportResponse;
31+
import org.elasticsearch.transport.TransportResponse.Empty;
32+
33+
import java.util.concurrent.atomic.AtomicReference;
34+
35+
public class RestBuilderListenerTests extends ESTestCase {
36+
37+
public void testXContentBuilderClosedInBuildResponse() throws Exception {
38+
AtomicReference<XContentBuilder> builderAtomicReference = new AtomicReference<>();
39+
RestBuilderListener<TransportResponse.Empty> builderListener =
40+
new RestBuilderListener<Empty>(new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)) {
41+
@Override
42+
public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception {
43+
builderAtomicReference.set(builder);
44+
builder.close();
45+
return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
46+
}
47+
};
48+
49+
builderListener.buildResponse(Empty.INSTANCE);
50+
assertNotNull(builderAtomicReference.get());
51+
assertTrue(builderAtomicReference.get().generator().isClosed());
52+
}
53+
54+
public void testXContentBuilderNotClosedInBuildResponseAssertionsDisabled() throws Exception {
55+
AtomicReference<XContentBuilder> builderAtomicReference = new AtomicReference<>();
56+
RestBuilderListener<TransportResponse.Empty> builderListener =
57+
new RestBuilderListener<Empty>(new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)) {
58+
@Override
59+
public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception {
60+
builderAtomicReference.set(builder);
61+
return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
62+
}
63+
64+
@Override
65+
boolean assertBuilderClosed(XContentBuilder xContentBuilder) {
66+
// don't check the actual builder being closed so we can test auto close
67+
return true;
68+
}
69+
};
70+
71+
builderListener.buildResponse(Empty.INSTANCE);
72+
assertNotNull(builderAtomicReference.get());
73+
assertTrue(builderAtomicReference.get().generator().isClosed());
74+
}
75+
76+
public void testXContentBuilderNotClosedInBuildResponseAssertionsEnabled() throws Exception {
77+
assumeTrue("tests are not being run with assertions", RestBuilderListener.class.desiredAssertionStatus());
78+
79+
RestBuilderListener<TransportResponse.Empty> builderListener =
80+
new RestBuilderListener<Empty>(new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)) {
81+
@Override
82+
public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception {
83+
return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
84+
}
85+
};
86+
87+
AssertionError error = expectThrows(AssertionError.class, () -> builderListener.buildResponse(Empty.INSTANCE));
88+
assertEquals("callers should ensure the XContentBuilder is closed themselves", error.getMessage());
89+
}
90+
}

0 commit comments

Comments
 (0)