From: eregontp@... Date: 2018-07-20T12:06:27+00:00 Subject: [ruby-core:88017] [Ruby trunk Feature#5446] at_fork callback API Issue #5446 has been updated by Eregon (Benoit Daloze). normalperson (Eric Wong) wrote: > It's been a known problem for decades, now (at least since the > days of mod_perl + DBI on Apache 1.x); and AFAIK there's no data > leaks from it. Anybody who makes that mistake would likely > raise/crash/burn long before seeing, much less leaking sensitive data. Yes, it's not a new problem. I disagree about no production leaks, because it happened to me on a website running for a national programming contest. For most of the contest it was fine as one process was able to handle the load, but at some point the webserver decided to spawn another process by forking, people starting seeing each's other solution, the scores were corrupted and everyone was puzzled as to what happened. We had to stop the contest due to this. I want to help protect future programmers from such bugs, if at all possible. And I believe it's possible. > I agree with Jeremy on this; it will likely cause new problems > and surprises if libraries use it. Let's design it so it doesn't. What's the harm/surprise to reconnect in at_fork(:after_child) for instance? The current hooks are webserver-specific and so migrating between unicorn/puma/passenger/etc means it's quite easy to forget to adapt to the new webserver hook, which would trigger this bug. Especially if e.g., using ActiveRecord with Passenger, where the hook is automatic and migrating to another forking webserver. Having standard hooks solves this, also works for rb_f_fork(), and empowers libraries to solve this issue (not just DB libraries but also concurrent-ruby (see above), etc). ---------------------------------------- Feature #5446: at_fork callback API https://bugs.ruby-lang.org/issues/5446#change-73036 * 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: