From bec0440a9c4c040d2f564256abddd7e6a6604123 Mon Sep 17 00:00:00 2001 From: Yury Lebedev Date: Sun, 4 Feb 2024 17:54:50 +0100 Subject: [PATCH] Fix conflict with resolv-replace gem (#989) * Fix conflict with resolv-replace gem Closes #987 In version 3.2.7 socket_timeout option was introduced for TCPSocket. This works unless `resolv-replace` gem is loaded (which was added to ruby standard library since version 3.0.0). This commit adds another check besides the ruby version check to avoid breaking dalli for applications that have `resolv-replace` gem required. * Fix rubocop warning * Comment additional check in TCP.open method definition --- lib/dalli/socket.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/dalli/socket.rb b/lib/dalli/socket.rb index 48f3c3f2..efb94475 100644 --- a/lib/dalli/socket.rb +++ b/lib/dalli/socket.rb @@ -88,7 +88,14 @@ class TCP < TCPSocket # options - supports enhanced logging in the case of a timeout attr_accessor :options - if RUBY_VERSION >= '3.0' + # Check that TCPSocket#initialize was not overwritten by resolv-replace gem + # (part of ruby standard library since 3.0.0, should be removed in 3.4.0), + # as it does not handle keyword arguments correctly. + # To check this we are using the fact that resolv-replace + # aliases TCPSocket#initialize method to #original_resolv_initialize. + # https://github.com/ruby/resolv-replace/blob/v0.1.1/lib/resolv-replace.rb#L8 + if RUBY_VERSION >= '3.0' && + !::TCPSocket.private_instance_methods.include?(:original_resolv_initialize) def self.open(host, port, options = {}) sock = new(host, port, connect_timeout: options[:socket_timeout]) sock.options = { host: host, port: port }.merge(options)