From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-2.6 required=3.0 tests=AWL,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=no autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id A2DA21F4B4 for ; Sun, 4 Apr 2021 10:52:29 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id CD1FD120BE5; Sun, 4 Apr 2021 19:51:25 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 9D92D120BE3 for ; Sun, 4 Apr 2021 19:51:23 +0900 (JST) Received: by filterdrecv-p3iad2-7d7c446bd4-6v772 with SMTP id filterdrecv-p3iad2-7d7c446bd4-6v772-20-60699A61-1E 2021-04-04 10:52:17.215311352 +0000 UTC m=+1009163.624462397 Received: from herokuapp.com (unknown) by geopod-ismtpd-3-1 (SG) with ESMTP id C4elDfwTSR2RQJHGdnFBqg for ; Sun, 04 Apr 2021 10:52:17.205 +0000 (UTC) Date: Sun, 04 Apr 2021 10:52:17 +0000 (UTC) From: eregontp@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 79245 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 5446 X-Redmine-Issue-Author: normalperson X-Redmine-Issue-Assignee: kosaki X-Redmine-Sender: Eregon X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?KippOI8ZHtTweq7XfQzW93937kJ4QNWwSBuHnaMEcr0Rwfw3mJuxBqnnmbgDt0?= =?us-ascii?Q?4=2FJt8+uqT7WI=2FCFw6Z0IbouGg5tt9G88YlkskiK?= =?us-ascii?Q?8A+Kwfg0jdUvYZ4VFrtvvthlCo1aCkYe1F5EfqI?= =?us-ascii?Q?fhOllfivXl9GGW3B5bcJLPrujrv+EjcDVN66XuO?= =?us-ascii?Q?tusSpJmxef2gSPETWsjxQGnJ5IzLXb4Kvr9j7wu?= =?us-ascii?Q?EI5bMGHg0XgWhpeeY=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 103202 Subject: [ruby-core:103202] [Ruby master Feature#5446] at_fork callback API X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #5446 has been updated by Eregon (Benoit Daloze). > Is there a way to expose Ruby methods/procs as C function pointers with JIT? @normalperson That feels very hacky to me. JIT'ing a function which is only called once seems suboptimal (and difficult to do before it's even called once). Also some Ruby state likely needs to be restored before running any `after_fork` hook. @byroot I agree, we should at least have `after_fork` (or `at_fork(when, &block)`). Honestly I don't understand why we don't have this yet, it's obviously needed and every forking webserver out there ends up having its own hook. For the use-case of purposefully keeping a database connection (which seems extremely rare), maybe one can require the database library with a different path to be explicit that playing with database fd sharing fire is wanted? (or some other mechanism so the `at_fork` hook is not installed in that case). 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. And BTW the current workaround of checking pid is slower also on platforms not supporting `fork`, so it is inefficient for all platforms and webservers. ---------------------------------------- Feature #5446: at_fork callback API https://bugs.ruby-lang.org/issues/5446#change-91277 * 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/