-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async requests are not considered when shutting down gracefully #2717
Comments
Interesting. Do you have a testcase or demo project demonstrating this we can use? |
This should reproduce the behaviour that I've described with 9.4.11: package com.example;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.StatisticsHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
public class JettyGracefulShutdown {
private static CountDownLatch latch = new CountDownLatch(1);
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
server.setStopTimeout(30000);
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.setContextPath("/");
contextHandler.addServlet(AsyncServlet.class, "/async").setAsyncSupported(true);
contextHandler.addServlet(SyncServlet.class, "/sync").setAsyncSupported(true);
server.setHandler(contextHandler);
StatisticsHandler handler = new StatisticsHandler();
handler.setHandler(server.getHandler());
server.setHandler(handler);
server.start();
System.out.println("Waiting for a request to /sync or /async");
latch.await();
System.out.println("Request received. Shutting down");
long start = System.currentTimeMillis();
server.stop();
long duration = System.currentTimeMillis() - start;
System.out.println("Shutdown complete in " + duration + "ms");
}
public static class AsyncServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
AsyncContext context = req.startAsync();
context.start(() -> {
latch.countDown();
try {
Thread.sleep(10000);
}
catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
context.complete();
});
}
}
public static class SyncServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
latch.countDown();
try {
Thread.sleep(10000);
}
catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
} Start the server and then make a request to
It takes just over 10 seconds as expected. Start the server again and make a request to
The connection's dropped, resulting in an empty reply almost immediately. Note that the shutdown still takes just over 10 seconds to complete so something's waiting for the request to complete. When it does complete It tries to send the response but it fails because the connection's already been dropped:
|
Uhm. Don't you have a What happens if you first try |
I guess you haven't tried running the example. Calling |
Now I ran the example 😃 To discuss your original intent, shutting down gracefully a server that may be bombed with requests is kind of hard, but here's what I suggest:
Unfortunately, @wilkinsona if you don't have 2. then it may be that |
…shutdown Fixes #2717 - Async requests are not considered when shutting down gracefully.
I'm trying to enhance Spring Boot's Jetty integration to optionally gracefully shut down Jetty. It's working nicely for synchronous requests but does not work for asynchronous requests. If I call
StatisticsHandler.shutdown
when there's a single asynchronous request in progress, the returned future is completed immediately rather than only completing once the request has completed.Here's the stats HTML captured at the time of
StatisticsHandler
callingshutdown.succeeded()
:The text was updated successfully, but these errors were encountered: