1

I've been trying to find a tomcat property for defining a request timeout. Specifically, I was looking for a way to limit the amount of time the request takes from the server side.

After doing some research, it seems it is not possible and

there is no way to guarantee that [a timeout] as you cannot force the thread that is handling the request to stop.

This feels by design, which makes me wonder why.

What are the reasons against having such a property? Or is it a technical limitation of the architecture?

PS: I found that the same thing holds true for Tomcat, Httpd, and apparently wakanda? which is why my question is about servers in general. Please correct me if I'm wrong in that assumption.

Miss Skooter
  • 803
  • 10
  • 22

1 Answers1

2

There's no mechanism to stop a thread in Java

There used to be such mechanism (Thread.stop()), but it wasn't safe as it could leave objects in inconsistent state. This is because if threads can stopped abruptly, you can't guarantee happens-before behavior. Because the thread could've stopped right in the middle of a critical section, see Why is Thread.stop deprecated?.

Instead politely ask to stop

You can try to Thread.interrupt(), but it's up to your business logic to check if that Thread.isInterrupted(). Do you? Probably not. So even if you could set some timeout, in a lot of cases it wouldn't work.

Most web apps are actually DB-bound. Meaning, they spend most of the time doing some requests. If you really want to limit request time and your app is also DB-bound, you could set some timeouts on that level. E.g. C3P0 provides some timeouts like unreturnedConnectionTimeout, checkoutTimeout and DBs like PostgeSQL provides all sorts of options like statement_timeout.

Same with any other communication over the network - if you don't want the risk of hanging because of firewalls or other reasons, you need to set up connectionTimeout and socketTimeout. Tools like Spring's RestTemplate have such options. This will help with the cases when the other side doesn't respond. But it won't help if the other side responds very slowly, gradually.

Stanislav Bashkyrtsev
  • 14,470
  • 7
  • 42
  • 45
  • Thank you for your answer. I understand that there is no mechanism. My question is more as to why. And I wasn't necessarily thinking about DBs (though thank you for the info), also like what if your server needs to communicate with some other service and it hangs? – Miss Skooter Aug 17 '23 at 18:35
  • Added the info. I think you misunderstand - there's no mechanism for this **in Java**. – Stanislav Bashkyrtsev Aug 18 '23 at 07:11
  • And the reason that there is no mechanism in Java ... because nobody has been able to figure out to implement `Thread.stop` etcetera safely. – Stephen C Aug 18 '23 at 07:14
  • Thank you for the clarification, I did misunderstand :) – Miss Skooter Aug 18 '23 at 07:19
  • To be clear though, when you talk about `connectionTimeout`, `socketTimeout`, and `RestTemplate` you're talking about client side right? – Miss Skooter Aug 18 '23 at 07:53
  • 1
    @MissSkooterstandswPalestine, yes, you can't fully control these things on the server side because the culprit of long connection could be in between (firewalls dropping packages). – Stanislav Bashkyrtsev Aug 18 '23 at 08:25