Bug #10031

Net::IMAP idle can still block a thread forever.

Added by James Pike about 1 year ago. Updated about 1 year ago.

Assignee:Shugo Maeda
ruby -v:ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux] Backport:2.0.0: UNKNOWN, 2.1: UNKNOWN


When calling Net::IMAP idle often errors will go unnoticed forever, simply leaving the Thread that called "idle" blocking forever.

For example try bringing your internet connection down whilst a thread is calling the idle method, the calling thread will now be blocked forever.

I've encountered this issue also without losing my internet connection, sometimes the remote server will disconnect the client and the client will never notice leading to the same issue.


#1 Updated by Eric Wong about 1 year ago

Setting SO_KEEPALIVE on the socket will help, but that still takes at
least 2 hours to detect on a stock Linux system. Do you want to set a
shorter keepalive, or is ~2 hours OK?

You can change the interval using OS-specific knobs (e.g. sysctl).

Totally untested one line patch here:

--- a/lib/net/imap.rb
+++ b/lib/net/imap.rb
@@ -1053,6 +1053,7 @@ module Net
       @tagno = 0
       @parser = ResponseParser.new
       @sock = TCPSocket.open(@host, @port)
+      @sock.setsockopt(:SOL_SOCKET, :SO_KEEPALIVE, 1)
     if options[:ssl]

#2 Updated by Shugo Maeda about 1 year ago

  • Status changed from Open to Assigned
  • Assignee set to Shugo Maeda

Also available in: Atom PDF