From: Gary Wright Date: 2009-07-16T02:57:08+09:00 Subject: Re: Process.fork weirdness On Jul 14, 2009, at 4:29 PM, Joel VanderWerf wrote: > That leaves batch filtering of files, which is most often performed > on stdin/stdout, and these descriptors are flushed in ruby's #fork > wrapper. Interesting. I didn't realize that Kernel#fork flushes stdout and stderr. That should probably be in the rdoc text for fork. > One thing left to watch out for is race conditions in (for example): > > File.open(..., "w") do |f| > YAML.dump(..., f) > ### before the file is closed, suppose another thread forks and exits > end I can think of three ways to avoid these sorts of race conditions: -- do all forking before opening files -- do all forking before creating any threads -- use a synchronization mechanism to control the timing of forks in a multi-threaded process In all cases I think it requires library code to be aware of these constraints. I'm not sure there is a way to protect buffered IO objects against a library that can fork at indeterminate times. Gary Wright