From 6bbeedc932121d953c769451f8987ffa8a31ef9f Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Tue, 1 Dec 2020 02:44:36 +0100 Subject: [PATCH] Reset Deflater/Inflater after Use in DeflateCompressor (#65617) (#65646) We should reset after use, not before reuse. Otherwise we keep the input buffers on these objects around for a long time and they can grow to O(MB). --- .../elasticsearch/common/compress/DeflateCompressor.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/compress/DeflateCompressor.java b/server/src/main/java/org/elasticsearch/common/compress/DeflateCompressor.java index f3e229b3496..411198e9307 100644 --- a/server/src/main/java/org/elasticsearch/common/compress/DeflateCompressor.java +++ b/server/src/main/java/org/elasticsearch/common/compress/DeflateCompressor.java @@ -229,9 +229,10 @@ public class DeflateCompressor implements Compressor { public BytesReference uncompress(BytesReference bytesReference) throws IOException { final BytesStreamOutput buffer = baos.get(); final Inflater inflater = inflaterRef.get(); - inflater.reset(); try (InflaterOutputStream ios = new InflaterOutputStream(buffer, inflater)) { bytesReference.slice(HEADER.length, bytesReference.length() - HEADER.length).writeTo(ios); + } finally { + inflater.reset(); } final BytesReference res = buffer.copyBytes(); buffer.reset(); @@ -245,11 +246,12 @@ public class DeflateCompressor implements Compressor { @Override public BytesReference compress(BytesReference bytesReference) throws IOException { final BytesStreamOutput buffer = baos.get(); - final Deflater deflater = deflaterRef.get(); - deflater.reset(); buffer.write(HEADER); + final Deflater deflater = deflaterRef.get(); try (DeflaterOutputStream dos = new DeflaterOutputStream(buffer, deflater, true)) { bytesReference.writeTo(dos); + } finally { + deflater.reset(); } final BytesReference res = buffer.copyBytes(); buffer.reset();