This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Infinite loop in imaplib.IMAP4_SSL when used with Gmail
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: out of date
Dependencies: Superseder: IMAP4_SSL spin because of SSLSocket.suppress_ragged_eofs
View: 5949
Assigned To: Nosy List: Ruben.Bakker, benjamin.peterson, janssen, kevinwatters, l0nwlf, ncoghlan, r.david.murray, rhettinger, rmore, rtucker, scott.dial
Priority: normal Keywords:

Created on 2010-06-16 15:11 by Ruben.Bakker, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
unnamed Ruben.Bakker, 2010-06-17 09:09
Messages (7)
msg107925 - (view) Author: Ruben Bakker (Ruben.Bakker) Date: 2010-06-16 15:11
When using imaplib.IMAP4_SSL to open a Gmail mailbox, the readline method can go into a infinite loop. It happens after Gmail drops the connection, usually after some time/inactivity. The next imaplib request will cause the infinite loop inside the readline() method.

Steps to reproduce:
Perform this script (use python -i to get the prompt):

import imaplib

HOST="imap.gmail.com"
PORT=993
USERNAME="username@gmail.com"
PASSWORD="password"

server = imaplib.IMAP4_SSL(host=HOST, port=PORT)
server.login(USERNAME, PASSWORD)

def f():
	print server.select("INBOX")
	print server.uid("FETCH", "1:*", "(UID FLAGS BODY.PEEK[HEADER.FIELDS (Subject)])")

Call the f() function and then wait about about an hour. Call f() again and server.select() will not return but take all CPU.

Add this line in IMAP4_SSL
          if not char: raise self.abort('socket error: EOF')

complete method:
    def readline(self):
        """Read line from remote."""
        line = []
        while 1:
            char = self.sslobj.read(1)
            if not char: raise self.abort('socket error: EOF')
            line.append(char)
            if char == "\n": return ''.join(line)
msg107926 - (view) Author: Ruben Bakker (Ruben.Bakker) Date: 2010-06-16 15:12
I forgot to mention that the section after "Add this line to IMAP4_SSL" describes the solution. Sorry about that.
msg107930 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-06-16 15:56
The readline return on '' was introduced by the patch for issue 5949, which discusses the fact that SSL may return '' indefinitely in some situations.  I've merged the nosy list from that issue to this one; anyone who isn't interested, my apologies and please delete yourself.  I'm especially hoping that Scott Dial will understand what is going on here and be able to suggest a fix.

I'm guessing this affects all maintained versions, but since that hasn't been tested I'm not updating the versions list.  Ruben, if you can test it with 2.7RC1 that would be especially helpful, since there are other fixes in 2.7 that may affect this, and if this is a regression we'll want to fix it before the next RC.
msg107931 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-06-16 15:58
Wait a minute, I misread your message.  You don't show the return on ''.  So I think perhaps the #5949 patch fixes this.  Ruben, can you please test with 2.7RC1?
msg107933 - (view) Author: Shashwat Anand (l0nwlf) Date: 2010-06-16 16:12
"import imaplib

HOST="imap.gmail.com"
PORT=993
USERNAME="username@gmail.com"
PASSWORD="password"

server = imaplib.IMAP4_SSL(host=HOST, port=PORT)
server.login(USERNAME, PASSWORD)

def f():
	print server.select("INBOX")
	print server.uid("FETCH", "1:*", "(UID FLAGS BODY.PEEK[HEADER.FIELDS (Subject)])")

Call the f() function and then wait about about an hour. Call f() again and server.select() will not return but take all CPU."

Tried to reproduce this on Python 2.7RC1. Seemed Ok to me. Took some 3 seconds to call f() not only once, but twice, thrice.
msg107997 - (view) Author: Ruben Bakker (Ruben.Bakker) Date: 2010-06-17 09:09
I installed Python 2.7RC1 on Mac OS X 10.6.3 and tried my script and it
worked for me. Imaplib throws the right exception instead of looping
forever:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "imapmail/explore/test2.py", line 30, in f
    server.select("INBOX")
  File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/imaplib.py",
line 642, in select
    typ, dat = self._simple_command(name, mailbox)
  File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/imaplib.py",
line 1060, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/imaplib.py",
line 890, in _command_complete
    raise self.abort('command: %s => %s' % (name, val))
imaplib.abort: command: SELECT => socket error: EOF
>>>

On Wed, Jun 16, 2010 at 6:12 PM, Shashwat Anand <report@bugs.python.org>wrote:

>
> Shashwat Anand <anand.shashwat@gmail.com> added the comment:
>
> "import imaplib
>
> HOST="imap.gmail.com"
> PORT=993
> USERNAME="username@gmail.com"
> PASSWORD="password"
>
> server = imaplib.IMAP4_SSL(host=HOST, port=PORT)
> server.login(USERNAME, PASSWORD)
>
> def f():
>        print server.select("INBOX")
>        print server.uid("FETCH", "1:*", "(UID FLAGS BODY.PEEK[HEADER.FIELDS
> (Subject)])")
>
> Call the f() function and then wait about about an hour. Call f() again and
> server.select() will not return but take all CPU."
>
> Tried to reproduce this on Python 2.7RC1. Seemed Ok to me. Took some 3
> seconds to call f() not only once, but twice, thrice.
>
> ----------
> nosy: +l0nwlf
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue9010>
> _______________________________________
>
msg108010 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-06-17 13:03
OK, this is out of date, then.  Nosy people, sorry for the noise.
History
Date User Action Args
2022-04-11 14:57:02adminsetgithub: 53256
2010-06-17 13:03:57r.david.murraysetstatus: open -> closed
resolution: out of date
messages: + msg108010

superseder: IMAP4_SSL spin because of SSLSocket.suppress_ragged_eofs
stage: test needed -> resolved
2010-06-17 09:09:11Ruben.Bakkersetfiles: + unnamed

messages: + msg107997
2010-06-16 16:12:44l0nwlfsetnosy: + l0nwlf
messages: + msg107933
2010-06-16 15:58:51r.david.murraysetmessages: + msg107931
2010-06-16 15:56:57r.david.murraysetnosy: + rhettinger, ncoghlan, janssen, scott.dial, kevinwatters, benjamin.peterson, r.david.murray, rmore, rtucker

messages: + msg107930
stage: test needed
2010-06-16 15:12:53Ruben.Bakkersetmessages: + msg107926
2010-06-16 15:11:23Ruben.Bakkercreate