classification
Title: socket.readline() interface doesn't handle EINTR properly
Type: behavior Stage: test needed
Components: Extension Modules, Library (Lib) Versions: Python 3.0, Python 2.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: jorend, loewis, orenti, sobomax (4)
Priority: normal Keywords: patch

Created on 2007-01-04 21:37 by sobomax, last changed 2009-03-30 18:48 by ajaksu2.

Files
File name Uploaded Description Edit Remove
diff sobomax, 2007-01-04 21:37 Proposed fix.
Messages (5)
msg51655 - (view) Author: Maxim Sobolev (sobomax) Date: 2007-01-04 21:37
The socket.readline() interface doesn't handle EINTR properly. Currently, when EINTR received exception is not handled and all data that has been in the buffer is lost. There is no way to recover that data from the code that uses the interface.

Correct behaviour would be to catch EINTR and restart recv(). Patch is attached.

Following is the real world example of how it affects httplib module:

  File "/usr/local/lib/python2.4/xmlrpclib.py", line 1096, in __call__
    return self.__send(self.__name, args)
  File "/usr/local/lib/python2.4/xmlrpclib.py", line 1383, in __request
    verbose=self.__verbose
  File "/usr/local/lib/python2.4/xmlrpclib.py", line 1131, in request
    errcode, errmsg, headers = h.getreply()
  File "/usr/local/lib/python2.4/httplib.py", line 1137, in getreply
    response = self._conn.getresponse()
  File "/usr/local/lib/python2.4/httplib.py", line 866, in getresponse
    response.begin()
  File "/usr/local/lib/python2.4/httplib.py", line 336, in begin
    version, status, reason = self._read_status()
  File "/usr/local/lib/python2.4/httplib.py", line 294, in _read_status
    line = self.fp.readline()
  File "/usr/local/lib/python2.4/socket.py", line 325, in readline
    data = recv(1)
error: (4, 'Interrupted system call')

-Maxim
msg51656 - (view) Author: Oren Tirosh (orenti) Date: 2007-01-07 18:24
You may have encountered this on sockets but *all* Python I/O does not handle restart on EINTR. 

The right place to fix this is probably in C, not the Python library. The places where an I/O operation could be interrupted are practically anywhere the GIL is released. This kind of change is likely to be controversial.
msg51657 - (view) Author: Maxim Sobolev (sobomax) Date: 2007-01-08 10:51
Well, it's not quite correct since for example httplib.py tries to handle EINTR. The fundamental problem with socket.readline() is that it does internal buffering so that getting EINTR results in data being lost.

I don't think it has to be fixed in C, since recv() is very low-level interface and it is expected to return EINTR on signal, so that "fixing" it there could possibly break software that relies on this behaviour. And I don't quite buy your reasoning - "since it's broken in few more places let's keep it consistently broken everywhere". To me it sounds like attempt to hide the head in the sand instead of facing the problem at hand. Fixing socket.readline() may be the first step in improvind the library to handle this condition properly.
msg51658 - (view) Author: Martin v. Löwis (loewis) Date: 2007-02-16 13:05
I agree that this should be fixed; I'm not sure I like the proposed fixed, though. It discards the exception and keeps running.

What it (IMO) should do instead is abort, then return the data on the next invocation. Of course, this may have problems in itself, since the file descriptor might not report read-ready when passed to select or poll, even though data are available.

Please discuss this on python-dev (and elsewhere), and report what recommendations people made.
msg51659 - (view) Author: Jason Orendorff (jorend) Date: 2007-03-07 17:54
loewis: I think your idea is the right answer.  I'm not worried about select/poll.  Surely no one uses select/poll and socket._fileobject.readline() on the same socket.  select/poll are for nonblocking sockets; this readline() method doesn't even catch EWOULDBLOCK.

...In fact even if you did use select/poll on the (blocking) socket after readline() threw EINTR--which no one should do--I think it would still work just as expected unless you were doing something truly weird.
History
Date User Action Args
2009-03-30 18:48:20ajaksu2setstage: test needed
type: behavior
components: + Library (Lib)
versions: + Python 2.6, Python 3.0
2007-01-04 21:37:50sobomaxcreate