From 44a2971e5c7d474b9a5c57fdd916281cd93609ec Mon Sep 17 00:00:00 2001 From: Daniel Pepper Date: Tue, 11 Jan 2022 16:47:01 -0800 Subject: [PATCH] atomic write race condition --- activesupport/CHANGELOG.md | 2 ++ activesupport/lib/active_support/core_ext/file/atomic.rb | 2 ++ activesupport/test/core_ext/file_test.rb | 4 ++++ 3 files changed, 8 insertions(+) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index a9c06b71445..c35a3597305 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,5 @@ +* Improve `File.atomic_write` error handling + * Fix `Class#descendants` and `DescendantsTracker#descendants` compatibility with Ruby 3.1. [The native `Class#descendants` was reverted prior to Ruby 3.1 release](https://bugs.ruby-lang.org/issues/14394#note-33), diff --git a/activesupport/lib/active_support/core_ext/file/atomic.rb b/activesupport/lib/active_support/core_ext/file/atomic.rb index 28b72ed42fc..b442ea3100f 100644 --- a/activesupport/lib/active_support/core_ext/file/atomic.rb +++ b/activesupport/lib/active_support/core_ext/file/atomic.rb @@ -64,6 +64,8 @@ class File file_name = join(dir, basename) FileUtils.touch(file_name) stat(file_name) + rescue Errno::ENOENT + file_name = nil ensure FileUtils.rm_f(file_name) if file_name end diff --git a/activesupport/test/core_ext/file_test.rb b/activesupport/test/core_ext/file_test.rb index b9d4c11d80c..314d38c5918 100644 --- a/activesupport/test/core_ext/file_test.rb +++ b/activesupport/test/core_ext/file_test.rb @@ -83,6 +83,10 @@ class AtomicWriteTest < ActiveSupport::TestCase File.unlink(file_name) rescue nil end + def test_probe_stat_in_when_no_dir + assert_nil File.probe_stat_in("/dir/does/not/exist") + end + private def file_name "atomic-#{Process.pid}.file"