title: JerseyClient SocketTimeoutException after less than a second to localhost
body: <p>Why do calls to another application running on the same machine (localhost) <em>sometimes</em> result in a SocketTimeoutException after less than a second? Calls to external endpoints on the internet timeout after 10 seconds roughly, and almost all calls to this application don't timeout.</p>
  <p>The application receiving the call continues to perform the operations normally after the timeout happens, and the calling application doesn't do anything after the call, so the error is benign. Still, this is concerning because I might one day want to wait for the call to finish and then process the data.</p>
  <p>The client uses the default timeout settings (timeout disabled).</p>
  <h1>Caller Code</h1>
  <pre><code>// client is an instance of the class JerseyClient
  WebTarget target = client.target(endpoint);
  Invocation.Builder builder = target.request().headers(headers);
  LOG.info(&quot;Sending HTTP request&quot;);
  Response response = builder.post(Entity.entity(body, MediaType.APPLICATION_JSON_TYPE));
  </code></pre>
  <h1>Caller Log</h1>
  <blockquote>
  <p>INFO  [2022-03-17 09:15:40] [jersey-server-managed-async-executor-132] MyAPI: Sending HTTP request<br />
  ERROR [2022-03-17 09:15:40] [jersey-server-managed-async-executor-132] MyAPI: Exception thrown<br />
  javax.ws.rs.ProcessingException: java.net.SocketTimeoutException: Read timed out<br />
  at io.dropwizard.client.DropwizardApacheConnector.apply(DropwizardApacheConnector.java:109)<br />
  at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:252)<br />
  at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:684)<br />
  at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:681)<br />
  at org.glassfish.jersey.internal.Errors.process(Errors.java:315)<br />
  at org.glassfish.jersey.internal.Errors.process(Errors.java:297)<br />
  at org.glassfish.jersey.internal.Errors.process(Errors.java:228)<br />
  at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)<br />
  at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681)<br />
  at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:437)<br />
  at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:343)<br />
  at MyAPI.sendPost(MyAPI.java:123)<br />
  Caused by: java.net.SocketTimeoutException: Read timed out<br />
  at java.net.SocketInputStream.socketRead0(Native Method)<br />
  at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)<br />
  at java.net.SocketInputStream.read(SocketInputStream.java:171)<br />
  at java.net.SocketInputStream.read(SocketInputStream.java:141)<br />
  at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:139)<br />
  at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:155)<br />
  at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:284)<br />
  at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140)<br />
  at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)<br />
  at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:261)<br />
  at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:165)<br />
  at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:167)<br />
  at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:272)<br />
  at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124)<br />
  at com.codahale.metrics.httpclient.InstrumentedHttpRequestExecutor.execute(InstrumentedHttpRequestExecutor.java:44)<br />
  at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)<br />
  at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)<br />
  at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)<br />
  at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)<br />
  at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)<br />
  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)<br />
  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)<br />
  at io.dropwizard.client.DropwizardApacheConnector.apply(DropwizardApacheConnector.java:87)<br />
  ... 42 more</p>
  </blockquote>
  <h1>Callee Code</h1>
  <pre><code>@POST
  @Timed
  @Path(&quot;/endpoint&quot;)
  public Response handleEvent(String body) throws Exception {
      LOG.info(String.format(&quot;handleEvent called. body = %s&quot;, body));
  
      // Do stuff...
  
      return Response.ok().entity(Status.OK).build();
  }
  </code></pre>