You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by lh...@apache.org on 2026/10/16 22:31:29 UTC

(pulsar) 06/24: [fix][broker] Fix web service close hang when the Jetty server fails to start (#26895)

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

lhotari pushed a commit to branch branch-4.0-backports-2026-10-17
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit 6b3bd6751ccb63346c8188dabde3493222bbc9c4
Author: Lari Hotari <lh...@users.noreply.github.com>
AuthorDate: Wed Oct 14 15:22:06 2026 -0700

    [fix][broker] Fix web service close hang when the Jetty server fails to start (#26895)
    
    (cherry picked from commit 7ddf52beef98fec691802ef90334d9c5ed375816)
---
 .../pulsar/broker/web/WebExecutorThreadPool.java   | 20 ++++++
 .../broker/web/WebExecutorThreadPoolTest.java      | 75 ++++++++++++++++++++++
 2 files changed, 95 insertions(+)

diff --git a/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/web/WebExecutorThreadPool.java b/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/web/WebExecutorThreadPool.java
index a1d566e135cf..2a681b745c0f 100644
--- a/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/web/WebExecutorThreadPool.java
+++ b/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/web/WebExecutorThreadPool.java
@@ -26,6 +26,7 @@ import org.eclipse.jetty.util.thread.ExecutorThreadPool;
 public class WebExecutorThreadPool extends ExecutorThreadPool {
 
     private final ThreadFactory threadFactory;
+    private volatile boolean startAttempted;
 
     public WebExecutorThreadPool(int maxThreads, String namePrefix) {
         this(maxThreads, namePrefix, 8192);
@@ -40,4 +41,23 @@ public class WebExecutorThreadPool extends ExecutorThreadPool {
     protected Thread newThread(Runnable job) {
         return threadFactory.newThread(job);
     }
+
+    @Override
+    protected void doStart() throws Exception {
+        startAttempted = true;
+        super.doStart();
+    }
+
+    /**
+     * {@link ExecutorThreadPool#join()} waits without a timeout for the executor to terminate, but the executor
+     * is shut down only in {@link #doStop()}, which Jetty skips for a pool that was never started. That happens
+     * when {@code Server.start()} fails to open a connector (e.g. the port is already in use), since connectors
+     * are opened before the server's beans are started. Joining such a pool would block forever.
+     */
+    @Override
+    public void join() throws InterruptedException {
+        if (startAttempted) {
+            super.join();
+        }
+    }
 }
diff --git a/pulsar-broker-common/src/test/java/org/apache/pulsar/broker/web/WebExecutorThreadPoolTest.java b/pulsar-broker-common/src/test/java/org/apache/pulsar/broker/web/WebExecutorThreadPoolTest.java
new file mode 100644
index 000000000000..1bf4b372a82d
--- /dev/null
+++ b/pulsar-broker-common/src/test/java/org/apache/pulsar/broker/web/WebExecutorThreadPoolTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.pulsar.broker.web;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.testng.annotations.Test;
+
+public class WebExecutorThreadPoolTest {
+
+    @Test(timeOut = 30000)
+    public void testJoinReturnsAfterServerFailsToBindPort() throws Exception {
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        try (ServerSocket occupied = new ServerSocket()) {
+            occupied.bind(new InetSocketAddress(loopback, 0));
+            WebExecutorThreadPool pool = new WebExecutorThreadPool(4, "test-web");
+            Server server = new Server(pool);
+            ServerConnector connector = new ServerConnector(server);
+            connector.setHost(loopback.getHostAddress());
+            connector.setPort(occupied.getLocalPort());
+            server.addConnector(connector);
+
+            assertThatThrownBy(server::start).isInstanceOf(IOException.class);
+            assertThat(pool.isStarted()).isFalse();
+            server.stop();
+
+            assertJoinCompletes(pool);
+        }
+    }
+
+    @Test(timeOut = 30000)
+    public void testJoinReturnsAfterStartedPoolIsStopped() throws Exception {
+        WebExecutorThreadPool pool = new WebExecutorThreadPool(4, "test-web");
+        pool.start();
+        pool.stop();
+
+        assertJoinCompletes(pool);
+    }
+
+    private static void assertJoinCompletes(WebExecutorThreadPool pool) {
+        CompletableFuture<Void> joined = CompletableFuture.runAsync(() -> {
+            try {
+                pool.join();
+            } catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+                throw new RuntimeException(e);
+            }
+        });
+        assertThat(joined).succeedsWithin(10, TimeUnit.SECONDS);
+    }
+}