← Java / Spring · Contents

JAVA-064 / Intermediate

External API calls without timeouts (resource exhaustion)

A04:2021 Insecure Design · CWE-400 · CWE-772 Missing Release of Resource

Why is this a problem?

Delayed responses exhaust threads and stop the entire service.

How to fix it

java
RestTemplate rt = new RestTemplateBuilder()
  .setConnectTimeout(Duration.ofSeconds(3)).setReadTimeout(Duration.ofSeconds(5)).build();

Further reading