diff --git a/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/DefaultStreamingDecoder.java b/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/DefaultStreamingDecoder.java index 6d341783c0a..f4696475779 100644 --- a/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/DefaultStreamingDecoder.java +++ b/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/DefaultStreamingDecoder.java @@ -38,7 +38,11 @@ public void request(int numMessages) { @Override public void decode(InputStream inputStream) throws DecodeException { if (closed) { - // ignored + try { + inputStream.close(); + } catch (IOException e) { + throw new DecodeException(e); + } return; } accumulate.addInputStream(inputStream); diff --git a/dubbo-remoting/dubbo-remoting-http12/src/test/java/org/apache/dubbo/remoting/http12/message/DefaultStreamingDecoderTest.java b/dubbo-remoting/dubbo-remoting-http12/src/test/java/org/apache/dubbo/remoting/http12/message/DefaultStreamingDecoderTest.java new file mode 100644 index 00000000000..5dd18382821 --- /dev/null +++ b/dubbo-remoting/dubbo-remoting-http12/src/test/java/org/apache/dubbo/remoting/http12/message/DefaultStreamingDecoderTest.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.remoting.http12.message; + +import org.apache.dubbo.remoting.http12.exception.DecodeException; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class DefaultStreamingDecoderTest { + + @Test + void closesInputReceivedAfterStreamClosed() { + DefaultStreamingDecoder decoder = new DefaultStreamingDecoder(); + decoder.onStreamClosed(); + CloseTrackingInputStream inputStream = new CloseTrackingInputStream(new byte[] {0}); + + decoder.decode(inputStream); + + assertTrue(inputStream.closed); + } + + @Test + void closesInputReceivedAfterDecoderClosed() { + DefaultStreamingDecoder decoder = new DefaultStreamingDecoder(); + decoder.close(); + CloseTrackingInputStream inputStream = new CloseTrackingInputStream(new byte[] {0}); + + decoder.decode(inputStream); + + assertTrue(inputStream.closed); + } + + @Test + void propagatesCloseFailureAsDecodeException() { + DefaultStreamingDecoder decoder = new DefaultStreamingDecoder(); + decoder.onStreamClosed(); + InputStream failingStream = new InputStream() { + @Override + public int read() throws IOException { + return -1; + } + + @Override + public void close() throws IOException { + throw new IOException("close failed"); + } + }; + + assertThrows(DecodeException.class, () -> decoder.decode(failingStream)); + } + + private static final class CloseTrackingInputStream extends ByteArrayInputStream { + + private boolean closed; + + private CloseTrackingInputStream(byte[] buf) { + super(buf); + } + + @Override + public void close() throws IOException { + this.closed = true; + super.close(); + } + } +}