# DRb.thread.join in a Process.fork block?

**URL:** <https://www.ruby-forum.com/t/drb-thread-join-in-a-process-fork-block/72034>\
**Category:** Ruby\
**Created:** [October 13, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/drb-thread-join-in-a-process-fork-block/72034 "2006-10-13T19:08:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elliterate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de053/32.png) [@elliterate](https://www.ruby-forum.com/u/elliterate)\
**Post date:** [October 13, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/drb-thread-join-in-a-process-fork-block/72034/1 "2006-10-13T19:08:30Z")

</div>

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?

Cheers,  
Ian.

---

<div class="post-metadata">

**Author:** ![elliterate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de053/32.png) [@elliterate](https://www.ruby-forum.com/u/elliterate)\
**Post date:** [October 13, 2006, 7:43pm UTC](https://www.ruby-forum.com/t/drb-thread-join-in-a-process-fork-block/72034/2 "2006-10-13T19:43:21Z")

</div>

Ian L. wrote:

> 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](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.

---

<div class="post-metadata">

**Author:** ![elliterate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de053/32.png) [@elliterate](https://www.ruby-forum.com/u/elliterate)\
**Post date:** [October 13, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/drb-thread-join-in-a-process-fork-block/72034/3 "2006-10-13T19:53:23Z")

</div>

On Sat, 14 Oct 2006, Ian L. wrote:

> threads" warning or some such, is this related?
> 
> Cheers,  
> Ian.

you have to fork first.

fork{  
fork{  
start\_drb  
}  
exit!  
}

fyi.

check out the code for slave - it does this and a bit more - though it’s  
not  
geared towards daemons.

-a
