From 14348480f0f3fb25a27301302cc74d8c1890ff3d Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Sat, 29 Aug 2026 22:58:50 +0200 Subject: [PATCH] Close sockets when DRb transport setup fails --- lib/drb/ssl.rb | 28 ++++++++++++++++++++-------- lib/drb/unix.rb | 36 ++++++++++++++++++++++++------------ 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/lib/drb/ssl.rb b/lib/drb/ssl.rb index 9ec55d3..c9f098a 100644 --- a/lib/drb/ssl.rb +++ b/lib/drb/ssl.rb @@ -259,10 +259,17 @@ def self.parse_uri(uri) # :nodoc: def self.open(uri, config) host, port, = parse_uri(uri) soc = TCPSocket.open(host, port) - ssl_conf = SSLConfig::new(config) - ssl_conf.setup_ssl_context - ssl = ssl_conf.connect(soc) - self.new(uri, ssl, ssl_conf, true) + stream = soc + begin + ssl_conf = SSLConfig::new(config) + ssl_conf.setup_ssl_context + ssl = ssl_conf.connect(soc) + stream = ssl + self.new(uri, ssl, ssl_conf, true) + rescue Exception + stream.close + raise + end end # Returns a DRb::DRbSSLSocket instance as a server-side connection, with @@ -286,10 +293,15 @@ def self.open_server(uri, config) port = soc.addr[1] if port == 0 @uri = "drbssl://#{host}:#{port}" - ssl_conf = SSLConfig.new(config) - ssl_conf.setup_certificate - ssl_conf.setup_ssl_context - self.new(@uri, soc, ssl_conf, false) + begin + ssl_conf = SSLConfig.new(config) + ssl_conf.setup_certificate + ssl_conf.setup_ssl_context + self.new(@uri, soc, ssl_conf, false) + rescue Exception + soc.close + raise + end end # This is a convenience method to parse +uri+ and separate out any diff --git a/lib/drb/unix.rb b/lib/drb/unix.rb index 1e371cc..b73e2ce 100644 --- a/lib/drb/unix.rb +++ b/lib/drb/unix.rb @@ -40,18 +40,27 @@ def self.open_server(uri, config) else soc = UNIXServer.open(filename) end - owner = config[:UNIXFileOwner] - group = config[:UNIXFileGroup] - if owner || group - require 'etc' - owner = Etc.getpwnam( owner ).uid if owner - group = Etc.getgrnam( group ).gid if group - File.chown owner, group, filename - end - mode = config[:UNIXFileMode] - File.chmod(mode, filename) if mode + begin + owner = config[:UNIXFileOwner] + group = config[:UNIXFileGroup] + if owner || group + require 'etc' + owner = Etc.getpwnam( owner ).uid if owner + group = Etc.getgrnam( group ).gid if group + File.chown owner, group, filename + end + mode = config[:UNIXFileMode] + File.chmod(mode, filename) if mode - self.new(uri, soc, config, true) + self.new(uri, soc, config, true) + rescue Exception + soc.close + begin + File.unlink(filename) + rescue Errno::ENOENT + end + raise + end end def self.uri_option(uri, config) @@ -97,8 +106,11 @@ def close shutdown # DRbProtocol#shutdown path = @socket.path if @server_mode @socket.close - File.unlink(path) if @server_mode @socket = nil + begin + File.unlink(path) if @server_mode + rescue Errno::ENOENT + end close_shutdown_pipe end