2013/12/7 Eric Wong <normalperson / yhbt.net>:

> However, I want to do this via callback, example with Worker class:
>
>     class Worker
>       attr_writer :pid
>
>       def initialize
>         @r, @w = IO.pipe
>         Process.atfork_parent { @r.close unless @r.closed? }
>         Process.atfork_child { @w.close unless @w.closed? }
>       end
>     end
>
>     worker = Worker.new # IO.pipe
>     worker.pid = fork { ... }
>     ...
>
>     # No need to remember what to close in parent/child

I think it doesn't work with multiple thread.

2.times {
  Thread.new {
    worker = Worker.new # IO.pipe
    worker.pid = fork { ... }
    ...
  }
}

If fork for worker 1 is called between IO.pipe and fork for worker 2,
pipes for worker 2 is leaked for the process for worker 1 and
not inherited to the process for worker 2.

I feel it is not a good example for this issue.
-- 
Tanaka Akira