[ python-Bugs-1705393 ] Select() failure (race condition)

SourceForge.net noreply at sourceforge.net
Mon Apr 23 08:00:25 CEST 2007


Bugs item #1705393, was opened at 2007-04-22 13:38
Message generated for change (Comment added) made by rongarret
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1705393&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Ron Garret (rongarret)
Assigned to: Nobody/Anonymous (nobody)
Summary: Select() failure (race condition)

Initial Comment:
select.select fails on file-like objects created by socket.makefile when data exists in the file buffer but not in the underlying socket.

Here is code to reproduce the bug.  Run it, then point a web browser at port 8080 and observer that select times out indicating that no input is available even though input is still in fact available.

=======

from SocketServer import *
from socket import *
from select import select

class myHandler(StreamRequestHandler):
  def handle(self):
    while 1:
      sl = select([self.rfile],[],[],1)[0]
      print sl
      l = self.rfile.readline()
      if len(l)<3: break
      print l,
      pass
    print>>self.wfile, 'HTTP/1.0 200 OK'
    print>>self.wfile, 'Content-type: text/plain'
    print>>self.wfile
    print>>self.wfile, 'foo'

server = TCPServer(('',8080), myHandler)
server.serve_forever()


----------------------------------------------------------------------

>Comment By: Ron Garret (rongarret)
Date: 2007-04-22 23:00

Message:
Logged In: YES 
user_id=1775883
Originator: YES

>Just removing the select() call works fine.

For this simple example yes, not for more complicated cases.  My
particular application is a transparent HTTP proxy which needs to be able
to serve multiple connections at once and handle keep-alive connections. 
(The proxy connects to local server processes through unix sockets, which
is why I can't use a regular HTTP proxy.  Send me email if you want more
details about why I'm doing this.)  Without writing a full-blown HTTP-aware
proxy, select (or something like it) is necessary.

Upon further study, though, I have come to the conclusion that this is not
actually a bug (since select is doing what it is advertised to do), just
very counter-intituitive behavior.  If select is going to accept file
objects as arguments it ought to do the Right Thing with them IMO.

----------------------------------------------------------------------

Comment By: Gabriel Genellina (gagenellina)
Date: 2007-04-22 21:53

Message:
Logged In: YES 
user_id=479790
Originator: NO

The handler should not do its own select() call. (And even then, select
should use the underlying socket, not the wrapping pseudo-file object).
Just removing the select() call works fine.


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1705393&group_id=5470


More information about the Python-bugs-list mailing list