Skip to content

Commit

Permalink
atomic write race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
dpep committed Jan 17, 2022
1 parent 83a4fa4 commit 44a2971
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 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),
Expand Down
2 changes: 2 additions & 0 deletions activesupport/lib/active_support/core_ext/file/atomic.rb
Expand Up @@ -64,6 +64,8 @@ def self.probe_stat_in(dir) # :nodoc:
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
Expand Down
4 changes: 4 additions & 0 deletions activesupport/test/core_ext/file_test.rb
Expand Up @@ -83,6 +83,10 @@ def test_atomic_write_returns_result_from_yielded_block
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"
Expand Down

0 comments on commit 44a2971

Please sign in to comment.