From: "tmm1 (Aman Gupta)" Date: 2013-12-06T15:37:40+09:00 Subject: [ruby-core:58898] [ruby-trunk - Feature #5446] at_fork callback API Issue #5446 has been updated by tmm1 (Aman Gupta). Thanks for your feedback. > 1. Why no before_fork? I planned to add this in the same way as after_fork, as long as there are no issues with my patch. I wasn't sure if rb_vm_t is the best place for the new hooks. If it seems sane, I can expand the patch. > 2. zk has :after_child nad :after_parent hook and your patch don't. Why? With two hooks, before_fork/after_fork methods are enough. If we want to implement three hooks, should we add three new methods? > 3. Why should the new method return proc? I guessed the return value can be used to de-register the hook later. But there is no way to do this currently. > 4. When rb_daemon() is used, some platform call after_fork once and > the other call twice. It seems useless. Ah, good point. Maybe we need a flag to ensure only one invocation. > 5. Why do hook fire at rb_thread_atfork() make a lot of new array? No reason. Patch was a simple proof of concept for feedback. > 6. Your patch doesn't aim to remove the hooks and I'm sure it is required. Do you have any API ideas for this? I agree we need some way to remove the hooks. One option is to use tracepoint api: tp = TracePoint.new(:before_fork, :after_fork_child){ ... } tp.enable tp.disable ---------------------------------------- Feature #5446: at_fork callback API https://bugs.ruby-lang.org/issues/5446#change-43450 Author: normalperson (Eric Wong) Status: Assigned Priority: Low Assignee: kosaki (Motohiro KOSAKI) Category: Target version: next minor It would be good if Ruby provides an API for registering fork() handlers. This allows libraries to automatically and agnostically reinitialize resources such as open IO objects in child processes whenever fork() is called by a user application. Use of this API by library authors will reduce false/improper sharing of objects across processes when interacting with other libraries/applications that may fork. This Ruby API should function similarly to pthread_atfork() which allows (at least) three different callbacks to be registered: 1) prepare - called before fork() in the original process 2) parent - called after fork() in the original process 3) child - called after fork() in the child process It should be possible to register multiple callbacks for each action (like at_exit and pthread_atfork(3)). These callbacks should be called whenever fork() is used: - Kernel#fork - IO.popen - `` - Kernel#system ... And any other APIs I've forgotten about I also want to consider handlers that only need to be called for plain fork() use (without immediate exec() afterwards, like with `` and system()). Ruby already has the internal support for most of this this to manage mutexes, Thread structures, and RNG seed. Currently, no external API is exposed. I can prepare a patch if an API is decided upon. -- http://bugs.ruby-lang.org/