diff --git a/lib/java/src/main/java/org/apache/thrift/transport/TSocket.java b/lib/java/src/main/java/org/apache/thrift/transport/TSocket.java index 2458d0f95d3..e6f3cbd62b5 100644 --- a/lib/java/src/main/java/org/apache/thrift/transport/TSocket.java +++ b/lib/java/src/main/java/org/apache/thrift/transport/TSocket.java @@ -233,19 +233,27 @@ public void open() throws TTransportException { } } - /** Closes the socket. */ + /** + * Closes the socket and discards any unflushed output. + * + *
Close the socket directly so that a blocked read or write can be interrupted without waiting + * for a buffered stream's lock. Call flush() before close() when pending output must be sent. + */ public void close() { - // Close the underlying streams - super.close(); - - // Close the socket - if (socket_ != null) { - try { - socket_.close(); - } catch (IOException iox) { - LOGGER.warn("Could not close socket.", iox); + Socket socket = socket_; + try { + if (socket != null) { + try { + socket.close(); + } catch (IOException iox) { + LOGGER.warn("Could not close socket.", iox); + } + socket_ = null; } - socket_ = null; + } finally { + // The socket owns its streams; the buffering wrappers have no separate resources to release. + inputStream_ = null; + outputStream_ = null; } } diff --git a/lib/java/src/main/java/org/apache/thrift/transport/TZlibTransport.java b/lib/java/src/main/java/org/apache/thrift/transport/TZlibTransport.java index 62a5ca5ae9a..97c78e1f662 100644 --- a/lib/java/src/main/java/org/apache/thrift/transport/TZlibTransport.java +++ b/lib/java/src/main/java/org/apache/thrift/transport/TZlibTransport.java @@ -89,11 +89,36 @@ public void open() throws TTransportException { transport_.open(); } + /** + * Finishes compression, flushes the output, and closes the underlying transport. + * + *
This can block while sending the compressed output. To abort pending I/O, close the + * underlying transport directly. + * + *
If finishing the output and closing the underlying transport both fail unchecked, the
+ * cleanup failure is suppressed on the first failure.
+ */
@Override
public void close() {
- super.close();
- if (transport_.isOpen()) {
- transport_.close();
+ Throwable primaryFailure = null;
+ try {
+ super.close();
+ } catch (RuntimeException | Error failure) {
+ primaryFailure = failure;
+ throw failure;
+ } finally {
+ try {
+ if (transport_.isOpen()) {
+ transport_.close();
+ }
+ } catch (RuntimeException | Error cleanupFailure) {
+ if (primaryFailure == null) {
+ throw cleanupFailure;
+ }
+ if (cleanupFailure != primaryFailure) {
+ primaryFailure.addSuppressed(cleanupFailure);
+ }
+ }
}
}
}
@@ -161,4 +186,11 @@ public void flush() throws IOException {
throw new IOException(e);
}
}
+
+ @Override
+ public void close() throws IOException {
+ // DeflaterOutputStream.close() finishes compression before closing this adapter. Flush the
+ // resulting trailer while the underlying transport is still open; its owner closes it later.
+ flush();
+ }
}
diff --git a/lib/java/src/test/java/org/apache/thrift/transport/TestTIOStreamTransport.java b/lib/java/src/test/java/org/apache/thrift/transport/TestTIOStreamTransport.java
index f5ae3ccad3d..4fb738f0658 100644
--- a/lib/java/src/test/java/org/apache/thrift/transport/TestTIOStreamTransport.java
+++ b/lib/java/src/test/java/org/apache/thrift/transport/TestTIOStreamTransport.java
@@ -20,14 +20,31 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.inOrder;
+import static org.mockito.Mockito.mock;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.junit.jupiter.api.Test;
+import org.mockito.InOrder;
public class TestTIOStreamTransport {
+ @Test
+ public void testCloseIndependentStreamsInOriginalOrder() throws Exception {
+ InputStream input = mock(InputStream.class);
+ OutputStream output = mock(OutputStream.class);
+ TIOStreamTransport transport = new TIOStreamTransport(input, output);
+
+ transport.close();
+
+ InOrder order = inOrder(input, output);
+ order.verify(input).close();
+ order.verify(output).close();
+ assertFalse(transport.isOpen());
+ }
+
// THRIFT-5022
@Test
public void testOpenClose_2streams() throws TTransportException {
diff --git a/lib/java/src/test/java/org/apache/thrift/transport/TestTSocket.java b/lib/java/src/test/java/org/apache/thrift/transport/TestTSocket.java
new file mode 100644
index 00000000000..8e40afd78f9
--- /dev/null
+++ b/lib/java/src/test/java/org/apache/thrift/transport/TestTSocket.java
@@ -0,0 +1,277 @@
+/*
+ * 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.thrift.transport;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.clearInvocations;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.net.SocketException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+public class TestTSocket {
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ public void testCloseDoesNotFlushOrCloseSocketStreams(boolean flushBeforeClose) throws Exception {
+ SocketStreams streams = new SocketStreams();
+ TSocket transport = new TSocket(streams.socket);
+ byte[] request = {1, 2, 3};
+ transport.write(request);
+ if (flushBeforeClose) {
+ transport.flush();
+ }
+ clearInvocations(streams.input, streams.output);
+
+ transport.close();
+
+ assertArrayEquals(flushBeforeClose ? request : new byte[0], streams.sent.toByteArray());
+ verifyNoInteractions(streams.input, streams.output);
+ verify(streams.socket).close();
+ assertTrue(streams.closed);
+ assertClosedAndIdempotent(transport, streams);
+ }
+
+ @Test
+ public void testCloseAfterExplicitFlushFailureStillClosesSocket() throws Exception {
+ SocketStreams streams = new SocketStreams();
+ TSocket transport = new TSocket(streams.socket);
+ IOException failure = new IOException("Output flush failure");
+ doThrow(failure).when(streams.output).flush();
+
+ assertSame(failure, assertThrows(TTransportException.class, transport::flush).getCause());
+ clearInvocations(streams.input, streams.output);
+ transport.close();
+
+ verifyNoInteractions(streams.input, streams.output);
+ assertTrue(streams.closed);
+ assertClosedAndIdempotent(transport, streams);
+ }
+
+ @Test
+ public void testCheckedSocketCloseFailureClearsReferences() throws Exception {
+ SocketStreams streams = new SocketStreams();
+ TSocket transport = new TSocket(streams.socket);
+ doThrow(new IOException("Socket close failure")).when(streams.socket).close();
+
+ transport.close();
+
+ verify(streams.socket).close();
+ assertClosedAndIdempotent(transport, streams);
+ }
+
+ @Test
+ public void testUncheckedSocketCloseFailureCanBeRetried() throws Exception {
+ SocketStreams streams = new SocketStreams();
+ TSocket transport = new TSocket(streams.socket);
+ IllegalStateException failure = new IllegalStateException("Socket close failure");
+ AtomicInteger attempts = new AtomicInteger();
+ doAnswer(
+ invocation -> {
+ if (attempts.getAndIncrement() == 0) {
+ throw failure;
+ }
+ streams.closed = true;
+ return null;
+ })
+ .when(streams.socket)
+ .close();
+
+ assertSame(failure, assertThrows(IllegalStateException.class, transport::close));
+ assertNull(transport.inputStream_);
+ assertNull(transport.outputStream_);
+
+ transport.close();
+
+ verify(streams.socket, times(2)).close();
+ assertTrue(streams.closed);
+ assertClosedAndIdempotent(transport, streams);
+ }
+
+ @Test
+ public void testCloseInterruptsBlockedWrite() throws Exception {
+ final int timeoutSeconds = 10;
+ SocketStreams streams = new SocketStreams();
+ TSocket transport = new TSocket(streams.socket);
+ CountDownLatch writeEntered = new CountDownLatch(1);
+ CountDownLatch socketClosed = new CountDownLatch(1);
+ CountDownLatch writerDone = new CountDownLatch(1);
+ CountDownLatch closerDone = new CountDownLatch(1);
+ AtomicReference