Project

General

Profile

Actions

Backport #7720

closed

Process.daemon does not clean up background Ruby threads, leading to hang at exit

Added by benweint (Ben Weintraub) almost 12 years ago. Updated about 7 years ago.

Status:
Rejected
[ruby-core:51549]

Description

Process.fork appears to make special considerations for Ruby background threads so that they are cleaned up in the child process after fork (since fork() only duplicates the calling thread). Process.daemon also relies on fork internally, but does not make the same considerations, leading to a hang at exit in a daemonized process that had multiple Ruby threads prior to calling Process.daemon.

The attached daemon.rb demonstrates the issue. It will call either Process.daemon (default) or Process.fork (if you pass 'fork' as the first arg).

If you pass nothing, causing it to call Process.daemon, the daemonized Ruby process will still think it has two threads, and will hang indefinitely on exit, in a loop in rb_thread_terminate_all. If you pass 'fork' as the first argument, the forked child does not hang, and appears to have only one Ruby thread (the one that called Process.fork).

It looks like rb_thread_atfork() is the relevant call in the fork case that makes this work, but it doesn't appear to be called in the daemon case.


Files

daemon.rb (306 Bytes) daemon.rb benweint (Ben Weintraub), 01/22/2013 02:33 AM

Related issues 1 (0 open1 closed)

Has duplicate Ruby master - Bug #7798: 子スレッドを実行しているとき Process.daemon すると,その後メインスレッドが終了してもプロセスが終了しないClosed02/07/2013Actions

Updated by samg (Sam Goldstein) almost 12 years ago

This seems to be the underlying cause of the bad interaction between resque and new relic described at https://github.com/defunkt/resque/issues/578

Updated by ko1 (Koichi Sasada) almost 12 years ago

Does this issue remain on trunk?
(Should we modify trunk before 2.0?)

Updated by kosaki (Motohiro KOSAKI) almost 12 years ago

trunk doesn't have an issue. fork and daemon make the same result.

Updated by usa (Usaku NAKAMURA) almost 12 years ago

  • Status changed from Open to Assigned
  • Assignee set to usa (Usaku NAKAMURA)

So, what's the difference?

Actions #5

Updated by usa (Usaku NAKAMURA) almost 12 years ago

  • Tracker changed from Bug to Backport
  • Project changed from Ruby master to Backport193

Updated by usa (Usaku NAKAMURA) about 7 years ago

  • Status changed from Assigned to Rejected

1.9.3 is out of date

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0