Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8233185

HttpServer.stop() blocks indefinitely when called on dispatch thread

    XMLWordPrintable

    Details

    • Subcomponent:
    • Introduced In Version:
      9
    • Resolved In Build:
      b26
    • CPU:
      x86_64
    • OS:
      windows_10

      Backports

        Description

        ADDITIONAL SYSTEM INFORMATION :
        Microsoft Windows 10 Enterprise (Version: 10.0.16299 Build 16299)
        OpenJDK Runtime Environment (build 14-ea+17-721)

        A DESCRIPTION OF THE PROBLEM :
        When calling HttpServer.stop( int delay ) on the dispatch thread of the HTTP server, the method call blocks indefinitely, and does not return after approximately <delay> seconds (as per Javadoc).

        REGRESSION : Last worked in version 8

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        1) Compile and start the attached application.
        2) Open the URL "http://localhost:8889/context" in your browser (on the same machine on which you have started your application).

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The HTTP server is shut down, and "HTTP server stopped" is printed on System.out.
        ACTUAL -
        The dispatch thread blocks indefinitely:

        Thread [HTTP-Dispatcher] (id=23)
        waiting for: Thread (id=23)
        Object.wait(long) line: not available [native method]
        Thread.join(long) line: 1303
        Thread.join() line: 1371
        ServerImpl.stop(int) line: 215
        HttpServerImpl.stop(int) line: 70
        HttpServerTest.handle(HttpExchange) line: 38
        Filter$Chain.doFilter(HttpExchange) line: 77
        AuthFilter.doFilter(HttpExchange, Filter$Chain) line: 82
        Filter$Chain.doFilter(HttpExchange) line: 80
        ServerImpl$Exchange$LinkHandler.handle(HttpExchange) line: 692
        Filter$Chain.doFilter(HttpExchange) line: 77
        ServerImpl$Exchange.run() line: 664
        ServerImpl$DefaultExecutor.execute(Runnable) line: 159
        ServerImpl$Dispatcher.handle(SocketChannel, HttpConnection) line: 442
        ServerImpl$Dispatcher.run() line: 408
        Thread.run() line: 830


        ---------- BEGIN SOURCE ----------
        package com.test;

        import java.io.IOException;
        import java.io.OutputStream;
        import java.net.InetSocketAddress;

        import com.sun.net.httpserver.HttpExchange;
        import com.sun.net.httpserver.HttpHandler;
        import com.sun.net.httpserver.HttpServer;

        public class HttpServerTest implements HttpHandler
        {
            
            private static final int HTTP_STATUS_CODE_OK = 200;
            
            private final HttpServer server;
            
            public HttpServerTest( HttpServer server )
            {
                this.server = server;
            }
            
            public static void main( String[] args ) throws IOException
            {
                HttpServer server = HttpServer.create( new InetSocketAddress( 8889 ), 0 );
                server.createContext( "/context", new HttpServerTest( server ) ); //$NON-NLS-1$
                server.start();
            }
            
            @Override
            public void handle( HttpExchange exchange ) throws IOException
            {
                System.out.println( "Stopping HTTP server..." ); //$NON-NLS-1$
                
                // Send response prior to executing, as the server shuts down and the response would not be emitted otherwise
                sendHttpStatusCode( HTTP_STATUS_CODE_OK, exchange );
                
                server.stop( 1 );
                
                System.out.println( "HTTP server stopped" ); //$NON-NLS-1$
            }
            
            private void sendHttpStatusCode( int httpCode, HttpExchange arg )
            {
                try
                {
                    arg.sendResponseHeaders( httpCode, 0 );
                    OutputStream out = arg.getResponseBody();
                    out.close();
                }
                catch ( IOException e )
                {
                    e.printStackTrace( System.err );
                }
            }
            
        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Calling HttpServer.stop() on a different thread

        FREQUENCY : always


          Attachments

            Issue Links

              Activity

                People

                Assignee:
                jboes Julia Boes
                Reporter:
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved: