Skip to content

Commit 82f0407

Browse files
Fix Broken Stream Close in writeRawValue (#60625)
Small oversight in #56078 that only showed up during backporting where a stream copy was turned from a non-closing to a closing one. Enhanced part of a test in this PR to make it show up in master also even though we practically never use this method with stream targets that actually close.
1 parent cb13743 commit 82f0407

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

libs/x-content/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public void writeRawField(String name, InputStream content, XContentType content
349349
} else {
350350
writeStartRaw(name);
351351
flush();
352-
Streams.copy(content, os, false);
352+
Streams.copy(content, os);
353353
writeEndRaw();
354354
}
355355
}
@@ -364,7 +364,7 @@ public void writeRawValue(InputStream stream, XContentType xContentType) throws
364364
generator.writeRaw(':');
365365
}
366366
flush();
367-
Streams.copy(stream, os);
367+
Streams.copy(stream, os, false);
368368
writeEndRaw();
369369
}
370370
}

server/src/test/java/org/elasticsearch/common/xcontent/BaseXContentTestCase.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import java.util.Locale;
7676
import java.util.Map;
7777
import java.util.concurrent.TimeUnit;
78+
import java.util.concurrent.atomic.AtomicBoolean;
7879

7980
import static java.util.Collections.emptyMap;
8081
import static java.util.Collections.singletonMap;
@@ -947,11 +948,18 @@ void doTestRawValue(XContent source) throws Exception {
947948
assertNull(parser.nextToken());
948949
}
949950

950-
os = new ByteArrayOutputStream();
951+
final AtomicBoolean closed = new AtomicBoolean(false);
952+
os = new ByteArrayOutputStream() {
953+
@Override
954+
public void close() {
955+
closed.set(true);
956+
}
957+
};
951958
try (XContentGenerator generator = xcontentType().xContent().createGenerator(os)) {
952959
generator.writeStartObject();
953960
generator.writeFieldName("test");
954961
generator.writeRawValue(new BytesArray(rawData).streamInput(), source.type());
962+
assertFalse("Generator should not have closed the output stream", closed.get());
955963
generator.writeEndObject();
956964
}
957965

0 commit comments

Comments
 (0)