Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions lib/java/src/main/java/org/apache/thrift/transport/TSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,27 @@ public void open() throws TTransportException {
}
}

/** Closes the socket. */
/**
* Closes the socket and discards any unflushed output.
*
* <p>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;
}
Comment thread
HTHou marked this conversation as resolved.
Resolved
}
Comment thread
HTHou marked this conversation as resolved.
Resolved

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,36 @@ public void open() throws TTransportException {
transport_.open();
}

/**
* Finishes compression, flushes the output, and closes the underlying transport.
*
* <p>This can block while sending the compressed output. To abort pending I/O, close the
* underlying transport directly.
*
* <p>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);
}
}
}
}
}
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading
Loading