From b1c3eca9320e83db1fe6fe281c2d4a8875e8f16b Mon Sep 17 00:00:00 2001
Date: Thu, 27 Jul 2023 20:07:00 +0800
Subject: [PATCH] [Backport]8179273: sun.net.httpserver.LeftOverInputStream should stop
 attempting to drain the stream when the server is stopped

---
 .../classes/sun/net/httpserver/LeftOverInputStream.java    | 7 +++++--
 jdk/src/share/classes/sun/net/httpserver/ServerImpl.java   | 4 ++++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/jdk/src/share/classes/sun/net/httpserver/LeftOverInputStream.java b/jdk/src/share/classes/sun/net/httpserver/LeftOverInputStream.java
index c715d72ad..d3a6e1b08 100644
--- a/jdk/src/share/classes/sun/net/httpserver/LeftOverInputStream.java
+++ b/jdk/src/share/classes/sun/net/httpserver/LeftOverInputStream.java
@@ -41,8 +41,8 @@ import com.sun.net.httpserver.spi.*;
  * isEOF() returns true, when all expected bytes have been read
  */
 abstract class LeftOverInputStream extends FilterInputStream {
-    ExchangeImpl t;
-    ServerImpl server;
+    final ExchangeImpl t;
+    final ServerImpl server;
     protected boolean closed = false;
     protected boolean eof = false;
     byte[] one = new byte [1];
@@ -109,6 +109,9 @@ abstract class LeftOverInputStream extends FilterInputStream {
         int bufSize = 2048;
         byte[] db = new byte [bufSize];
         while (l > 0) {
+            if (server.isFinishing()) {
+                break;
+            }
             long len = readImpl (db, 0, bufSize);
             if (len == -1) {
                 eof = true;
diff --git a/jdk/src/share/classes/sun/net/httpserver/ServerImpl.java b/jdk/src/share/classes/sun/net/httpserver/ServerImpl.java
index a5adbf609..271a5bbc7 100644
--- a/jdk/src/share/classes/sun/net/httpserver/ServerImpl.java
+++ b/jdk/src/share/classes/sun/net/httpserver/ServerImpl.java
@@ -219,6 +219,10 @@ class ServerImpl {
         return httpsConfig;
     }
 
+    public final boolean isFinishing() {
+        return finished;
+    }
+
     public void stop (int delay) {
         if (delay < 0) {
             throw new IllegalArgumentException ("negative delay parameter");
-- 
2.19.1

