From 5c106011c1d684cf626a2d6b1886fd4a9dfee3d7 Mon Sep 17 00:00:00 2001
From: Eric Wong <e@80x24.org>
Date: Fri, 20 Apr 2018 11:44:14 +0000
Subject: [PATCH] net/imap: set SO_KEEPALIVE on TCP sockets

Otherwise connections (commonly on IDLE, but it could be any
command) may never receive notifications of link errors.
---
 lib/net/imap.rb | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/net/imap.rb b/lib/net/imap.rb
index 3d33b94fdd..b42186dce0 100644
--- a/lib/net/imap.rb
+++ b/lib/net/imap.rb
@@ -1129,7 +1129,9 @@ def initialize(host, port_or_options = {},
     end
 
     def tcp_socket(host, port)
-      Socket.tcp(host, port, :connect_timeout => @open_timeout)
+      s = Socket.tcp(host, port, :connect_timeout => @open_timeout)
+      s.setsockopt(:SOL_SOCKET, :SO_KEEPALIVE, true)
+      s
     rescue Errno::ETIMEDOUT
       raise Net::OpenTimeout, "Timeout to open TCP connection to " +
         "#{host}:#{port} (exceeds #{@open_timeout} seconds)"
-- 
EW

