似乎随机关闭套接字

时间:2017-08-25 04:00:23

标签: python sockets

我一直在寻找并处理这个问题一个星期。我有客户端代码导致select()返回一个实际上因外部原因而关闭的套接字抛出错误9 BAD FILE DESCRIPTOR,但是我测试了来自不同python文件的代码并且无法将其转换为错误。我尝试了一百万件事。这是服务器的一个片段:

注意:这将在几次迭代中起作用然后突然中断,它会在message_queue中出错,因为文件描述符会导致密钥错误,甚至消息/没有消息都有该套接字存在的密钥。

#Create the socket to communicate with uWSGI applications
server_address = ('localhost', 10001)
server = create_server_socket(server_address)
#Sockets which we expect to read on from select()
input_sockets = [server]
#Sockets which we expect to write to from select()
output_sockets = []
#Message buffer dicitonary for outgoing messages
message_queue = {}
#Now wait for connections endlessly
while input_sockets:
    print >> sys.stderr, "Waiting for the next event..."
    readable, writable, exceptional = select.select(input_sockets, output_sockets, input_sockets)
    #Handle input_sockets
    for s in readable:
        #Server socket is available for reading now
        if s is server:
            #Create a connection and address object when incoming request is recieved
            connection, client_addr = s.accept()
            print >> sys.stderr, "Connection recieved from %s!" % (client_addr,)
            #Set client connection to non blocking as well
            connection.setblocking(0)
            #Add this socket to input sockets as it will read for client data
            input_sockets.append(connection)
            #Give connection a queue for sending messages to it
            message_queue[connection] = Queue.Queue()
        #A client has sent data so we can handle its request
        else:
            #Pull data from the client
            data = ""
            try:
                while True:
                    message = s.recv(1024)
                    if not message:
                        break
                    data += message
            except Exception as e:
                print str(e)
            if data:
                #Readable client socket has data
                print >> sys.stderr, 'Recieved "%s" from %s' % (data, s.getpeername())
                message_queue[s].put(data)

                #Add output channel now to send message
                if s not in output_sockets:
                    output_sockets.append(s)
            #There is no data to be read, socket must be closed
            else:
                print >> sys.stderr, 'Closing', client_addr,'after recieving no data.'
                #Stop listening for input on the socket
                if s in output_sockets:
                    output_sockets.remove(s)
                input_sockets.remove(s)
                #Close the connection
                s.close()
                del message_queue[s]
    #Handle writable connections    
    for s in writable:
        if s:
            try:
                next_message = message_queue[s].get_nowait()
            except:
                print >> sys.stderr, 'No data to send for', s.getpeername()
                output_sockets.remove(s)
            else:
                try:
                    print >> sys.stderr, 'Sending "%s" to %s' % (next_message, s.getpeername())
                    s.sendall(next_message)
                except:
                    print >> sys.stderr, 'No data to send for', s.getpeername()
                    output_sockets.remove(s)
                #s.sendall('EOF:!@#$:EOF')
    #Now handle any exceptions
    for s in exceptional:
        print >> sys.stderr, 'Handling exception on ', s.getpeername()
        input_sockets.remove(s)
        if s in output_sockets:
            output_sockets.remove(s)
        s.close()
        #Remove any messages
        del message_queue[s]

客户端:

messages = [ 'This is the message. ',
         'It will be sent ',
         'in parts.',
         ]
server_address = ('localhost', 10001)

# Create a TCP/IP socket
socks = [ socket.socket(socket.AF_INET, socket.SOCK_STREAM),
          socket.socket(socket.AF_INET, socket.SOCK_STREAM),
          ]

# Connect the socket to the port where the server is listening
print >>sys.stderr, 'connecting to %s port %s' % server_address
for s in socks:
    s.connect(server_address)
for message in messages:

    # Send messages on both sockets
    for s in socks:
        print >>sys.stderr, '%s: sending "%s"' % (s.getsockname(), message)
        s.send(message)

    # Read responses on both sockets
    for s in socks:
        data = s.recv(1024)
        print >>sys.stderr, '%s: received "%s"' % (s.getsockname(), data)
        if not data:
            print >>sys.stderr, 'closing socket', s.getsockname()
            s.close()

注意:此客户端仅用于测试并开始传递消息。

1 个答案:

答案 0 :(得分:1)

当一个套接字以可读和可写方式返回时,代码中存在竞争,并且因为读取返回0字节而关闭套接字。在这种情况下,您从input_socketsoutput_socketsmessage_queue中删除套接字,但已关闭的套接字仍在writable中,因此它会尝试在同一次迭代中写入它选择循环。

我不知道这是否是你将看到的比赛,因为你既没有显示调试输出也没有你说你在哪里偶然发现这个EBADF。为了跟踪类似的问题,我建议使用关于套接字的位置以及尝试处理套接字的位置的更多调试信息来增加代码,因为它是可读或可写的,这样当您查看时,您实际上可以找到比赛的确切位置调试输出。