Skip to content

Commit a8be0a5

Browse files
committed
Cat APIs should not close the stream obtained from the channel
The cat APIs and rest tables would obtain a stream from the RestChannel, which happened to be a ReleasableBytesStreamOutput. These APIs used the stream to write content to, closed the stream, and then tried to send a response. After #23941 was merged, closing the stream meant that the bytes were released for use elsewhere. This caused occasional corruption of the response when the bytes were used prior to the response being sent. This commit changes these two usages to wrap the stream obtained from the channel in a flush on close stream so that the bytes are still reserved until the message is sent.
1 parent 51db6ab commit a8be0a5

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

core/src/main/java/org/elasticsearch/rest/action/cat/AbstractCatAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020

2121
import org.elasticsearch.client.node.NodeClient;
2222
import org.elasticsearch.common.Table;
23+
import org.elasticsearch.common.io.Streams;
2324
import org.elasticsearch.common.io.UTF8StreamWriter;
24-
import org.elasticsearch.common.io.stream.BytesStreamOutput;
25+
import org.elasticsearch.common.io.stream.BytesStream;
2526
import org.elasticsearch.common.settings.Settings;
2627
import org.elasticsearch.rest.BaseRestHandler;
2728
import org.elasticsearch.rest.BytesRestResponse;
@@ -56,7 +57,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
5657
return channel -> {
5758
Table table = getTableWithHeader(request);
5859
int[] width = buildHelpWidths(table, request);
59-
BytesStreamOutput bytesOutput = channel.bytesOutput();
60+
BytesStream bytesOutput = Streams.flushOnCloseStream(channel.bytesOutput());
6061
UTF8StreamWriter out = new UTF8StreamWriter().setOutput(bytesOutput);
6162
for (Table.Cell cell : table.getHeaders()) {
6263
// need to do left-align always, so create new cells

core/src/main/java/org/elasticsearch/rest/action/cat/RestTable.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
import org.elasticsearch.common.Booleans;
2323
import org.elasticsearch.common.Strings;
2424
import org.elasticsearch.common.Table;
25+
import org.elasticsearch.common.io.Streams;
2526
import org.elasticsearch.common.io.UTF8StreamWriter;
26-
import org.elasticsearch.common.io.stream.BytesStreamOutput;
27+
import org.elasticsearch.common.io.stream.BytesStream;
2728
import org.elasticsearch.common.regex.Regex;
2829
import org.elasticsearch.common.unit.ByteSizeValue;
2930
import org.elasticsearch.common.unit.SizeValue;
@@ -82,7 +83,7 @@ public static RestResponse buildTextPlainResponse(Table table, RestChannel chann
8283
List<DisplayHeader> headers = buildDisplayHeaders(table, request);
8384
int[] width = buildWidths(table, request, verbose, headers);
8485

85-
BytesStreamOutput bytesOut = channel.bytesOutput();
86+
BytesStream bytesOut = Streams.flushOnCloseStream(channel.bytesOutput());
8687
UTF8StreamWriter out = new UTF8StreamWriter().setOutput(bytesOut);
8788
int lastHeader = headers.size() - 1;
8889
if (verbose) {

0 commit comments

Comments
 (0)