rmannibucau commented on PR #857:
URL: https://github.com/apache/tomcat/pull/857#issuecomment-2891074271
@markt-asf agree it fails similarly with `bindOnInit` or not set:
here is a reproducer:
```
package com.github.rmannibucau;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.valves.ErrorReportValve;
import org.apache.tomcat.util.modeler.Registry;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.nio.file.Files;
import java.nio.file.Path;
import static java.net.http.HttpResponse.BodyHandlers.discarding;
// leads to:
// WARNING: The acceptor thread [http-nio-8080-Acceptor] did not stop cleanly
//
// tested with java 17 and 24 with maven dep (+transitives):
// <dependency>
// <groupId>org.apache.tomcat</groupId>
// <artifactId>tomcat-catalina</artifactId>
// <version>11.0.7</version>
// </dependency>
public final class StartAndStopTomcat {
private StartAndStopTomcat() {
// no-op
}
public static void main(final String... args) throws LifecycleException,
InterruptedException, IOException {
Registry.disableRegistry();
// setup "Tomcat"
final var tomcat = new Tomcat();
tomcat.setBaseDir("target/work");
Files.createDirectories(Path.of("target/work"));
tomcat.setPort(8080);
final var host = new StandardHost();
host.setAutoDeploy(false);
host.setName("localhost");
tomcat.getEngine().addChild(host);
try {
tomcat.init();
} catch (final LifecycleException e) {
try {
tomcat.destroy();
} catch (final LifecycleException ex) {
// no-op
}
throw new IllegalStateException(e);
}
try {
tomcat.start();
} catch (final LifecycleException e) {
// "stop"
throw new IllegalStateException(e);
}
// tomcat.getConnector().setProperty("bindOnInit", "false");
// or next line which just enables to trigger the init of the
connector:
tomcat.getConnector();
// "create a context
final var context = new StandardContext();
context.setPath("");
context.setName("");
context.setFailCtxIfServletStartFails(true);
context.addLifecycleListener(new Tomcat.FixContextListener());
final var errorReportValve = new ErrorReportValve();
errorReportValve.setShowReport(false);
errorReportValve.setShowServerInfo(false);
context.getPipeline().addValve(errorReportValve);
// no need of all these checks in general since we use a flat
classpath
context.setClearReferencesThreadLocals(false);
context.setClearReferencesRmiTargets(false);
context.setClearReferencesHttpClientKeepAliveThread(false);
context.setClearReferencesStopThreads(false);
context.setClearReferencesStopTimerThreads(false);
context.setSkipMemoryLeakChecksOnJvmShutdown(true);
tomcat.getHost().addChild(context);
// ensure it is started
final var state = context.getState();
if (state == LifecycleState.STOPPED || state ==
LifecycleState.FAILED) {
// "stop"
throw new IllegalStateException("Context didn't start");
}
// do a request to use the acceptor
System.out.println("Started");
try (final var client = HttpClient.newHttpClient()) {
client.send(HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:8080")).build(),
discarding());
}
//
// stop
//
System.out.println("Stopping");
final var server = tomcat.getServer();
tomcat.stop();
tomcat.destroy();
if (server != null) { // give a change to stop the utility executor
otherwise it just leaks and stop later
final var utilityExecutor = server.getUtilityExecutor();
if (utilityExecutor != null) {
utilityExecutor.close();
}
}
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]