From b7880146d13c8ed837ffa92102f1a907dd0d21ee Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 24 Mar 2009 20:08:08 -0700 Subject: [PATCH] Close socket we create before raising exceptions This can potentially fix some file descriptor leaks (but there may not be any leaks because of GC which can mysteriously close file descriptors as I've discovered). Better safe than sorry... --- lib/mogilefs/util.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/mogilefs/util.rb b/lib/mogilefs/util.rb index 9ccd4f0..28c7d44 100644 --- a/lib/mogilefs/util.rb +++ b/lib/mogilefs/util.rb @@ -180,9 +180,14 @@ class Socket sock = mogilefs_new(host, port, timeout) syswrite_full(sock, request, timeout) timeout -= (Time.now - t0) - raise MogileFS::Timeout, 'socket read timeout' if timeout < 0 + if timeout < 0 + sock.close rescue nil + raise MogileFS::Timeout, 'socket read timeout' + end r = IO.select([sock], nil, nil, timeout) return sock if r && r[0] + + sock.close rescue nil raise MogileFS::Timeout, 'socket read timeout' end -- 2.11.4.GIT