From 744b489286d45b8112034e52126e8ba31c57fe34 Mon Sep 17 00:00:00 2001 From: kares Date: Sat, 2 Jul 2022 16:31:33 +0200 Subject: [PATCH] [fix] adjust write fix to revert regressions (hangs) follow-up on 75a66e1b5ad55fd61cf565f43d0cba2f1a41f20a which would not work with the higher level syswrite usage --- .../java/org/jruby/ext/openssl/SSLSocket.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/jruby/ext/openssl/SSLSocket.java b/src/main/java/org/jruby/ext/openssl/SSLSocket.java index ee0eec6f..a33e764b 100644 --- a/src/main/java/org/jruby/ext/openssl/SSLSocket.java +++ b/src/main/java/org/jruby/ext/openssl/SSLSocket.java @@ -674,28 +674,32 @@ public int write(ByteBuffer src, boolean blocking) throws SSLException, IOExcept final boolean blockingMode = channel.isBlocking(); if ( ! blocking ) channel.configureBlocking(false); - int written = 0; + boolean loop = false; int written = 0; try { - while (true) { - if (netWriteData.hasRemaining()) flushData(blocking); - else if (!src.hasRemaining()) break; - + if (netWriteData.hasRemaining()) flushData(blocking); + do { netWriteData.clear(); final SSLEngineResult result = engine.wrap(src, netWriteData); netWriteData.flip(); switch (result.getStatus()) { case OK: + loop = flushData(blocking) && src.hasRemaining(); written += result.bytesConsumed(); break; case BUFFER_OVERFLOW: netWriteData = Utils.ensureCapacity(netWriteData, engine.getSession().getPacketBufferSize()); netWriteData.position(netWriteData.limit()); + loop = true; // src.hasRemaining(); + if (netWriteData.hasRemaining()) flushData(blocking); break; case CLOSED: throw getRuntime().newIOError("closed SSL engine"); // EOF? + case BUFFER_UNDERFLOW: + debug("SSLSocket.write unexpected BUFFER_UNDERFLOW"); + return written; } - } + } while (loop); } finally { if ( ! blocking ) channel.configureBlocking(blockingMode);