imaplib2 patch/question

Ethan Glasser-Camp glasse at
Sat Dec 11 08:33:29 GMT 2010


Please forgive me for emailing you directly, but I couldn't figure out 
how else to send feedback for this library (imaplib2). I am cc:ing the 
offlineimap mailing list, since it concerns my efforts to integrate the 
two projects.

I'm trying to get offlineimap to support the IMAP IDLE command. An 
attempt was made once to get this to work using your imaplib2, but this 
caused stability issues for some users. It looks like fixes to imaplib2 
were made that may address these issues, so I decided to take a little 
while to try to integrate offlineimap and the newest imaplib2. 
Everything seems to be working OK, except for when connections break -- 
and my ISP is such that connections break with some frequency.

offlineimap has a setting "socktimeout" which is given to 
socket.setdefaulttimeout(). In the old, pre-imaplib2 world, this let the 
user specify the maximum amount of time he was comfortable waiting for a 
possibly broken connection. However, since imaplib2 uses non-blocking 
input in a separate thread, socket.setdefaulttimeout() is useless for 
us, meaning that (on my machine) broken connections stop the program 
from doing anything for 15-20 minutes.

I have been poking around in the code for imaplib2, trying to figure out 
where would be a good place to put code to "simulate" the effect of this 
timeout. First I thought it would be good to put a timeout in the 
Request.get_response() method, since this seemed like it could let the 
connection languish for as long as it needed to, up until a response was 
needed, and then it could break. However, adding a timeout there did not 
seem to fix the problem; connections would still take 15-20 minutes to 
die sometimes. I have no idea why, but my working hypothesis is that the 
get_response() call never fired because the writer thread never finished 
actually sending the commands.

Currently I am experimenting with accumulating timeout in the reader 
thread (something like the attached patch). This "seems to work", but I 
think I've seen one case where it hung for 20+ minutes trying to stop 
idling (so possibly the same problem as above).

I was wondering if you could comment on this or any other approach you 
think would be good to implement this functionality, and your 
willingness to update imaplib2 to include functionality like this 
(assuming a suitable patch were provided). I'm especially interested in: 
is self.tagged_commands the best way to check if we are expecting input? 
What do you think the timeout variable should be called, given that 
there are already idle_timeout things throughout the class?

Thanks for your time!


-------------- next part --------------
A non-text attachment was scrubbed...
Name: imaplib2.patch
Type: text/x-patch
Size: 3169 bytes
Desc: not available
URL: <>

More information about the OfflineIMAP-project mailing list