2

Getting java.net.SocketException: Broken pipe (Write failed) while sending json payload arround size of 3.2 mb in size but its intermittent. However for small size it's working. The code is running on jetty server.

I thought there may be memory issue after upgrading server memory with heap size arround 7GB and verified while running this job memory and cpu utilization was normal but still it is failing.

Also we try with modifying below sysctl setting on jetty server but still no luck sometimes get processed successfully but from few days it continously failing.

   sysctl -w net.core.rmem_max=16777216  sysctl -w
   net.core.wmem_max=16777216  sysctl -w net.ipv4.tcp_rmem="4096 87380
   16777216"  sysctl -w net.ipv4.tcp_wmem="4096 16384 16777216"   sysctl
   -w net.core.somaxconn=4096   sysctl -w net.core.netdev_max_backlog=16384  sysctl -w net.ipv4.tcp_max_syn_backlog=8192 

Below is code that we are using to call rest api:

   RestClient restTemplate = null;
   HttpEntity<String> entity = new HttpEntity<String>(payload, headers);
   ResponseEntity<String> responseEntity = null;try
    {
        restTemplate = new RestClient(null, null, accessKey, secretKey);
        responseEntity = restTemplate.exchange(url, method, entity, String.class);
        return responseEntity;
    }catch(
    Exception ex)
    {

    }

    // contructor setting
    public RestClient(final String proxyHost, final String proxyPort, final String username, final String password) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(username, password));
        HttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
        setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
    }

Any idea how i can fix this issue, i am struggling past more than 20 days. Please let me know if you need any more input.

1 Answer 1

2

After going through lots of articles I have found a solution to the problem. This is caused by:

  • most usually, writing to a connection when the other end has already closed it;
  • less usually, the peer closing the connection without reading all the data that is already pending at his end.

I found one useful article here: https://blog.stackpath.com/glossary-keep-alive/

It is working only by just adding in header below line:

headers.add(new BasicHeader(HttpHeaders.CONNECTION, "close"));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.