From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Mathieu Othacehe Newsgroups: gmane.lisp.guile.bugs Subject: bug#43066: Hang when forking new process. Date: Thu, 27 Aug 2020 09:42:47 +0200 Message-ID: <87r1rsh6vc.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="8614"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) To: 43066@debbugs.gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Thu Aug 27 09:43:06 2020 Return-path: Envelope-to: guile-bugs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kBCYr-00028K-Pz for guile-bugs@m.gmane-mx.org; Thu, 27 Aug 2020 09:43:05 +0200 Original-Received: from localhost ([::1]:45914 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kBCYq-0008RQ-Pt for guile-bugs@m.gmane-mx.org; Thu, 27 Aug 2020 03:43:04 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:34846) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kBCYo-0008RJ-3w for bug-guile@gnu.org; Thu, 27 Aug 2020 03:43:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:58157) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kBCYn-0007ks-RX for bug-guile@gnu.org; Thu, 27 Aug 2020 03:43:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kBCYn-0002pg-Pe for bug-guile@gnu.org; Thu, 27 Aug 2020 03:43:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Mathieu Othacehe Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Thu, 27 Aug 2020 07:43:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 43066 X-GNU-PR-Package: guile X-Debbugs-Original-To: bug-guile@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.159851417210870 (code B ref -1); Thu, 27 Aug 2020 07:43:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 27 Aug 2020 07:42:52 +0000 Original-Received: from localhost ([127.0.0.1]:41470 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kBCYe-0002pG-Jf for submit@debbugs.gnu.org; Thu, 27 Aug 2020 03:42:52 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:53810) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kBCYb-0002p7-Uj for submit@debbugs.gnu.org; Thu, 27 Aug 2020 03:42:51 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:34814) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kBCYb-0008Qw-Nl for bug-guile@gnu.org; Thu, 27 Aug 2020 03:42:49 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:48011) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kBCYb-0007jG-Fh for bug-guile@gnu.org; Thu, 27 Aug 2020 03:42:49 -0400 Original-Received: from [2a01:e0a:19b:d9a0:d1f2:1c2d:9e7a:dd0] (port=51724 helo=cervin) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1kBCYb-0007YS-2z for bug-guile@gnu.org; Thu, 27 Aug 2020 03:42:49 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Original-Sender: "bug-guile" Xref: news.gmane.io gmane.lisp.guile.bugs:9871 Archived-At: --=-=-= Content-Type: text/plain Hello, When forking, the finalization pipe file descriptors are inherited. If the child process spawns a finalization thread, it will use a copy of its parent finalization pipe file descriptors. Hence, if the parent tries to stop its finalization thread, by forking another process for instance, it may stop the child finalization thread and hang forever waiting for its own finalization thread to stop. Here's a small reproducer attached. On my machine, the program hangs around iteration 100. Note that this has previously been discussed here[1]. The attached patch should fix this by closing the finalization pipe file descriptor copies in the child right after forking and opening a new pipe by calling scm_init_finalizer_thread. Thanks, Mathieu [1]: https://issues.guix.gnu.org/41948 --=-=-= Content-Type: application/octet-stream Content-Disposition: attachment; filename=t.scm Content-Transfer-Encoding: base64 KHVzZS1tb2R1bGVzIChpY2UtOSBtYXRjaCkpCgooZGVmaW5lIChzdGFydC1wcm9jZXNzIGNvbW1h bmQpCiAgKG1hdGNoIChwcmltaXRpdmUtZm9yaykKICAgICgwCiAgICAgKGV4ZWNsICIvYmluL3No IiAic2giICItYyIgY29tbWFuZCkpCiAgICAoXyAjdCkpKQoKKG1hdGNoIChwcmltaXRpdmUtZm9y aykKICAoMAogICAod2hpbGUgI3QKICAgICAoZ2MpCiAgICAgKHVzbGVlcCAyMDAwMDApKSkKICAo cGlkCiAgIChsZXQgbG9vcCAoKGNvdW50IDApKQogICAgIChmb3JtYXQgI3QgIkZvcmtpbmcgfmF+ JSIgY291bnQpCiAgICAgKHN0YXJ0LXByb2Nlc3MgInNsZWVwIDEiKQogICAgICh1c2xlZXAgKHJh bmRvbSAyMDAwMDApKQogICAgIChsb29wICgxKyBjb3VudCkpKSkpCg== --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Close-finalization-pipe-after-forking.patch >From 004c0c78c9c21c48b38d76b5d7b356b40c8e5a4a Mon Sep 17 00:00:00 2001 From: Mathieu Othacehe Date: Thu, 27 Aug 2020 09:16:55 +0200 Subject: [PATCH] Close finalization pipe after forking. When forking, the finalization pipe file descriptors are inherited. If the child process spawns a finalization thread, it will use a copy of its parent finalization pipe file descriptors. Hence, if the parent tries to stop its finalization thread, by forking another process for instance, it may stop the child finalization thread and hang forever for its own finalization thread to stop. Fix it by closing the finalization pipe file descriptor copies in the child right after forking and opening a new pipe by calling scm_init_finalizer_thread. * libguile/finalizers.c (scm_i_finalizer_post_fork): New function. * libguile/finalizers.c (scm_i_finalizer_post_fork): Declare it. * libguile/posix.c (scm_fork): Call it in the child process, right after forking. --- libguile/finalizers.c | 13 +++++++++++++ libguile/finalizers.h | 1 + libguile/posix.c | 4 ++++ 3 files changed, 18 insertions(+) diff --git a/libguile/finalizers.c b/libguile/finalizers.c index 0ae165fd1..b1803b34b 100644 --- a/libguile/finalizers.c +++ b/libguile/finalizers.c @@ -305,6 +305,19 @@ scm_i_finalizer_pre_fork (void) #endif } +void +scm_i_finalizer_post_fork (void) +{ +#if SCM_USE_PTHREAD_THREADS + if (automatic_finalization_p) + { + close (finalization_pipe[0]); + close (finalization_pipe[1]); + scm_init_finalizer_thread (); + } +#endif +} + diff --git a/libguile/finalizers.h b/libguile/finalizers.h index 44bafb22e..866e4d1eb 100644 --- a/libguile/finalizers.h +++ b/libguile/finalizers.h @@ -36,6 +36,7 @@ SCM_INTERNAL void scm_i_add_resuscitator (void *obj, scm_t_finalizer_proc, void *data); SCM_INTERNAL void scm_i_finalizer_pre_fork (void); +SCM_INTERNAL void scm_i_finalizer_post_fork (void); /* CALLBACK will be called after each garbage collection. It will be called from a finalizer, which may be from an async or from another diff --git a/libguile/posix.c b/libguile/posix.c index 47769003a..022bda6e3 100644 --- a/libguile/posix.c +++ b/libguile/posix.c @@ -1247,6 +1247,10 @@ SCM_DEFINE (scm_fork, "primitive-fork", 0, 0, 0, pid = fork (); if (pid == -1) SCM_SYSERROR; + + if (!pid) + scm_i_finalizer_post_fork (); + return scm_from_int (pid); } #undef FUNC_NAME -- 2.28.0 --=-=-=--