From: daniel@...42.com Date: 2021-04-05T03:07:11+00:00 Subject: [ruby-core:103235] [Ruby master Feature#5446] at_fork callback API Issue #5446 has been updated by Dan0042 (Daniel DeLorme). > > For the vast majority of fork uses in practice it would be much safer if database connections and other fds which are unsafe to keep open across forks were automatically closed. > > Indeed, and it's possible to do that with `IO#close_on_exec`, I apologize, somehow I was thinking that `fork` belongs to the `exec` family of functions and so `IO#close_on_exec` applied to it, but that's obviously not the case. Since ruby doesn't have something like `IO#close_on_fork` then yeah having an `after_fork` hook is very necessary. ---------------------------------------- Feature #5446: at_fork callback API https://bugs.ruby-lang.org/issues/5446#change-91316 * Author: normalperson (Eric Wong) * Status: Assigned * Priority: Normal * Assignee: kosaki (Motohiro KOSAKI) ---------------------------------------- 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. -- https://bugs.ruby-lang.org/ Unsubscribe: