atomic write race condition

This commit is contained in:
Daniel Pepper 2022-01-11 16:47:01 -08:00
parent 83a4fa414b
commit 44a2971e5c
No known key found for this signature in database
GPG Key ID: B7228DC1EC451D66
3 changed files with 8 additions and 0 deletions

View File

@ -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),

View File

@ -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

View File

@ -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"