简体   繁体   中英

java.net.SocketException: Broken pipe (Write failed) while posting json payload to rest api

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.

After going through lots of articals. Now, 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 artical 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"));

Hi I have a similiar issue but my error is only on prod environment not on the local or test environment so I cannot be sure that your solution it will work for me. So can you check my error is same with your error. You can check my error here :

Broken pipe (Write failed) while posting json payload to rest api

Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM