From 943c0306349f437845eb2a9de05478ed555741e5 Mon Sep 17 00:00:00 2001 From: HTHou Date: Mon, 9 Nov 2026 18:55:00 +0800 Subject: [PATCH] THRIFT-5081: Avoid flushing Java socket output during close Client: java Close the underlying socket directly to interrupt blocked I/O and avoid flushing Conscrypt output after the shared socket has closed. Discard unflushed socket buffers and let normal callers flush explicitly. Finish and flush compressed output in the zlib output adapter before its owner closes the underlying transport. Attempt cleanup after unchecked failures, preserving the first failure and suppressing subsequent cleanup failures. Leave generic stream transport cleanup unchanged. Cover cancellation of blocked writes, repeated close, socket-close and flush failures, complete zlib streams, and combined unchecked failures, including a shared exception object and cleanup-only failures. Generated-by: OpenAI Codex (GPT-6) --- .../org/apache/thrift/transport/TSocket.java | 30 +- .../thrift/transport/TZlibTransport.java | 38 ++- .../transport/TestTIOStreamTransport.java | 17 ++ .../apache/thrift/transport/TestTSocket.java | 277 ++++++++++++++++++ .../thrift/transport/TestTZlibTransport.java | 133 +++++++++ 5 files changed, 481 insertions(+), 14 deletions(-) create mode 100644 lib/java/src/test/java/org/apache/thrift/transport/TestTSocket.java 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 writeFailure = new AtomicReference<>(); + AtomicReference closeFailure = new AtomicReference<>(); + doAnswer( + invocation -> { + // BufferedOutputStream holds its lock while invoking this underlying write. + writeEntered.countDown(); + socketClosed.await(); + throw new SocketException("Socket closed"); + }) + .when(streams.output) + .write(any(byte[].class), anyInt(), anyInt()); + doAnswer( + invocation -> { + streams.closed = true; + socketClosed.countDown(); + return null; + }) + .when(streams.socket) + .close(); + Thread writer = + new Thread( + () -> { + try { + transport.write(new byte[64 * 1024]); + } catch (Throwable failure) { + writeFailure.set(failure); + } finally { + writerDone.countDown(); + } + }, + "blocked-socket-writer"); + Thread closer = + new Thread( + () -> { + try { + transport.close(); + } catch (Throwable failure) { + closeFailure.set(failure); + } finally { + closerDone.countDown(); + } + }, + "socket-closer"); + writer.setDaemon(true); + closer.setDaemon(true); + try { + writer.start(); + assertTrue( + writeEntered.await(timeoutSeconds, TimeUnit.SECONDS), + "Writer did not enter socket write"); + closer.start(); + + assertTrue( + closerDone.await(timeoutSeconds, TimeUnit.SECONDS), + "close() waited for the blocked writer"); + assertTrue( + writerDone.await(timeoutSeconds, TimeUnit.SECONDS), + "Socket close did not interrupt the writer"); + assertNull(closeFailure.get()); + assertInstanceOf(TTransportException.class, writeFailure.get()); + assertInstanceOf(SocketException.class, writeFailure.get().getCause()); + } finally { + // Release both workers even when testing an implementation with the regression. + streams.socket.close(); + writer.join(1000); + closer.join(1000); + } + } + + private static void assertClosedAndIdempotent(TSocket transport, SocketStreams streams) { + assertFalse(transport.isOpen()); + assertNull(transport.inputStream_); + assertNull(transport.outputStream_); + + clearInvocations(streams.socket, streams.input, streams.output); + transport.close(); + verifyNoInteractions(streams.socket, streams.input, streams.output); + } + + private static class SocketStreams { + final Socket socket = mock(Socket.class); + final InputStream input = mock(InputStream.class); + final OutputStream output = mock(OutputStream.class); + final ByteArrayOutputStream sent = new ByteArrayOutputStream(); + boolean closed; + + SocketStreams() throws IOException { + when(socket.isConnected()).thenReturn(true); + when(socket.getInputStream()).thenReturn(input); + when(socket.getOutputStream()).thenReturn(output); + doAnswer( + invocation -> { + closed = true; + return null; + }) + .when(socket) + .close(); + doAnswer( + invocation -> { + socket.close(); + return null; + }) + .when(input) + .close(); + doAnswer( + invocation -> { + socket.close(); + return null; + }) + .when(output) + .close(); + doAnswer( + invocation -> { + checkOpen(); + sent.write( + invocation.getArgument(0), + invocation.getArgument(1), + invocation.getArgument(2)); + return null; + }) + .when(output) + .write(any(byte[].class), anyInt(), anyInt()); + doAnswer( + invocation -> { + checkOpen(); + return null; + }) + .when(output) + .flush(); + } + + private void checkOpen() throws SocketException { + if (closed) { + throw new SocketException("Socket is closed"); + } + } + } +} diff --git a/lib/java/src/test/java/org/apache/thrift/transport/TestTZlibTransport.java b/lib/java/src/test/java/org/apache/thrift/transport/TestTZlibTransport.java index ef70c53d715..22fca7669f1 100644 --- a/lib/java/src/test/java/org/apache/thrift/transport/TestTZlibTransport.java +++ b/lib/java/src/test/java/org/apache/thrift/transport/TestTZlibTransport.java @@ -20,7 +20,13 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; +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.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; @@ -28,19 +34,146 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; import java.util.Arrays; import java.util.zip.DataFormatException; import java.util.zip.DeflaterOutputStream; import java.util.zip.InflaterInputStream; import org.apache.thrift.TConfiguration; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; public class TestTZlibTransport { + @ParameterizedTest + @CsvSource({"false, false", "true, false", "true, true"}) + public void testCloseFinishesCompressedStreamOverSocket( + boolean writeData, boolean flushBeforeClose) throws Exception { + byte[] expected = writeData ? new byte[] {1, 2, 3} : new byte[0]; + try (ServerSocket listener = new ServerSocket(0, 1, InetAddress.getLoopbackAddress()); + TSocket socket = + new TSocket(new Socket(listener.getInetAddress(), listener.getLocalPort())); + Socket peer = listener.accept()) { + peer.setSoTimeout(5000); + try (TTransport transport = getTransport(socket)) { + if (writeData) { + transport.write(expected); + } + if (flushBeforeClose) { + transport.flush(); + } + } + + try (DataInputStream input = + new DataInputStream(new InflaterInputStream(peer.getInputStream()))) { + byte[] actual = new byte[expected.length]; + input.readFully(actual); + assertArrayEquals(expected, actual); + // Reading to the end also verifies that the compression trailer reached the peer. + assertEquals(-1, input.read()); + } + } + } + protected TTransport getTransport(TTransport underlying) throws TTransportException { return new TZlibTransport(underlying); } + @Test + public void testCheckedFinalFlushFailureStillClosesUnderlyingTransport() throws Exception { + TTransport underlying = mock(TTransport.class); + when(underlying.isOpen()).thenReturn(true); + TZlibTransport transport = new TZlibTransport(underlying); + doThrow(new TTransportException("Final flush failure")).when(underlying).flush(); + + transport.close(); + + verify(underlying).close(); + assertNull(transport.inputStream_); + assertNull(transport.outputStream_); + } + + @Test + public void testUncheckedFinalFlushFailureStillClosesUnderlyingTransport() throws Exception { + TTransport underlying = mock(TTransport.class); + when(underlying.isOpen()).thenReturn(true); + TZlibTransport transport = new TZlibTransport(underlying); + IllegalStateException failure = new IllegalStateException("Final flush failure"); + doThrow(failure).when(underlying).flush(); + + assertSame(failure, assertThrows(IllegalStateException.class, transport::close)); + + verify(underlying).close(); + assertNull(transport.inputStream_); + assertNull(transport.outputStream_); + } + + @ParameterizedTest + @CsvSource({"false, false", "false, true", "true, false", "true, true"}) + public void testClosePreservesFirstUncheckedFailure(boolean primaryError, boolean cleanupError) + throws Exception { + TTransport underlying = mock(TTransport.class); + when(underlying.isOpen()).thenReturn(true); + TZlibTransport transport = new TZlibTransport(underlying); + Throwable primary = + primaryError + ? new AssertionError("Final flush failure") + : new IllegalStateException("Final flush failure"); + Throwable cleanup = + cleanupError + ? new AssertionError("Underlying close failure") + : new IllegalArgumentException("Underlying close failure"); + doThrow(primary).when(underlying).flush(); + doThrow(cleanup).when(underlying).close(); + + Throwable actual = assertThrows(Throwable.class, transport::close); + + assertSame(primary, actual); + assertArrayEquals(new Throwable[] {cleanup}, actual.getSuppressed()); + verify(underlying).close(); + assertNull(transport.inputStream_); + assertNull(transport.outputStream_); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + public void testClosePropagatesUncheckedCleanupFailure(boolean cleanupError) throws Exception { + TTransport underlying = mock(TTransport.class); + when(underlying.isOpen()).thenReturn(true); + TZlibTransport transport = new TZlibTransport(underlying); + Throwable failure = + cleanupError + ? new AssertionError("Underlying close failure") + : new IllegalStateException("Underlying close failure"); + doThrow(failure).when(underlying).close(); + + assertSame(failure, assertThrows(Throwable.class, transport::close)); + + assertEquals(0, failure.getSuppressed().length); + verify(underlying).close(); + assertNull(transport.inputStream_); + assertNull(transport.outputStream_); + } + + @Test + public void testCloseDoesNotSuppressFailureOntoItself() throws Exception { + TTransport underlying = mock(TTransport.class); + when(underlying.isOpen()).thenReturn(true); + TZlibTransport transport = new TZlibTransport(underlying); + IllegalStateException failure = new IllegalStateException("Transport failure"); + doThrow(failure).when(underlying).flush(); + doThrow(failure).when(underlying).close(); + + assertSame(failure, assertThrows(IllegalStateException.class, transport::close)); + + assertEquals(0, failure.getSuppressed().length); + verify(underlying).close(); + } + public static byte[] byteSequence(int start, int end) { byte[] result = new byte[end - start + 1]; for (int i = 0; i <= (end - start); i++) {