1

I use embedded Jetty to host a WEB server. Requests are forwarded (by my internal implementation) to code from dynamically loaded JARs. At some time I need to hot-update in-memory JARs and for that purpose I wait for requests to complete, but it may happen that either because of programming mistake or because of actual long-running action waiting goes beyond reasonable time.

I'd like to cancel processing of WEB request after some timeout.
What can I do in such case with Jetty (v.8+)?

PS: I'm aware about Thread.stop() over the thread with stuck WEB request and it does not seem to be a good approach.

Xtra Coder
  • 3,389
  • 4
  • 40
  • 59
  • As with any cancellation logic, you need to decide yourself how to handle cancellation. This includes the means by which you deliver the cancellation message, as well as what to do in case of cancellation. After all, "graceful" is by nature a subjective term. As to deadlocks take a look at http://stackoverflow.com/questions/1849972/how-to-kill-deadlocked-threads-in-java – D Levant Jun 15 '16 at 12:23

2 Answers2

2

You could write a Jetty HandlerWrapper that uses a ScheduledExecutorService to interrupt the request thread if it hasn't completed after a certain period of time. See here for an example:

https://github.com/outbrain/ob1k/blob/master/ob1k-jetty/src/main/java/com/outbrain/ob1k/server/jetty/handler/RequestTimeoutHandler.java

ck1
  • 5,243
  • 1
  • 21
  • 25
  • Interrupting the request thread is *dangerous* and not a valid solution (you will cause internal state issues doing that) – Joakim Erdfelt Jun 15 '16 at 16:33
  • Fixed link, with the commit from the date this answer was posted: https://github.com/outbrain/ob1k/blob/51f1ab4eb4a9fa7a8b458088aaf363703ade1509/ob1k-jetty/src/main/java/com/outbrain/ob1k/server/jetty/handler/RequestTimeoutHandler.java – henry700 Nov 11 '22 at 13:03
0

The only valid cancellation of a request is by terminating the connection and/or endpoint.

This is typically (and easily) done via a idle timeout.

For doing hot-swap of a jar, close (or stop) the connector, wait for all of the requests / connection to idle timeout, swap your jars, then restart the connector.

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • 1
    Seems to be a good choice except cases when request-handling code is just stuck in endless cycle and is not sending anything to output. – Xtra Coder Jun 15 '16 at 20:07