1.ãtcpãå
³äºtcp socketåºç°çâconnection reset by peerâåâbroken pipeâ
ãtcpãå ³äºtcp socketåºç°çâconnection reset by peerâåâbroken pipeâ
å¨socketéä¿¡è¿ç¨ä¸ï¼ç»å¸¸åç°å®¢æ·ç«¯æè æå¡å¨çæ¥å¿ä¸åºç°âbroken pipeâæè âconnection reset by peerâçé误æ示ã
以åä¸ç´ä»¥ä¸ºèªå·±ç解äºè¿ä¸¤ä¸ªé误å¼å¸¸æ示æå å«çæä¹ï¼èå®é ç解å®å ¨é误ã
æçé误ç解åä¸é¢è¿æ®µæ¥èªblogspotç表述差ä¸å¤ï¼
```
Maybe I'm just dumb,源码公开课 源码 but I always thought "broken pipe" meant, "the other end of this socket closed before I finished sending something" and "connection reset by peer" meant, well, roughly the same thing. (As well as indicating some slightly more esoteric problems.)
Turns out though, "broken pipe" actually means "I just tried to send something and the socket was already closed to sending."
So in the following example, if the other end of (TCP) socket "sock" closes or dies before the write method, "connection reset by peer" will be raised. The next write will give a broken-pipe error, since the socket now knows that further sending is invalid.
```
```
try:
sock.write('foo')
except:
pass # connection reset by peer
sock.write('bar') # broken pipe
```
RSTçæ å¿ä½ï¼è¿ä¸ªæ è¯ä¸ºå¨å¦ä¸å ç§æ åµä¸ä¼è¢«è®¾ç½®ï¼ä»¥ä¸æ¯æäºè§£çæ åµï¼å¯è½è¿ææ´å¤çåºæ¯ï¼æ²¡æéªè¯ï¼
1. å½å°è¯åæªå¼æ¾çæå¡å¨ç«¯å£å»ºç«tcpè¿æ¥æ¶ï¼æå¡å¨tcpå°ä¼ç´æ¥å客æ·ç«¯åéresetæ¥æ
2. åæ¹ä¹åå·²ç»æ£å¸¸å»ºç«äºéä¿¡ééï¼ä¹å¯è½è¿è¡è¿äºäº¤äºï¼å½æä¸æ¹å¨äº¤äºçè¿ç¨ä¸åçäºå¼å¸¸ï¼å¦å´©æºçï¼å¼å¸¸çä¸æ¹ä¼å对端åéresetæ¥æï¼éç¥å¯¹æ¹å°è¿æ¥å ³é
3. å½æ¶å°TCPæ¥æï¼ä½æ¯åç°è¯¥æ¥æä¸æ¯å·²å»ºç«çTCPè¿æ¥å表å¯å¤ççï¼åå ¶ç´æ¥å对端åéresetæ¥æ
4. ackæ¥æ丢失ï¼å¹¶ä¸è¶ åºä¸å®çéä¼ æ¬¡æ°ææ¶é´åï¼ä¼ä¸»å¨å对端åéresetæ¥æéæ¾è¯¥TCPè¿æ¥
å ¶å®æ们javaå¼å¸¸éçå°çBroken pipeæè Connection reset by peerä¿¡æ¯ä¸æ¯jdkæè jvméå®ä¹çï¼æçå°è¿äºå ³é®åå¾å¾ä¼é¦å æç´¢ä¸jdkæè hotspotæºç æ¾å°ä½ç½®è¿è¡ä¸ä¸æåæï¼ä½æ¯æ²¡æ¾å°ï¼åé¢ææ³å°åºè¯¥æ¯Linuxæè glibcéå®ä¹çï¼æç¶å¨glibcéçå°äºå¦ä¸çæè¿°åå®ä¹ã
对äºBroken pipeå¨ç®¡éçå¦å¤ä¸ç«¯æ²¡æè¿ç¨å¨è¯»çæ¶åå°±ä¼æåºæ¤å¼å¸¸ï¼Connection reset by peerçæè¿°å ¶å®ä¸æ¯å¾æ£ç¡®ï¼ä»æçå®è·µæ¥çåªæè¿°äºä¸æ¹é¢ï¼å ¶å®å¨æä¸ç«¯æ£å¸¸closeä¹åï¼ä¹æ¯å¯è½ä¼ææ¤å¼å¸¸çã
connection reset by peerâåâbroken pipeâåºç°çåºæ¯ï¼
1ï¼å¾ä¸ä¸ªå¯¹ç«¯å·²ç»closeçééåæ°æ®çæ¶åï¼å¯¹æ¹çtcpä¼æ¶å°è¿ä¸ªæ¥æï¼å¹¶ä¸åé¦ä¸ä¸ªresetæ¥æãå½æ¶å°resetæ¥æçæ¶åï¼ç»§ç»åselect读æ°æ®çæ¶åå°±ä¼æåºConnect reset by peerçå¼å¸¸ï¼ã
2ï¼å½ç¬¬ä¸æ¬¡å¾ä¸ä¸ªå¯¹ç«¯å·²ç»closeçééåæ°æ®çæ¶åä¼åä¸é¢çæ åµä¸æ ·ï¼ä¼æ¶å°resetæ¥æãå½å次å¾è¿ä¸ªsocketåæ°æ®çæ¶åï¼å°±ä¼æåºBroken pipeäº ãæ ¹æ®tcpç约å®ï¼å½æ¶å°resetå çæ¶åï¼ä¸å±å¿ é¡»è¦ååºå¤çï¼è°ç¨å°socketæ件æ述符è¿è¡å ³éï¼å ¶å®ä¹æå³çpipeä¼å ³éï¼å æ¤ä¼æåºè¿ä¸ªé¡¾åæä¹çå¼å¸¸ã
ä»tcpåçè§åº¦ç解Broken pipeåConnection Reset by Peerçåºå«
/blog////tcp-broken-pipe
å ³äºtcp socketåºç°çâconnection reset by peerâåâbroken pipeâ
////%E5%%B3%E4%BA%8Etcp-socket%E5%%BA%E7%8E%B0%E7%9A%connection-reset-by-peer%E5%%8Cbroken-pipe