From 982af05178b2b44dd3cdf38e2aeef4808cf46207 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sat, 28 May 2016 23:53:20 -0400 Subject: [PATCH] Add a note about why we will always leak RDP tmpfiles --- plugins/hosts/darwin/cap/rdp.rb | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/plugins/hosts/darwin/cap/rdp.rb b/plugins/hosts/darwin/cap/rdp.rb index d3df31d05..3a02589d6 100644 --- a/plugins/hosts/darwin/cap/rdp.rb +++ b/plugins/hosts/darwin/cap/rdp.rb @@ -9,7 +9,18 @@ module VagrantPlugins class RDP def self.rdp_client(env, rdp_info) config_path = self.generate_config_file(rdp_info) - Vagrant::Util::Subprocess.execute("open", config_path.to_s) + begin + Vagrant::Util::Subprocess.execute("open", config_path.to_s) + ensure + # Note: this technically will never get run; neither would an + # at_exit call. The reason is that `exec` replaces this process, + # effectively the same as `kill -9`. This is solely here to prove + # that and so that future developers do not waste a ton of time + # try to identify why Vagrant is leaking RDP connection files. + # There is a catch-22 here in that we can't delete the file before + # we exec, and we can't delete the file after we exec :(. + File.unlink(config_path) if File.file?(config_path) + end end protected @@ -25,21 +36,24 @@ module VagrantPlugins } # Create the ".rdp" file - config_path = Pathname.new(Dir.tmpdir).join( - "vagrant-rdp-#{Time.now.to_i}-#{rand(10000)}.rdp") - config_path.open("w+") do |f| + t = ::Tempfile.new(["vagrant-rdp", ".rdp"]).tap do |f| + f.binmode + opts.each do |k, v| - f.puts("#{k}:#{v}") + f.write("#{k}:#{v}") end if rdp_info[:extra_args] rdp_info[:extra_args].each do |arg| - f.puts("#{arg}") + f.write("#{arg}") end end + + f.fsync + f.close end - return config_path + return t.path end end end