From a723699519648e2eb513fc0401f0e545628ed901 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Wed, 10 Apr 2019 13:37:04 +0300 Subject: [PATCH] FIX: Ensure TarReader is closed. --- lib/discourse_ip_info.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/discourse_ip_info.rb b/lib/discourse_ip_info.rb index 70819ad23cd..b67f5cdafe0 100644 --- a/lib/discourse_ip_info.rb +++ b/lib/discourse_ip_info.rb @@ -26,23 +26,27 @@ class DiscourseIpInfo uri = URI("https://geolite.maxmind.com/download/geoip/database/#{name}.tar.gz") - tar_gz_file = Tempfile.new begin + tar_gz_file = Tempfile.new tar_gz_file.binmode tar_gz_file.write(Net::HTTP.get(uri)) tar_gz_file.close - extractor = Gem::Package::TarReader.new(Zlib::GzipReader.open(tar_gz_file.path)) - extractor.rewind + begin + extractor = Gem::Package::TarReader.new(Zlib::GzipReader.open(tar_gz_file.path)) + extractor.rewind - extractor.each do |entry| - next unless entry.full_name.ends_with?(".mmdb") - File.open(mmdb_path(name), "wb") { |f| f.write(entry.read) } + extractor.each do |entry| + next unless entry.full_name.ends_with?(".mmdb") + File.open(mmdb_path(name), "wb") { |f| f.write(entry.read) } + end + ensure + extractor.close end + ensure tar_gz_file.close tar_gz_file.unlink - extractor.close end end