Forum: Ruby-core at_fork callback API

398856ea967f3cab2dbe3df99d732069?d=identicon&s=25 tmm1 (Aman Gupta) (Guest)
on 2013-11-30 22:53
(Received via mailing list)
Issue #5446 has been updated by tmm1 (Aman Gupta).

Here is another example of a gem which implement its own fork hooks:
Feature #5446: at_fork callback API

Author: normalperson (Eric Wong)
Status: Assigned
Priority: Low
Assignee: kosaki (Motohiro KOSAKI)
Target version: next minor

It would be good if Ruby provides an API for registering fork()

This allows libraries to automatically and agnostically reinitialize
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
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

Ruby already has the internal support for most of this this to manage
Thread structures, and RNG seed.  Currently, no external API is exposed.
I can
prepare a patch if an API is decided upon.
This topic is locked and can not be replied to.