From: Jason Gladish Date: 2013-03-30T02:16:18+09:00 Subject: [ruby-core:53825] Thread/fork issue --089e0116136ac80ac104d9136c59 Content-Type: text/plain; charset=ISO-8859-1 Hello all, I've found an issue where calling fork inside a thread, and passing a block to the fork, causes the forked process to continue after the block. I've reproduced the issue on the following versions of ruby: ruby 2.0.0p100 (2013-03-27 revision 39954) [x86_64-darwin10.8.0] ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin10.8.0] Here is the script I used to reproduce: 1000.times do |j| puts "run #{j}" threads = [] 100.times do |i| threads << Thread.new(i) do |local_i| opid = fork do # exit!(true) # fixes the issue # exit(true) # doesn't fix the issue # no 'exit' also exhibits issue end ::Process.waitpid(opid, 0) File.open("/tmp/test_thread_fork_#{local_i}.pid", "w") {|f| f.write "1" } end end threads.map { |t| t.join } borked = false 100.times do |i| fn = "/tmp/test_thread_fork_#{i}.pid" contents = File.read(fn) if contents.size > 1 puts "file #{fn} was written to many times (#{contents})" borked = true end end exit(false) if borked end As you can see from the comments inside the fork I can work around the issue by using "exit!". I am correct in understanding that there should be no case in which the file is written to multiple times, correct? Thank you, Jason Gladish --089e0116136ac80ac104d9136c59 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Hello all,

I've found an issue wher= e calling fork inside a thread, and passing a block to the fork, causes the= forked process to continue after the block. =A0I've reproduced the iss= ue on the following versions of ruby:
ruby 2.0.0p100 (2013-03-27 revision 39954) [x86_64-darwin10.8.0]<= /div>
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-dar= win10.8.0]

Here is the script I used to repr= oduce:

1000.times do |j|
=A0 puts "run #{j= }"
=A0 threads =3D []
=A0 100.times do |i|
=A0 =A0 threads << Thread.new(i) do |local_i|
=A0 =A0 = =A0 opid =3D fork do
=A0 =A0 =A0 =A0 # exit!(true) # fixes the issue
=A0 =A0 =A0 = =A0 # exit(true) # doesn't fix the issue
=A0 =A0 =A0 =A0 # no= 'exit' also exhibits issue
=A0 =A0 =A0 end
=A0= =A0 =A0 ::Process.waitpid(opid, 0)
=A0 =A0 =A0 File.open("/tmp/test_thread_fork_#{local_i}.pid"= , "w") {|f| f.write "1" }
=A0 =A0 end
=A0 end
=A0 threads.map { |t| t.join }
=A0 =A0
=
=A0 borked =3D false
=A0 100.times do |i|
=A0 =A0 fn =3D "/tmp/test_thread_f= ork_#{i}.pid"
=A0 =A0 contents =3D File.read(fn)
= =A0 =A0 if contents.size > 1
=A0 =A0 =A0 puts "file #{fn}= was written to many times (#{contents})"
=A0 =A0 =A0 borked =3D true
=A0 =A0 end
=A0 end
=A0 exit(false) if borked
end

<= div>As you can see from the comments inside the fork I can work around the = issue by using "exit!". =A0I am correct in understanding that the= re should be no case in which the file is written to multiple times, correc= t?

Thank you,
Jason Gladish

= --089e0116136ac80ac104d9136c59--