Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ private void flushOutput(OutputStream os) throws IOException {
}

private QueueAction nextQueueAction(boolean flushedSinceLastSend) throws InterruptedException {
synchronized (outQueue) {
while (true) {
boolean closed = handler.isClosed();
while (true) {
// Read the handler state before taking the queue monitor to keep lock ordering consistent
// with input-side code paths that already hold the handler lock and then inspect outQueue.
boolean closed = handler.isClosed();
synchronized (outQueue) {
if (!outQueue.isEmpty()) {
return QueueAction.message(outQueue.removeFirst());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -91,6 +93,27 @@ void run_whenHandlerAlreadyClosed_flushesAndClosesStream() throws IOException {
assertTrue(outputHandler.closedOutputQueue);
}

@Test
void run_whenCheckingClosedState_doesNotHoldQueueMonitor() throws IOException {
when(handler.getSocket()).thenReturn(socket);
when(socket.getOutputStream()).thenReturn(outputStream);
doAnswer(
invocation -> {
assertFalse(Thread.holdsLock(outputHandler.outQueue));
return true;
})
.when(handler)
.isClosed();

outputHandler.run();

verify(outputStream, atLeastOnce()).flush();
verify(outputStream).close();
verify(handler).close();
verify(handler).closedOutput();
assertTrue(outputHandler.closedOutputQueue);
}

@Test
void run_whenMessageQueued_sendsMessageBeforeClosing() throws IOException {
FCPMessage message = mock(FCPMessage.class);
Expand Down
Loading