Closed apoikos closed 2 years ago
This looks good to me. I'd prefer:
diff --git a/lib/hiredis/ext/connection.rb b/lib/hiredis/ext/connection.rb
index c62a5d2..0e1fbf7 100644
--- a/lib/hiredis/ext/connection.rb
+++ b/lib/hiredis/ext/connection.rb
@@ -22,7 +22,11 @@ module Hiredis
end
def sock
- @sock ||= Socket.for_fd(fileno)
+ return @sock if @sock
+
+ @sock = Socket.for_fd(fileno)
+ @sock.autoclose = false
+ @sock
end
end
end
Closing, as this was merged via #82
Hiredis::Ext::Connection#sock
creates aSocket
object usingSocket.for_fd
, wrapping the socket managed in C by hiredis and returns it to the caller. TheSocket
does not haveautoclose
set tofalse
, so this has the unwanted side-effect that once the object becomes stale and is GC'd, the underlying file descriptor will be closed. This can causeErrno::EBADF
errors, as outlined in Ruby Bug #1174, especially when the Redis driver performs a reconnect and creates a new instance ofHiredis::Ext::Connection
, forgetting the old one completely.Note that
Errno::EBADF
errors might affect other parts of the program, as the FD is closed at GC time and it may refer to something else. The following script reproduces the issue:Fix this by setting
autoclose
tofalse
when creating theSocket
object.