Skip to content

Repeated WebServer creation/stopping breaks after a couple of iterations. #141

@bpicode

Description

@bpicode

Affects version: 2.105

When I repeatedly create/stop a WebServer, at some point the created WebServers are unable to dispatch connection attempts.

Here is a test to reproduce:

package testing;

import lombok.extern.slf4j.Slf4j;
import net.codestory.http.WebServer;
import net.codestory.http.routes.NoParamRouteWithContext;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

@Slf4j
public class WebServerTest {

    @Test
    public void testName() throws Exception {
        for (int i = 0; i < 100; i++) {
            WebServer webServer = new WebServer()
                    .configure(routes -> routes.url("/").get((NoParamRouteWithContext) context -> "responseText"))
                    .start();
            URLConnection urlConnection = new URL("http://localhost:" + webServer.port() + "/").openConnection();
            urlConnection.connect();
            try (InputStream is = urlConnection.getInputStream()) {
                log.info("RUN {} :: read :: {}", i, IOUtils.toString(is));
            } finally {
                webServer.stop();
            }
        }
    }
}

In my case I get an error at the 24th attempt:

Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:39.063 [main] INFO testing.WebServerTest - RUN 0 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:39.364 [main] INFO testing.WebServerTest - RUN 1 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:39.529 [main] INFO testing.WebServerTest - RUN 2 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:39.764 [main] INFO testing.WebServerTest - RUN 3 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:39.876 [main] INFO testing.WebServerTest - RUN 4 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:40.149 [main] INFO testing.WebServerTest - RUN 5 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:40.407 [main] INFO testing.WebServerTest - RUN 6 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:40.538 [main] INFO testing.WebServerTest - RUN 7 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:40.778 [main] INFO testing.WebServerTest - RUN 8 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:40.991 [main] INFO testing.WebServerTest - RUN 9 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:41.074 [main] INFO testing.WebServerTest - RUN 10 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:41.155 [main] INFO testing.WebServerTest - RUN 11 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:41.261 [main] INFO testing.WebServerTest - RUN 12 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:41.351 [main] INFO testing.WebServerTest - RUN 13 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:41.437 [main] INFO testing.WebServerTest - RUN 14 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:41.512 [main] INFO testing.WebServerTest - RUN 15 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:41.623 [main] INFO testing.WebServerTest - RUN 16 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:41.702 [main] INFO testing.WebServerTest - RUN 17 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:41.746 [main] INFO testing.WebServerTest - RUN 18 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:41.797 [main] INFO testing.WebServerTest - RUN 19 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:41.861 [main] INFO testing.WebServerTest - RUN 20 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:41.921 [main] INFO testing.WebServerTest - RUN 21 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...
20:32:41.989 [main] INFO testing.WebServerTest - RUN 22 :: read :: responseText
Dev mode. Start with -DPROD_MODE=true to run in production mode.
Server started on port 8080
Reloading configuration...

java.net.SocketException: Unexpected end of file from server
	at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:792)
	at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
	at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:789)
	at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1569)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
	at testing.WebServerTest.testName(WebServerTest.java:24)

Note that the same effects are observed if one starts the server on a random port.
Any help is appreciated.

Greets

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions