ValueError: filedescriptor out of range in select()

Laszlo Nagy gandalf at shopzeus.com
Tue Mar 17 10:04:22 EDT 2009


This is a long running process, written in Python. Only standard lib is 
used. This process accepts connections on TCP sockets, read/write data.

After about one day, it starts throwing this when I try to connect:

2009-03-17 09:49:50,096 INFO .accesspoint0 ('127.0.0.1', 55510) connecting
2009-03-17 09:49:50,097 ERROR .accesspoint0 Traceback (most recent call 
last):
  File 
"/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/accesspoints/srvtcp.py", 
line 34, in handle_request
    t = sorb.endpoint.SocketEndpoint(conn,self.router)
  File "/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/endpoint.py", 
line 304, in __init__
    StreamEndpoint.__init__(self,router)
  File "/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/endpoint.py", 
line 133, in __init__
    self.format = self.read_str() # determine remote format
  File "/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/endpoint.py", 
line 236, in read_str
    size = self.read_long()
  File "/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/endpoint.py", 
line 222, in read_long
    return struct.unpack(">q",self.read_data(8))[0]
  File "/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/endpoint.py", 
line 344, in read_data
    ready = select.select([fd], [], [], 0.2)
ValueError: filedescriptor out of range in select()


The code for read_data:

    def read_data(self,size):
        res = ""
        fd = self.socket.fileno()
        while not self.stop_requested.isSet():
            remaining = size - len(res)
            if remaining<=0:
                break
            # Give one second for an incoming connection so we can stop the
            # server in seconds when needed
            ready = select.select([fd], [], [], 0.2)
            if fd in ready[0]:
                data = self.socket.recv(min(remaining,8192)) # 8192 is 
recommended by socket.socket manual.
                if not data:
                    # select returns the fd but there is no data to read 
-> connection closed!
                    self.shutdown()
                    raise TransportClosedError("Connection closed.")
                else:
                    res += data
            else:
                pass
        if self.stop_requested.isSet():
            self.shutdown()
            raise TransportClosedError()
        return res


If I telnet this server this is what I see:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.


E.g. the connection is closed instantly after the connection has been 
made. For some reason, the client socket on the server side has an 
invalid fileno, and it cannot be passed to select.select(). I found some 
articles on the internet about this error, but nothing useful about how 
to solve it.

Please help.

   Laszlo




More information about the Python-list mailing list