diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslConnection.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslConnection.java index 1dffaadc6d69..97c4ca363c77 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslConnection.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslConnection.java @@ -862,6 +862,11 @@ protected void needsFillInterest() { interest = true; _fillState = FillState.INTERESTED; + if (_flushState == FlushState.IDLE && BufferUtil.hasContent(_encryptedOutput)) + { + _flushState = FlushState.WRITING; + write = _encryptedOutput; + } } break; @@ -1279,9 +1284,26 @@ public void doShutdownOutput() { // If we still can't flush, but we are not closing the endpoint, // let's just flush the encrypted output in the background. - ByteBuffer write = _encryptedOutput; - if (BufferUtil.hasContent(write)) - endPoint.write(Callback.from(Callback.NOOP::succeeded, t -> endPoint.close()), write); + ByteBuffer write = null; + synchronized (_decryptedEndPoint) + { + if (BufferUtil.hasContent(_encryptedOutput)) + { + write = _encryptedOutput; + _flushState = FlushState.WRITING; + } + } + if (write != null) + { + endPoint.write(Callback.from(() -> + { + synchronized (_decryptedEndPoint) + { + _flushState = FlushState.IDLE; + releaseEncryptedOutputBuffer(); + } + }, t -> endPoint.close()), write); + } } } @@ -1489,19 +1511,23 @@ private final class IncompleteWriteCallback implements Callback, Invocable public void succeeded() { boolean fillable; + boolean interested; synchronized (_decryptedEndPoint) { if (LOG.isDebugEnabled()) LOG.debug("IncompleteWriteCB succeeded {}", SslConnection.this); - releaseEncryptedOutputBuffer(); _flushState = FlushState.IDLE; + + interested = _fillState == FillState.INTERESTED; fillable = _fillState == FillState.WAIT_FOR_FLUSH; if (fillable) _fillState = FillState.IDLE; } - if (fillable) + if (interested) + ensureFillInterested(); + else if (fillable) _decryptedEndPoint.getFillInterest().fillable(); _decryptedEndPoint.getWriteFlusher().completeWrite(); @@ -1520,7 +1546,8 @@ public void failed(final Throwable x) releaseEncryptedOutputBuffer(); _flushState = FlushState.IDLE; - failFillInterest = _fillState == FillState.WAIT_FOR_FLUSH; + failFillInterest = _fillState == FillState.WAIT_FOR_FLUSH || + _fillState == FillState.INTERESTED; if (failFillInterest) _fillState = FillState.IDLE; } diff --git a/jetty-io/src/test/java/org/eclipse/jetty/io/SslConnectionTest.java b/jetty-io/src/test/java/org/eclipse/jetty/io/SslConnectionTest.java index 17a00339d486..3755231dfd33 100644 --- a/jetty-io/src/test/java/org/eclipse/jetty/io/SslConnectionTest.java +++ b/jetty-io/src/test/java/org/eclipse/jetty/io/SslConnectionTest.java @@ -485,9 +485,6 @@ public void testBlockedClose() throws Exception server.configureBlocking(false); _manager.accept(server); - //__startBlocking.set(5); - //__blockFor.set(3); - client.getOutputStream().write("Short".getBytes(StandardCharsets.UTF_8)); byte[] buffer = new byte[1024]; int len = client.getInputStream().read(buffer); @@ -515,7 +512,7 @@ public void testBlockedClose() throws Exception assertTrue(__onIncompleteFlush.get()); ((TestEP)_lastEndp).getWriteFlusher().completeWrite(); len = client.getInputStream().read(buffer); - assertThat(len, is(len)); + assertThat(len, is(-1)); } } }