Ian Leitch wrote:
> Hi list,
> 
> I am trying to make my DRb server fork into a daemon, yet the DRbServer
> thread joins immediately when called inside a fork block. I'm not entirely
> sure why this is happening but I'm coming to the conclusion that I'm going
> to have to resort to a sleeping while loop. I also tried blocking on a 
> Mutex
> to keep the process running, but that resulted in an error about blocking
> the only thread (which isn't true because I have a pool of them, plus the
> DRbServer thread which should still be running).
> I did find a commit by matz some time ago about removing a "fork kills
> threads" warning or some such, is this related?

Forking with threads is a little tricky. It causes all threads except 
the thread calling #fork to be killed.

   t1 = Thread.new {sleep}

   Thread.new do
     fork do
       p t1
     end
   end

   Process.wait

Output:

#<Thread:0xb7d986b8 dead>

This is probably sensible behavior, but it causes problems with mutexes.

If thread A does a Process.fork while thread B is holding a Mutex, then 
in the child, all other threads, including thread B, will be dead. But 
thread B is _still_ holding the mutex. So if thread A tries to get the 
mutex, the child process will deadlock.

There's a safer implementation of Mutex and fork in the fsdb lib, with a 
mini-test at the end (see 
http://wiki.rubygarden.org/Ruby/page/show/ForkableMutex). It cleans up 
dead threads that are holding mutexes, and it also fixes a race 
condition in the waiters queue.

But it won't help you keep the DRb threads alive after a fork (in the 
child). Probably you need to start the service separately in the fork.

-- 
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407