ValueError: filedescriptor out of range in select()

Laszlo Nagy gandalf at
Tue Mar 17 15:04:22 CET 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 ('', 55510) connecting
2009-03-17 09:49:50,097 ERROR .accesspoint0 Traceback (most recent call 
line 34, in handle_request
    t = sorb.endpoint.SocketEndpoint(conn,self.router)
  File "/usr/local/www/vhosts/", 
line 304, in __init__
  File "/usr/local/www/vhosts/", 
line 133, in __init__
    self.format = self.read_str() # determine remote format
  File "/usr/local/www/vhosts/", 
line 236, in read_str
    size = self.read_long()
  File "/usr/local/www/vhosts/", 
line 222, in read_long
    return struct.unpack(">q",self.read_data(8))[0]
  File "/usr/local/www/vhosts/", 
line 344, in read_data
    ready =[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:
            # Give one second for an incoming connection so we can stop the
            # server in seconds when needed
            ready =[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!
                    raise TransportClosedError("Connection closed.")
                    res += data
        if self.stop_requested.isSet():
            raise TransportClosedError()
        return res

If I telnet this server this is what I see:

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 I found some 
articles on the internet about this error, but nothing useful about how 
to solve it.

Please help.


More information about the Python-list mailing list