欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python提示[Errno 32]Broken pipe导致线程crash错误解决方法

程序员文章站 2022-05-24 19:04:34
本文实例讲述了python提示[errno 32]broken pipe导致线程crash错误解决方法。分享给大家供大家参考。具体方法如下: 1. 错误现象 threa...

本文实例讲述了python提示[errno 32]broken pipe导致线程crash错误解决方法。分享给大家供大家参考。具体方法如下:

1. 错误现象
threadinghttpserver 实现的 http 服务,如果客户端在服务器返回前,主动断开连接,则服务器端会报 [errno 32] broken pipe 错,并导致处理线程 crash.
下面先看个例子,python 版本: 2.7
示例代码

复制代码 代码如下:
#!/usr/bin/env python
#!coding=utf-8
 
import os
import time
import socket
import threading
from basehttpserver import httpserver ,basehttprequesthandler
from socketserver import threadingmixin
 
class requesthandler(basehttprequesthandler):
    def do_get(self):
        """
        处理get请求
        """      
        query=self.path
        print "query: %s thread=%s" % (query, str(threading.current_thread()))
        
        #ret_str="<html>" + self.path + "<br>" + str(self.server) + "<br>" + str(self.responses) +  "</html>"
        ret_str="<html>" + self.path + "<br>" + str(self.server) +  "</html>"
        
        time.sleep(5)
        
        try:
            self.send_response(200)
            self.send_header('content-type','text/html')
            self.end_headers()
            self.wfile.write(ret_str)
        except socket.error, e:
            print "socket.error : connection broke. aborting" + str(e)
            self.wfile._sock.close()  # close socket
            self.wfile._sock=none
            return false
       
        print "success prod query :%s" % (query)
        return true
 
#多线程处理
class threadinghttpserver(threadingmixin,httpserver):
    pass
    
if __name__ == '__main__':
    serveraddr = ('',9001)
 
    ser = threadinghttpserver(serveraddr,requesthandler)
    ser.serve_forever()
    sys.exit(0)

运行服务
./thread_http_server_error.py
第1次 curl ,等待返回
复制代码 代码如下:
[~]$curl -s 'http://10.232.41.142:9001/hello1′
<html>/hello1<br><__main__.threadinghttpserver instance at 0x37483b0></html>[~]$
此时服务器端输出日志如下:
$./thread_http_server_error.py
query: /hello1 thread=
search041142.sqa.cm4.tbsite.net – - [15/may/2014 15:02:27] “get /hello1 http/1.1″ 200 -
success prod query :/hello1

 第2次 curl ,不等待返回,ctrl +c 来模拟客户端断开
复制代码 代码如下:
[~]$curl -s 'http://10.232.41.142:9001/hello2′
[~]$ ctrl+c

此时服务器端输出日志如下:
复制代码 代码如下:
query: /hello2 thread=
search041142.sqa.cm4.tbsite.net – - [15/may/2014 15:33:10] “get /hello2 http/1.1″ 200 -
socket.error : connection broke. aborting[errno 32] broken pipe
—————————————-
exception happened during processing of request from ('10.232.41.142′, 48769)
traceback (most recent call last):
file “/home/wuzhu/tools/python_2_7_1/lib/python2.7/socketserver.py”, line 582, in process_request_thread
self.finish_request(request, client_address)
file “/home/wuzhu/tools/python_2_7_1/lib/python2.7/socketserver.py”, line 323, in finish_request
self.requesthandlerclass(request, client_address, self)
file “/home/wuzhu/tools/python_2_7_1/lib/python2.7/socketserver.py”, line 639, in __init__
self.handle()
file “/home/wuzhu/tools/python_2_7_1/lib/python2.7/basehttpserver.py”, line 337, in handle
self.handle_one_request()
file “/home/wuzhu/tools/python_2_7_1/lib/python2.7/basehttpserver.py”, line 326, in handle_one_request
 self.wfile.flush() #actually send the response if not already done.
file “/home/wuzhu/tools/python_2_7_1/lib/python2.7/socket.py”, line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
attributeerror: 'nonetype' object has no attribute 'sendall'

2. 原因分析

“[errno 32] broken pipe “ 产生的原因还是比较明确的,由于 client 在服务器返回前主动断开连接,所以服务器在返回时写 socket 收到sigpipe报错。虽然在我们的程序中也对异常进行了处理,将handler 的 wfile._sock 对象close 掉 ,但python 的库里basehttpserver.py中basehttprequesthandler 类的成员函数handle_one_request还是会直接调用 wfile.flush ,而没有判断 wfile 是否已经 close。

复制代码 代码如下:
def handle_one_request(self):
    """handle a single http request.
 
    you normally don't need to override this method; see the class
    __doc__ string for information on how to handle specific http
    commands such as get and post.
 
    """
    try:
        self.raw_requestline = self.rfile.readline(65537)
        if len(self.raw_requestline) > 65536:
            self.requestline = ''
            self.request_version = ''
            self.command = ''
            self.send_error(414)
            return
        if not self.raw_requestline:
            self.close_connection = 1
            return
        if not self.parse_request():
            # an error code has been sent, just exit
            return
        mname = 'do_' + self.command
        if not hasattr(self, mname):
            self.send_error(501, "unsupported method (%r)" % self.command)
            return
        method = getattr(self, mname)
        method()
        #没有判断 wfile 是否已经 close 就直接调用 flush()
        self.wfile.flush() #actually send the response if not already done.
    except socket.timeout, e:
        #a read or a write timed out.  discard this connection
        self.log_error("request timed out: %r", e)
        self.close_connection = 1
        return

3. 解决办法

只要在requesthandler重载其基类basehttprequesthandler的成员函数handle_one_reques(),在调用 wfile.flush() 前加上 wfile 是否已经 close 即可。

复制代码 代码如下:
#!/usr/bin/env python
#!coding=utf-8

import os
import time
import socket
import threading
from basehttpserver import httpserver ,basehttprequesthandler
from socketserver import threadingmixin

class requesthandler(basehttprequesthandler):
    
    def handle_one_request(self):
        """handle a single http request.
 
        you normally don't need to override this method; see the class
        __doc__ string for information on how to handle specific http
        commands such as get and post.
 
        """
        try:
            self.raw_requestline = self.rfile.readline(65537)
            if len(self.raw_requestline) > 65536:
                self.requestline = ''
                self.request_version = ''
                self.command = ''
                self.send_error(414)
                return
            if not self.raw_requestline:
                self.close_connection = 1
                return
            if not self.parse_request():
                # an error code has been sent, just exit
                return
            mname = 'do_' + self.command
            if not hasattr(self, mname):
                self.send_error(501, "unsupported method (%r)" % self.command)
                return
            method = getattr(self, mname)
            print "before call do_get"
            method()
            #增加 debug info 及 wfile 判断是否已经 close
            print "after call do_get"
            if not self.wfile.closed:
                self.wfile.flush() #actually send the response if not already done.
            print "after wfile.flush()"
        except socket.timeout, e:
            #a read or a write timed out.  discard this connection
            self.log_error("request timed out: %r", e)
            self.close_connection = 1
            return
    
    def do_get(self):
        """
        处理get请求
        """
        query=self.path
        print "query: %s thread=%s" % (query, str(threading.current_thread()))
 
        ret_str="<html>" + self.path + "<br>" + str(self.server) +  "</html>"
 
        time.sleep(5)
        
        try:
            self.send_response(200)
            self.send_header('content-type','text/html')
            self.end_headers()         
            self.wfile.write(ret_str)
        except socket.error, e:
            print "socket.error : connection broke. aborting" + str(e)
            self.wfile._sock.close()
            self.wfile._sock=none
            return false
       
        print "success prod query :%s" % (query)
        return true
 
#多线程处理
class threadinghttpserver(threadingmixin,httpserver):
    pass
    
if __name__ == '__main__':
    serveraddr = ('',9001)
 
    ser = threadinghttpserver(serveraddr,requesthandler)
    ser.serve_forever()
    sys.exit(0)


运行服务
./thread_http_server.py
curl ,不等待返回,ctrl +c 来模拟客户端断开
复制代码 代码如下:
[~]$curl -s 'http://10.232.41.142:9001/hello2'
[~]$ ctrl+c

此时服务器端输出日志如下:
复制代码 代码如下:
$./thread_http_server.pybefore call do_get
query: /hello2 thread=<thread(thread-1, started 1103210816)>
search041142.sqa.cm4.tbsite.net - - [15/may/2014 15:54:09] "get /hello2 http/1.1" 200 -
socket.error : connection broke. aborting[errno 32] broken pipe
after call do_get
after wfile.flush()

希望本文所述对大家的python程序设计有所帮助。