From: jean.boussier@... Date: 2021-04-04T21:55:57+00:00 Subject: [ruby-core:103210] [Ruby master Feature#5446] at_fork callback API Issue #5446 has been updated by byroot (Jean Boussier). > it would be nice to see a use-case that just cannot be solved with IO#close_on_exec The need is not limited to IOs. If you have say some kind of buffered event queue ([example](https://github.com/Shopify/statsd-instrument/pull/280)), you need to clear it after fork otherwise you will send some events twice. > fork() seems hacky now that Ractor exists Seems kind of out of topic to discuss the merits of threads vs process, but even then I wouldn't say so. First Ractors still aren't quite production ready, but even if they were, standalone processes still provide extra isolation that is in some ways superior to threads/ractors. When unicorn timeouts a workers, since it's a distinct process, you know the replacement will be spawned clean. If your workers are thread/ractors you either have to be certain you won't need to kill them, or be certain that your whole software stack will handle it cleanly. And from experience, most code out there doesn't (no blame here, it's incredibly hard to do). Of course you can always consider that a service that get stuck or slow and need to be killed is a bug in itself, but this kind of resiliency as a last line of defence is appreciable. ---------------------------------------- Feature #5446: at_fork callback API https://bugs.ruby-lang.org/issues/5446#change-91284 * 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: