You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@asterixdb.apache.org by mb...@apache.org on 2026/08/19 18:00:17 UTC

(asterixdb) 07/12: [NO ISSUE][HYR][NET] Bound the SSL handshake and stop it spinning

This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/asterixdb.git

commit b322b1e80396a5d41c73e4cc263061d7be0618d2
Author: Michael Blow <mi...@couchbase.com>
AuthorDate: Thu Aug 13 13:00:54 2026 -0400

    [NO ISSUE][HYR][NET] Bound the SSL handshake and stop it spinning
    
    - user model changes: no
    - storage format changes: no
    - interface changes: no
    
    Details:
    - IPCConnectionManager.register() puts the channel in non-blocking mode,
      but SslHandshake.unwrap() was written as though it were blocking: a
      read() returning 0 - the ordinary "peer has not sent anything yet"
      case - fell through to engine.unwrap() on an empty buffer, yielded
      BUFFER_UNDERFLOW, left the status at NEED_UNWRAP, and the loop in
      handshake() retried immediately. A handshake waiting on its peer
      therefore burned 100% of a core for as long as it waited; a thread
      dump of a hung CI run showed 6383s of CPU over 6395s elapsed. The
      write loop in wrap() had the same defect: write() returning 0 - the
      peer not draining, or a full send buffer - was retried immediately
    - wait for channel readiness on a private selector instead, at the
      points where no progress is possible: a read with nothing buffered and
      nothing arrived, a partial record left by BUFFER_UNDERFLOW, and a
      write that the channel accepted no bytes of. Note the read buffer may
      still hold a complete record after a zero-length read, so waiting
      unconditionally on read() == 0 would stall a handshake whose bytes had
      already arrived
    - bound the whole handshake by a deadline
      (hyracks.ssl.handshake.timeout.sec, default 60). On expiry handshake()
      returns false, which the connection manager already treats as a failed
      handshake: it closes the handle, waitTillConnected() observes CLOSED,
      and getIPCHandle() retries. Before this an unresponsive peer hung the
      caller forever
    - run the SSLEngine delegated tasks inline rather than on a
      single-thread executor created per handshake and never shut down; the
      handshake already runs on an executor thread of its own
    
    Ext-ref: MB-73310
    Co-Authored-By: Claude Opus 5 <no...@anthropic.com>
    Co-Authored-By: Claude Fable 5 <no...@anthropic.com>
    Change-Id: I50ffed3f912903663c4b67e713454b79e9d80503
    Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/21546
    Tested-by: Jenkins <je...@fulliautomatix.ics.uci.edu>
    Reviewed-by: Ali Alsuliman <al...@gmail.com>
    Integration-Tests: Jenkins <je...@fulliautomatix.ics.uci.edu>
---
 hyracks-fullstack/hyracks/hyracks-ipc/pom.xml      |   6 +
 .../apache/hyracks/ipc/sockets/SslHandshake.java   | 149 ++++++++++++++++-----
 .../hyracks/ipc/tests/SslHandshakeTimeoutTest.java |  86 ++++++++++++
 .../hyracks/util/annotations/AiProvenance.java     |   5 +
 4 files changed, 212 insertions(+), 34 deletions(-)

diff --git a/hyracks-fullstack/hyracks/hyracks-ipc/pom.xml b/hyracks-fullstack/hyracks/hyracks-ipc/pom.xml
index 37fcbced97..35ebde2aed 100644
--- a/hyracks-fullstack/hyracks/hyracks-ipc/pom.xml
+++ b/hyracks-fullstack/hyracks/hyracks-ipc/pom.xml
@@ -41,6 +41,12 @@
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <systemPropertyVariables combine.children="append">
+            <!-- the handshake bound is read once at class-init; shorten it so the timeout test is quick -->
+            <hyracks.ssl.handshake.timeout.sec>3</hyracks.ssl.handshake.timeout.sec>
+          </systemPropertyVariables>
+        </configuration>
       </plugin>
     </plugins>
   </build>
diff --git a/hyracks-fullstack/hyracks/hyracks-ipc/src/main/java/org/apache/hyracks/ipc/sockets/SslHandshake.java b/hyracks-fullstack/hyracks/hyracks-ipc/src/main/java/org/apache/hyracks/ipc/sockets/SslHandshake.java
index fa4c8af914..3f4c231fd1 100644
--- a/hyracks-fullstack/hyracks/hyracks-ipc/src/main/java/org/apache/hyracks/ipc/sockets/SslHandshake.java
+++ b/hyracks-fullstack/hyracks/hyracks-ipc/src/main/java/org/apache/hyracks/ipc/sockets/SslHandshake.java
@@ -20,19 +20,29 @@ package org.apache.hyracks.ipc.sockets;
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
 import java.nio.channels.SocketChannel;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
 
 import javax.net.ssl.SSLEngine;
 import javax.net.ssl.SSLEngineResult;
 import javax.net.ssl.SSLException;
 
 import org.apache.hyracks.util.NetworkUtil;
+import org.apache.hyracks.util.annotations.AiProvenance;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 
 public class SslHandshake {
 
-    private final ExecutorService executor = Executors.newSingleThreadExecutor();
+    private static final Logger LOGGER = LogManager.getLogger();
+
+    public static final String HANDSHAKE_TIMEOUT_SEC_KEY = "hyracks.ssl.handshake.timeout.sec";
+    // TODO(mblow): this could be a config parameter
+    private static final long HANDSHAKE_TIMEOUT_NANOS =
+            TimeUnit.SECONDS.toNanos(Integer.getInteger(HANDSHAKE_TIMEOUT_SEC_KEY, 60));
+
     private final ByteBuffer handshakeOutData;
     private final SocketChannel socketChannel;
     private final SSLEngine engine;
@@ -40,6 +50,8 @@ public class SslHandshake {
     private ByteBuffer handshakeInData;
     private ByteBuffer outEncryptedData;
     private ByteBuffer inEncryptedData;
+    private Selector selector;
+    private long deadlineNanos;
 
     public SslHandshake(SslSocketChannel sslSocketChannel) {
         socketChannel = sslSocketChannel.getSocketChannel();
@@ -53,34 +65,101 @@ public class SslHandshake {
         handshakeInData = ByteBuffer.allocate(appBufferSize);
     }
 
+    @AiProvenance(agent = AiProvenance.Agent.CLAUDE_OPUS_5, tool = AiProvenance.Tool.CLAUDE_CODE_UI, contributionKind = AiProvenance.ContributionKind.REFACTORED, notes = "handshake deadline, inline tasks")
     public boolean handshake() throws IOException {
-        handshakeStatus = engine.getHandshakeStatus();
-        while (handshakeStatus != SSLEngineResult.HandshakeStatus.FINISHED
-                && handshakeStatus != SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) {
-            switch (handshakeStatus) {
-                case NEED_UNWRAP:
-                    if (!unwrap()) {
-                        return false;
-                    }
-                    break;
-                case NEED_WRAP:
-                    wrap();
-                    break;
-                case NEED_TASK:
-                    Runnable task;
-                    while ((task = engine.getDelegatedTask()) != null) {
-                        executor.execute(task);
-                    }
-                    handshakeStatus = engine.getHandshakeStatus();
-                    break;
-                default:
-                    throw new IllegalStateException("Invalid SSL handshake status: " + handshakeStatus);
+        deadlineNanos = System.nanoTime() + HANDSHAKE_TIMEOUT_NANOS;
+        try {
+            handshakeStatus = engine.getHandshakeStatus();
+            while (handshakeStatus != SSLEngineResult.HandshakeStatus.FINISHED
+                    && handshakeStatus != SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) {
+                if (System.nanoTime() - deadlineNanos >= 0) {
+                    LOGGER.warn("SSL handshake with {} timed out in state {}", socketChannel, handshakeStatus);
+                    return false;
+                }
+                switch (handshakeStatus) {
+                    case NEED_UNWRAP:
+                        if (!unwrap()) {
+                            return false;
+                        }
+                        break;
+                    case NEED_WRAP:
+                        if (!wrap()) {
+                            return false;
+                        }
+                        break;
+                    case NEED_TASK:
+                        Runnable task;
+                        // run inline; the handshake already owns a thread of its own
+                        while ((task = engine.getDelegatedTask()) != null) {
+                            task.run();
+                        }
+                        handshakeStatus = engine.getHandshakeStatus();
+                        break;
+                    default:
+                        throw new IllegalStateException("Invalid SSL handshake status: " + handshakeStatus);
+                }
             }
+            return true;
+        } finally {
+            NetworkUtil.closeQuietly(selector);
+        }
+    }
+
+    private boolean awaitReadable() throws IOException {
+        return await(SelectionKey.OP_READ, "data");
+    }
+
+    private boolean awaitWritable() throws IOException {
+        return await(SelectionKey.OP_WRITE, "buffer space");
+    }
+
+    /**
+     * Blocks until the channel is ready for the given operation, the handshake deadline expires, or the
+     * wait is interrupted. The channel is non-blocking, so without this a read or write making no progress
+     * would be retried immediately, spinning a core until the peer acts.
+     *
+     * @return true if the operation should be retried, false if the handshake deadline has passed
+     */
+    @AiProvenance(agent = AiProvenance.Agent.CLAUDE_OPUS_5, tool = AiProvenance.Tool.CLAUDE_CODE_UI, contributionKind = AiProvenance.ContributionKind.GENERATED, notes = "await readability within deadline")
+    @AiProvenance(agent = AiProvenance.Agent.CLAUDE_FABLE_5, tool = AiProvenance.Tool.CLAUDE_CODE_UI, contributionKind = AiProvenance.ContributionKind.REFACTORED, notes = "generalized to await writability")
+    private boolean await(int op, String what) throws IOException {
+        final long remainingNanos = deadlineNanos - System.nanoTime();
+        if (remainingNanos <= 0) {
+            LOGGER.warn("SSL handshake with {} timed out awaiting {}", socketChannel, what);
+            return false;
+        }
+        if (selector == null) {
+            selector = Selector.open();
+        }
+        // re-registering against the same selector just updates the interest set
+        socketChannel.register(selector, op);
+        // select() may return spuriously; the caller re-checks the deadline on the next pass
+        selector.select(Math.max(1, TimeUnit.NANOSECONDS.toMillis(remainingNanos)));
+        selector.selectedKeys().clear();
+        if (Thread.currentThread().isInterrupted()) {
+            LOGGER.warn("SSL handshake with {} interrupted awaiting {}", socketChannel, what);
+            return false;
         }
         return true;
     }
 
-    private void wrap() throws IOException {
+    /**
+     * Writes out the encrypted handshake data, waiting for the peer within the handshake deadline whenever
+     * the channel accepts no bytes.
+     *
+     * @return true if the buffer was fully written, false if the handshake deadline has passed
+     */
+    @AiProvenance(agent = AiProvenance.Agent.CLAUDE_FABLE_5, tool = AiProvenance.Tool.CLAUDE_CODE_UI, contributionKind = AiProvenance.ContributionKind.GENERATED, notes = "bounded flush of handshake data")
+    private boolean flush() throws IOException {
+        while (outEncryptedData.hasRemaining()) {
+            if (socketChannel.write(outEncryptedData) == 0 && !awaitWritable()) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private boolean wrap() throws IOException {
         outEncryptedData.clear();
         SSLEngineResult result;
         try {
@@ -94,21 +173,18 @@ public class SslHandshake {
         switch (result.getStatus()) {
             case OK:
                 outEncryptedData.flip();
-                while (outEncryptedData.hasRemaining()) {
-                    socketChannel.write(outEncryptedData);
-                }
-                break;
+                return flush();
             case BUFFER_OVERFLOW:
                 outEncryptedData = NetworkUtil.enlargeSslPacketBuffer(engine, outEncryptedData);
-                break;
+                return true;
             case CLOSED:
                 outEncryptedData.flip();
-                while (outEncryptedData.hasRemaining()) {
-                    socketChannel.write(outEncryptedData);
+                if (!flush()) {
+                    return false;
                 }
                 inEncryptedData.clear();
                 handshakeStatus = engine.getHandshakeStatus();
-                break;
+                return true;
             case BUFFER_UNDERFLOW:
             default:
                 throw new IllegalStateException("Invalid SSL status " + result.getStatus());
@@ -126,6 +202,10 @@ public class SslHandshake {
             engine.closeOutbound();
             return false;
         }
+        if (read == 0 && inEncryptedData.position() == 0) {
+            // nothing buffered and nothing arrived; wait for the peer rather than spinning on the channel
+            return awaitReadable();
+        }
         inEncryptedData.flip();
         SSLEngineResult result;
         try {
@@ -145,7 +225,8 @@ public class SslHandshake {
                 break;
             case BUFFER_UNDERFLOW:
                 inEncryptedData = handleBufferUnderflow(engine, inEncryptedData);
-                break;
+                // only a partial record is buffered; more bytes are needed before a retry can make progress
+                return awaitReadable();
             case CLOSED:
                 if (engine.isOutboundDone()) {
                     return false;
diff --git a/hyracks-fullstack/hyracks/hyracks-ipc/src/test/java/org/apache/hyracks/ipc/tests/SslHandshakeTimeoutTest.java b/hyracks-fullstack/hyracks/hyracks-ipc/src/test/java/org/apache/hyracks/ipc/tests/SslHandshakeTimeoutTest.java
new file mode 100644
index 0000000000..8f2652f392
--- /dev/null
+++ b/hyracks-fullstack/hyracks/hyracks-ipc/src/test/java/org/apache/hyracks/ipc/tests/SslHandshakeTimeoutTest.java
@@ -0,0 +1,86 @@
+/*
+ * 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.hyracks.ipc.tests;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadMXBean;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SocketChannel;
+import java.util.concurrent.TimeUnit;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLEngine;
+
+import org.apache.hyracks.ipc.sockets.SslHandshake;
+import org.apache.hyracks.ipc.sockets.SslSocketChannel;
+import org.apache.hyracks.util.annotations.AiProvenance;
+import org.junit.Test;
+
+/**
+ * The IPC layer hands {@link SslHandshake} a non-blocking channel. A peer which accepts the connection
+ * and then says nothing must therefore make the handshake wait, and give up at its deadline, rather than
+ * retry the zero-length read in a tight loop.
+ */
+@AiProvenance(agent = AiProvenance.Agent.CLAUDE_OPUS_5, tool = AiProvenance.Tool.CLAUDE_CODE_UI, contributionKind = AiProvenance.ContributionKind.TEST_GENERATED, notes = "handshake deadline regression")
+public class SslHandshakeTimeoutTest {
+
+    /**
+     * The bound is read once at class-init, so it cannot be set from here; surefire supplies it for this
+     * module. Read it back rather than assuming, so the assertions track whatever the build configured.
+     */
+    private static final int TIMEOUT_SEC = Integer.getInteger(SslHandshake.HANDSHAKE_TIMEOUT_SEC_KEY, 60);
+
+    @Test
+    public void unresponsivePeerFailsAtDeadlineWithoutSpinning() throws Exception {
+        assertTrue("expected " + SslHandshake.HANDSHAKE_TIMEOUT_SEC_KEY + " to be shortened for tests, but it is "
+                + TIMEOUT_SEC + "s; check the surefire configuration in this module's pom", TIMEOUT_SEC <= 10);
+        try (ServerSocketChannel server = ServerSocketChannel.open()) {
+            server.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
+            try (SocketChannel client = SocketChannel.open(server.getLocalAddress());
+                    // accepted, but deliberately never written to
+                    SocketChannel accepted = server.accept()) {
+                client.configureBlocking(false);
+                final SSLEngine engine = SSLContext.getDefault().createSSLEngine();
+                engine.setUseClientMode(true);
+                final SslSocketChannel sslChannel = new SslSocketChannel(client, engine);
+
+                final ThreadMXBean threads = ManagementFactory.getThreadMXBean();
+                final long cpuStart = threads.getCurrentThreadCpuTime();
+                final long start = System.nanoTime();
+                final boolean handshakeSucceeded = sslChannel.handshake();
+                final long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
+                final long cpuMillis = TimeUnit.NANOSECONDS.toMillis(threads.getCurrentThreadCpuTime() - cpuStart);
+
+                assertFalse("handshake should fail against a peer that never replies", handshakeSucceeded);
+                assertTrue("gave up after " + elapsedMillis + "ms, before the " + TIMEOUT_SEC + "s deadline",
+                        elapsedMillis >= TimeUnit.SECONDS.toMillis(TIMEOUT_SEC) - 250);
+                assertTrue("still waiting after " + elapsedMillis + "ms, well past the deadline",
+                        elapsedMillis < TimeUnit.SECONDS.toMillis(TIMEOUT_SEC) * 4);
+                // the point of the fix: the wait must be idle, not a retry loop on the non-blocking channel
+                assertTrue("handshake burned " + cpuMillis + "ms of CPU over " + elapsedMillis
+                        + "ms of waiting; it is spinning rather than waiting", cpuMillis < elapsedMillis / 4);
+            }
+        }
+    }
+}
diff --git a/hyracks-fullstack/hyracks/hyracks-util/src/main/java/org/apache/hyracks/util/annotations/AiProvenance.java b/hyracks-fullstack/hyracks/hyracks-util/src/main/java/org/apache/hyracks/util/annotations/AiProvenance.java
index 2d32983795..ab94ab7c6b 100644
--- a/hyracks-fullstack/hyracks/hyracks-util/src/main/java/org/apache/hyracks/util/annotations/AiProvenance.java
+++ b/hyracks-fullstack/hyracks/hyracks-util/src/main/java/org/apache/hyracks/util/annotations/AiProvenance.java
@@ -202,6 +202,11 @@ public @interface AiProvenance {
 
         CLAUDE_OPUS_3(ANTHROPIC, "claude-3-opus", "Claude Opus 3"),
 
+        // =========================
+        // Anthropic — Claude Fable
+        // =========================
+        CLAUDE_FABLE_5(ANTHROPIC, "claude-fable-5", "Claude Fable 5"),
+
         // =========================
         // Anthropic — Claude Sonnet
         // =========================