From: eregontp@... Date: 2018-07-19T20:16:51+00:00 Subject: [ruby-core:88011] [Ruby trunk Feature#5446] at_fork callback API Issue #5446 has been updated by Eregon (Benoit Daloze). IMHO, this is is still very much needed for cases using plain fork(). The default close-on-exec behavior is irrelevant for fork() without exec() and does not help. Inheriting, e.g., a database connection in such a case is extremely harmful (data corruption or leaking sensitive data) and a common pitfall. Having a nice at_fork hook would allow to safely disconnect the database in each forked process to avoid accidental file descriptor sharing between forks. Sequel: https://github.com/jeremyevans/sequel/issues/691 Passenger has PhusionPassenger.on_event(:starting_worker_process). They give the example of memcached and by default use it to reestablish ActiveRecord connections: https://www.phusionpassenger.com/library/indepth/ruby/spawn_methods/#unintentional-file-descriptor-sharing Puma has before_fork: https://github.com/puma/puma/pull/754/files Unicorn implements its own before_fork/after_fork: https://www.rubydoc.info/gems/unicorn/5.0.1/Unicorn%2FConfigurator:after_fork @normalperson Don't you think this deserves to be in core? I would guess all of these do not work if rb_fork() is called. What more motivation do we need? Wait that a major incident happen due to unintentional fd sharing because database libraries cannot protect themselves from fork()? ---------------------------------------- Feature #5446: at_fork callback API https://bugs.ruby-lang.org/issues/5446#change-73029 * Author: normalperson (Eric Wong) * Status: Assigned * Priority: Normal * Assignee: kosaki (Motohiro KOSAKI) * Target version: ---------------------------------------- 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: