From: Ben Weintraub Date: 2013-01-18T10:39:16+09:00 Subject: Re: Background threads and Process.daemon --20cf30363f85790ae104d3862c0e Content-Type: text/plain; charset=ISO-8859-1 Ah, I believe I've found the issue. Process.daemon calls daemon(3), which calls fork(). The POSIX documentation for fork says: A process shall be created with a single thread. If a multi-threaded process calls *fork*(), the new process shall contain a replica of the calling thread and its entire address space, possibly including the states of mutexes and other resources. From: http://pubs.opengroup.org/onlinepubs/009695399/functions/fork.html So this seems a misunderstanding of daemon() semantics on my part rather than a bug. Sorry for the noise! Ben On Thursday, January 17, 2013, Ben Weintraub wrote: > Hello all, > > I'm seeing some unexpected behavior with background threads and > Process.daemon, and wondering whether I'm just doing something > wrong/unsupported, or if I've hit upon a Ruby bug. > > Here's a test case that exposes the oddness I'm seeing: > https://gist.github.com/4561056 > > This code spawns a background thread that loops calling sleep(1) > repeatedly, then calls Process.daemon, and finally puts the main thread > into a sleep loop. > > To reproduce the strangeness, run it, find the PID of the daemonized > process, and send it SIGQUIT. Instead of exiting, the process gets stuck in > a loop in rb_thread_terminate_all that looks like this: > https://gist.github.com/4561403 > > In addition, after daemonization, attaching to the process with gdb shows > only 2 native threads. Prior to daemonization, three native threads are > visible from gdb. > > Is this (creating a background thread and then daemonizing) expected to > work? Am I doing something else wrong here? I'm using ruby 1.9.3p327 on > Mac OS X 10.8.2. > > Thanks! > Ben > --20cf30363f85790ae104d3862c0e Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Ah, I believe I've found the issue. Process.daemon calls daemon(3), whi= ch calls fork(). The POSIX documentation for fork says:

= A process shall be created with a single thread. If a multi-threaded pro= cess calls=A0fork(), the new process shall contain a replica of the = calling thread and its entire address space, possibly including the states = of mutexes and other resources.

= F= rom:=A0http://pubs.opengroup.org/onlinepubs/009695399/functions/fork.ht= ml

So this seems a misunderstanding of daemon() semantics on= =A0my part rather than a bug.=A0

Sorr= y for the noise!
Ben

On Thursday, January 17, 2013, Ben We= intraub wrote:
Hello all,

I'm seeing some unexpected behavior with background threads and Proc= ess.daemon, and wondering whether I'm just doing something wrong/unsupp= orted, or if I've hit upon a Ruby bug.

Here's a test case that exposes the oddness I'm= seeing:
https://gist.github.com/4561056

This= code spawns a background thread that loops calling sleep(1) repeatedly, th= en calls Process.daemon, and finally puts the main thread into a sleep loop= .

To reproduce the strangeness, run it, find the PID of t= he daemonized process, and send it SIGQUIT. Instead of exiting, the process= gets stuck in a loop in rb_thread_terminate_all=A0that looks like this:
https://= gist.github.com/4561403

In addition, after= daemonization, attaching to the process with gdb shows only 2 native threa= ds. Prior to daemonization, three native threads are visible from gdb.

Is this (creating a background thread and then daemoniz= ing) expected to work? Am I doing something else wrong here? =A0I'm usi= ng ruby=A01.9.3p327 on Mac OS X 10.8.2.

Thanks!
Ben
--20cf30363f85790ae104d3862c0e--