From e8c5e7c0cd672e7185b815e40be6fd5c1ef09b86 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 24 Mar 2026 13:21:13 +0900 Subject: [PATCH] mruby-test: write generated C files atomically to avoid race condition With `rake -m`, the C compiler can start reading a partially-written gem_test.c before generation completes. Write to a .tmp file first, then rename to the final path. Co-authored-by: Claude --- mrbgems/mruby-test/mrbgem.rake | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/mrbgems/mruby-test/mrbgem.rake b/mrbgems/mruby-test/mrbgem.rake index d8339fec0..9a6078cec 100644 --- a/mrbgems/mruby-test/mrbgem.rake +++ b/mrbgems/mruby-test/mrbgem.rake @@ -19,9 +19,11 @@ MRuby::Gem::Specification.new('mruby-test') do |spec| file assert_c => [assert_rb, build.mrbcfile] do |t| _pp "GEN", t.name.relative_path mkdir_p File.dirname(t.name) - open(t.name, 'w') do |f| + tmpfile = t.name + ".tmp" + open(tmpfile, 'w') do |f| mrbc.run f, assert_rb, 'mrbtest_assert_irep', cdump: false end + File.rename(tmpfile, t.name) end gem_table = build.gems.generate_gem_table build @@ -36,7 +38,8 @@ MRuby::Gem::Specification.new('mruby-test') do |spec| file g.test_rbireps => [g.test_rbfiles, build.mrbcfile].flatten do |t| _pp "GEN", t.name.relative_path mkdir_p File.dirname(t.name) - open(t.name, 'w') do |f| + tmpfile = t.name + ".tmp" + open(tmpfile, 'w') do |f| g.print_gem_test_header(f) test_preload = g.test_preload and [g.dir, MRUBY_ROOT].map {|dir| File.expand_path(g.test_preload, dir) @@ -117,6 +120,7 @@ MRuby::Gem::Specification.new('mruby-test') do |spec| end f.puts %Q[}] end + File.rename(tmpfile, t.name) end end @@ -134,7 +138,8 @@ MRuby::Gem::Specification.new('mruby-test') do |spec| file clib => ["#{build.build_dir}/mrbgems/active_gems.txt", build.mrbcfile, __FILE__] do |_t| _pp "GEN", clib.relative_path mkdir_p File.dirname(clib) - open(clib, 'w') do |f| + tmpfile = clib + ".tmp" + open(tmpfile, 'w') do |f| f.puts %Q[/*] f.puts %Q[ * This file contains a list of all] f.puts %Q[ * test functions.] @@ -169,5 +174,6 @@ MRuby::Gem::Specification.new('mruby-test') do |spec| end f.puts %Q[}] end + File.rename(tmpfile, clib) end end