emacs-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#43066: closed (Hang when forking new process.)


From: GNU bug Tracking System
Subject: bug#43066: closed (Hang when forking new process.)
Date: Tue, 11 May 2021 10:44:02 +0000

Your message dated Tue, 11 May 2021 12:43:33 +0200
with message-id <87mtt11rfu.fsf@gnu.org>
and subject line Re: bug#43066: Hang when forking new process.
has caused the debbugs.gnu.org bug report #43066,
regarding Hang when forking new process.
to be marked as done.

(If you believe you have received this mail in error, please contact
help-debbugs@gnu.org.)


-- 
43066: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=43066
GNU Bug Tracking System
Contact help-debbugs@gnu.org with problems
--- Begin Message --- Subject: Hang when forking new process. Date: Thu, 27 Aug 2020 09:42:47 +0200 User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux)
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

Attachment: t.scm
Description: Binary data

>From 004c0c78c9c21c48b38d76b5d7b356b40c8e5a4a Mon Sep 17 00:00:00 2001
From: Mathieu Othacehe <othacehe@gnu.org>
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


--- End Message ---
--- Begin Message --- Subject: Re: bug#43066: Hang when forking new process. Date: Tue, 11 May 2021 12:43:33 +0200 User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Hi Mathieu,

Mathieu Othacehe <othacehe@gnu.org> skribis:

>>From 004c0c78c9c21c48b38d76b5d7b356b40c8e5a4a Mon Sep 17 00:00:00 2001
> From: Mathieu Othacehe <othacehe@gnu.org>
> 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.

Apologies for overlooking this patch!

This was fixed differently in 5a281e35f4a5ae78fbcf10591d9358bec8f0bee0,
which is in 3.0.7.

Thanks,
Ludo’.


--- End Message ---

reply via email to

[Prev in Thread] Current Thread [Next in Thread]