From 7a9d3735d69e78e20014d668230a8bf9a815062a Mon Sep 17 00:00:00 2001 From: Max Cai <87447626+maxcai314@users.noreply.github.com> Date: Sat, 25 Oct 2025 22:29:05 -0700 Subject: [PATCH 1/3] Make fields volatile for thread safety --- .../java/org/nanohttpd/protocols/http/ServerRunnable.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/nanohttpd/protocols/http/ServerRunnable.java b/core/src/main/java/org/nanohttpd/protocols/http/ServerRunnable.java index b9907abb..0e2a4b5d 100644 --- a/core/src/main/java/org/nanohttpd/protocols/http/ServerRunnable.java +++ b/core/src/main/java/org/nanohttpd/protocols/http/ServerRunnable.java @@ -44,13 +44,13 @@ */ public class ServerRunnable implements Runnable { - private NanoHTTPD httpd; + private final NanoHTTPD httpd; private final int timeout; - private IOException bindException; + private volatile IOException bindException; - private boolean hasBinded = false; + private volatile boolean hasBinded = false; public ServerRunnable(NanoHTTPD httpd, int timeout) { this.httpd = httpd; From 03351fb48fde32cb5c256f60d4487b1becf1feef Mon Sep 17 00:00:00 2001 From: Max Cai <87447626+maxcai314@users.noreply.github.com> Date: Sat, 25 Oct 2025 22:40:44 -0700 Subject: [PATCH 2/3] Re-interrupt thread instead of ignoring InterruptedException --- .../main/java/org/nanohttpd/protocols/http/NanoHTTPD.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/nanohttpd/protocols/http/NanoHTTPD.java b/core/src/main/java/org/nanohttpd/protocols/http/NanoHTTPD.java index 42c21514..f54892f8 100644 --- a/core/src/main/java/org/nanohttpd/protocols/http/NanoHTTPD.java +++ b/core/src/main/java/org/nanohttpd/protocols/http/NanoHTTPD.java @@ -609,10 +609,8 @@ public void start(final int timeout, boolean daemon) throws IOException { while (!serverRunnable.hasBinded() && serverRunnable.getBindException() == null) { try { Thread.sleep(10L); - } catch (Throwable e) { - // on android this may not be allowed, that's why we - // catch throwable the wait should be very short because we are - // just waiting for the bind of the socket + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); // re-interrupt } } if (serverRunnable.getBindException() != null) { From 6892f24f6948c63601008b053075b6cc737b5de6 Mon Sep 17 00:00:00 2001 From: Max Cai Date: Sat, 25 Oct 2025 23:52:28 -0700 Subject: [PATCH 3/3] :Revert "Re-interrupt thread instead of ignoring InterruptedException" This reverts commit 03351fb48fde32cb5c256f60d4487b1becf1feef. --- .../main/java/org/nanohttpd/protocols/http/NanoHTTPD.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/nanohttpd/protocols/http/NanoHTTPD.java b/core/src/main/java/org/nanohttpd/protocols/http/NanoHTTPD.java index f54892f8..42c21514 100644 --- a/core/src/main/java/org/nanohttpd/protocols/http/NanoHTTPD.java +++ b/core/src/main/java/org/nanohttpd/protocols/http/NanoHTTPD.java @@ -609,8 +609,10 @@ public void start(final int timeout, boolean daemon) throws IOException { while (!serverRunnable.hasBinded() && serverRunnable.getBindException() == null) { try { Thread.sleep(10L); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); // re-interrupt + } catch (Throwable e) { + // on android this may not be allowed, that's why we + // catch throwable the wait should be very short because we are + // just waiting for the bind of the socket } } if (serverRunnable.getBindException() != null) {