解决Java中的java.io.IOException: Broken pipe问题
java 中java.io.ioexception: broken pipe
认识broken pipe
pipe是管道的意思,管道里面是数据流,通常是从文件或网络套接字读取的数据。
当该管道从另一端突然关闭时,会发生数据突然中断,即是broken。
对于文件file来说,这可能是文件安装在已断开连接的光盘或远程网络上。
对于socket来说,可能是网络被拔出或另一端的进程崩溃。
在java中,没有具体的brokenpipeexception。
将此类错误包含在另一个异常,例如java.io.ioexception:broken pipe
解决问题
其实当该异常产生的时候,对于服务端来说,并没有多少影响。因为可能是某个客户端突然中止了进程导致了该错误。但是为了程序能够美观、优雅,还是要在合适的地方捕获该异常,并处理一下。
还有一种可能性就是程序里面有代码执行时间过长,用户等待时间过久,才会强行中止进程。
那么这种情况下就需要去分析log中报异常的地方是经常发生在什么地方,合理优化该段代码,提升代码的运行速度,才能从根本上避免此类问题的再次发生。
可能原因:
1、客户端请求服务器数据,服务器突然挂了;
2、客户端请求服务端数据,服务端正常返回,凡是客户端由于超时等原因断开。
原因分析:
做了压力测试,发现遇到的情况属于第二种,由于去数据库取数据的时候,30秒里无法正常获取有效的数据库连接,或者查询时间长,导致osb过来的请求超时,也就是客户端超时了,服务端还试图返回数据,结果导致如题异常。
解决方法:
1、增大数据库连接池数量;
2、延长客户端超时时间。
java.io.ioexception: broken pipe 线上问题记录
今天在上线前staging环境验证时遇到这样一个问题java.io.ioexception: broken pipe,操作服务刚起来之后,测试人员第一次进行更配[post]操作,结果操作失败,但是第二次就好了,
我们是springboot+angularjs前后端分离,然后服务使用k8s在容器中部署,原始的异常栈信息是这样,
2019-09-26t02:42:09z [http-nio-8080-exec-9] warn [org.springframework.web.servlet.mvc.method.annotation.exceptionhandlerexceptionresolver:411] - failed to invoke @exceptionhandler method: public org.springframework.http.responseentity<com.inspur.common.exception.errorresponse> com.inspur.common.exception.commoncontrolleradvice.exceptionhandler(javax.servlet.http.httpservletrequest,java.lang.exception) org.apache.catalina.connector.clientabortexception: java.io.ioexception: broken pipe at org.apache.catalina.connector.outputbuffer.doflush(outputbuffer.java:321) at org.apache.catalina.connector.outputbuffer.flush(outputbuffer.java:284) at org.apache.catalina.connector.coyoteoutputstream.flush(coyoteoutputstream.java:118) at org.springframework.security.web.util.oncommittedresponsewrapper$savecontextservletoutputstream.flush(oncommittedresponsewrapper.java:514) at com.fasterxml.jackson.core.json.utf8jsongenerator.flush(utf8jsongenerator.java:1100) at com.fasterxml.jackson.databind.objectwriter.writevalue(objectwriter.java:915) at org.springframework.http.converter.json.abstractjackson2httpmessageconverter.writeinternal(abstractjackson2httpmessageconverter.java:285) at org.springframework.http.converter.abstractgenerichttpmessageconverter.write(abstractgenerichttpmessageconverter.java:102) at org.springframework.web.servlet.mvc.method.annotation.abstractmessageconvertermethodprocessor.writewithmessageconverters(abstractmessageconvertermethodprocessor.java:272) at org.springframework.web.servlet.mvc.method.annotation.httpentitymethodprocessor.handlereturnvalue(httpentitymethodprocessor.java:224) at org.springframework.web.method.support.handlermethodreturnvaluehandlercomposite.handlereturnvalue(handlermethodreturnvaluehandlercomposite.java:82) at org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:119) at org.springframework.web.servlet.mvc.method.annotation.exceptionhandlerexceptionresolver.doresolvehandlermethodexception(exceptionhandlerexceptionresolver.java:400) at org.springframework.web.servlet.handler.abstracthandlermethodexceptionresolver.doresolveexception(abstracthandlermethodexceptionresolver.java:61) at org.springframework.web.servlet.handler.abstracthandlerexceptionresolver.resolveexception(abstracthandlerexceptionresolver.java:136) at org.springframework.web.servlet.handler.handlerexceptionresolvercomposite.resolveexception(handlerexceptionresolvercomposite.java:78) at org.springframework.web.servlet.dispatcherservlet.processhandlerexception(dispatcherservlet.java:1255) at org.springframework.web.servlet.dispatcherservlet.processdispatchresult(dispatcherservlet.java:1062) at org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:1008) at org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:925) at org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:974) at org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:866) at javax.servlet.http.httpservlet.service(httpservlet.java:687) at org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:851) at javax.servlet.http.httpservlet.service(httpservlet.java:790) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:231) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at com.inspur.common.filter.repeatedlyreadfilter.dofilter(repeatedlyreadfilter.java:34) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at com.inspur.eip.config.filter.keyclockauthfilter.dofilter(keyclockauthfilter.java:53) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at com.alibaba.druid.support.http.webstatfilter.dofilter(webstatfilter.java:123) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at com.alibaba.druid.support.http.webstatfilter.dofilter(webstatfilter.java:123) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at org.springframework.boot.actuate.web.trace.servlet.httptracefilter.dofilterinternal(httptracefilter.java:90) at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at com.inspur.iam.adapter.filter.securitycontextfilter.dofilter(securitycontextfilter.java:119) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:320) at org.springframework.security.web.access.intercept.filtersecurityinterceptor.invoke(filtersecurityinterceptor.java:127) at org.springframework.security.web.access.intercept.filtersecurityinterceptor.dofilter(filtersecurityinterceptor.java:91) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.access.exceptiontranslationfilter.dofilter(exceptiontranslationfilter.java:119) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.session.sessionmanagementfilter.dofilter(sessionmanagementfilter.java:137) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.authentication.anonymousauthenticationfilter.dofilter(anonymousauthenticationfilter.java:111) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.keycloak.adapters.springsecurity.filter.keycloakauthenticatedactionsfilter.dofilter(keycloakauthenticatedactionsfilter.java:74) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.keycloak.adapters.springsecurity.filter.keycloaksecuritycontextrequestfilter.dofilter(keycloaksecuritycontextrequestfilter.java:77) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.servletapi.securitycontextholderawarerequestfilter.dofilter(securitycontextholderawarerequestfilter.java:170) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.savedrequest.requestcacheawarefilter.dofilter(requestcacheawarefilter.java:63) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.keycloak.adapters.springsecurity.filter.keycloakauthenticationprocessingfilter.successfulauthentication(keycloakauthenticationprocessingfilter.java:208) at org.springframework.security.web.authentication.abstractauthenticationprocessingfilter.dofilter(abstractauthenticationprocessingfilter.java:240) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.authentication.logout.logoutfilter.dofilter(logoutfilter.java:116) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.keycloak.adapters.springsecurity.filter.keycloakpreauthactionsfilter.dofilter(keycloakpreauthactionsfilter.java:84) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.header.headerwriterfilter.dofilterinternal(headerwriterfilter.java:66) at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.context.securitycontextpersistencefilter.dofilter(securitycontextpersistencefilter.java:105) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.context.request.async.webasyncmanagerintegrationfilter.dofilterinternal(webasyncmanagerintegrationfilter.java:56) at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.filterchainproxy.dofilterinternal(filterchainproxy.java:215) at org.springframework.security.web.filterchainproxy.dofilter(filterchainproxy.java:178) at org.springframework.web.filter.delegatingfilterproxy.invokedelegate(delegatingfilterproxy.java:357) at org.springframework.web.filter.delegatingfilterproxy.dofilter(delegatingfilterproxy.java:270) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at org.springframework.web.filter.requestcontextfilter.dofilterinternal(requestcontextfilter.java:99) at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at org.springframework.web.filter.httpputformcontentfilter.dofilterinternal(httpputformcontentfilter.java:109) at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at org.springframework.web.filter.hiddenhttpmethodfilter.dofilterinternal(hiddenhttpmethodfilter.java:93) at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at org.springframework.boot.actuate.metrics.web.servlet.webmvcmetricsfilter.filterandrecordmetrics(webmvcmetricsfilter.java:155) at org.springframework.boot.actuate.metrics.web.servlet.webmvcmetricsfilter.filterandrecordmetrics(webmvcmetricsfilter.java:123) at org.springframework.boot.actuate.metrics.web.servlet.webmvcmetricsfilter.dofilterinternal(webmvcmetricsfilter.java:108) at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at org.springframework.web.filter.characterencodingfilter.dofilterinternal(characterencodingfilter.java:200) at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:198) at org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:96) at org.keycloak.adapters.tomcat.abstractauthenticatedactionsvalve.invoke(abstractauthenticatedactionsvalve.java:67) at org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:493) at org.keycloak.adapters.tomcat.abstractkeycloakauthenticatorvalve.invoke(abstractkeycloakauthenticatorvalve.java:181) at org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:140) at org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:81) at org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:87) at org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:342) at org.apache.coyote.http11.http11processor.service(http11processor.java:800) at org.apache.coyote.abstractprocessorlight.process(abstractprocessorlight.java:66) at org.apache.coyote.abstractprotocol$connectionhandler.process(abstractprotocol.java:806) at org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1498) at org.apache.tomcat.util.net.socketprocessorbase.run(socketprocessorbase.java:49) at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1149) at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:624) at org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) at java.lang.thread.run(thread.java:748) caused by: java.io.ioexception: broken pipe at sun.nio.ch.filedispatcherimpl.write0(native method) at sun.nio.ch.socketdispatcher.write(socketdispatcher.java:47) at sun.nio.ch.ioutil.writefromnativebuffer(ioutil.java:93) at sun.nio.ch.ioutil.write(ioutil.java:65) at sun.nio.ch.socketchannelimpl.write(socketchannelimpl.java:471) at org.apache.tomcat.util.net.niochannel.write(niochannel.java:134) at org.apache.tomcat.util.net.nioblockingselector.write(nioblockingselector.java:101) at org.apache.tomcat.util.net.nioselectorpool.write(nioselectorpool.java:157) at org.apache.tomcat.util.net.nioendpoint$niosocketwrapper.dowrite(nioendpoint.java:1306) at org.apache.tomcat.util.net.socketwrapperbase.dowrite(socketwrapperbase.java:726) at org.apache.tomcat.util.net.socketwrapperbase.flushblocking(socketwrapperbase.java:679) at org.apache.tomcat.util.net.socketwrapperbase.flush(socketwrapperbase.java:669) at org.apache.coyote.http11.http11outputbuffer$socketoutputbuffer.flush(http11outputbuffer.java:646) at org.apache.coyote.http11.filters.chunkedoutputfilter.flush(chunkedoutputfilter.java:169) at org.apache.coyote.http11.http11outputbuffer.flush(http11outputbuffer.java:252) at org.apache.coyote.http11.http11processor.flush(http11processor.java:1561) at org.apache.coyote.abstractprocessor.action(abstractprocessor.java:380) at org.apache.coyote.response.action(response.java:173) at org.apache.catalina.connector.outputbuffer.doflush(outputbuffer.java:317) ... 127 more resolve exception
之后的操作,就正常了,正常日志如下:
google之后大概知道是什么问题了
注:读懂下面这句话,首先要熟悉tcp 四次挥手,不太熟悉 请右转google
总结 broken pipe:
这个异常是客户端读取超时关闭了连接,这时候服务器端再向客户端已经断开的连接写数据时就发生了broken pipe异常!
作为一个后端工程师,一定要对日常的一些异常有一个积累总结,笔者就自己有在总结,一是总结整理之后,下次遇到同样的问题自己不会很茫然不知所措,尤其是在上线是,其他同事都在等着你解决问题的时候,你吭吭唧唧那就不太好了,二是笔者之前在面试时也有经常被问到类似的问题,记忆尤新的就是被云校的百度小哥问到connection by peer,之前博客也有介绍,作为社招有经验的工程师,更不能再以校招的面试作为自己的标准了,要有自己的经验积累,反正就是,干,就完了
最后,附一张我的错误记事本截图
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
推荐阅读
-
使用java8中的Optional API解决nullPointException问题
-
java web中的中文乱码问题和解决方法
-
解决: java.io.IOException: 打开的文件过多 的问题
-
解决AndroidStudio无法运行java中的mian方法问题
-
OY!原来是JAVA的BUG呀~~~在SwingWorker中嵌套调用SwingWorker的死锁问题解决方法。
-
解决IDEA和CMD中java命令提示错误: 找不到或无法加载主类的问题
-
Jedis Unexpected end of stream & java.net.SocketException: Broken pipe问题解决思路
-
基于Java中Stack类实现的Hanoi塔问题解决算法
-
解决java main函数中的args数组传值问题
-
解决Java中的java.io.IOException: Broken pipe问题