From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id WO0qCAAIl2CogQEAgWs5BA (envelope-from ) for ; Sat, 08 May 2021 23:52:00 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id sJiuAwAIl2B+LAAAbx9fmQ (envelope-from ) for ; Sat, 08 May 2021 21:52:00 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 769D617D14 for ; Sat, 8 May 2021 23:51:59 +0200 (CEST) Received: from localhost ([::1]:39730 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lfUre-0006P2-Lb for larch@yhetil.org; Sat, 08 May 2021 17:51:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35972) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lfNLH-0000gW-9Y for bug-guix@gnu.org; Sat, 08 May 2021 09:50:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:38881) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lfNLH-0001fJ-2c for bug-guix@gnu.org; Sat, 08 May 2021 09:50:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lfNLH-0001UR-0k for bug-guix@gnu.org; Sat, 08 May 2021 09:50:03 -0400 X-Loop: help-debbugs@gnu.org Subject: bug#41948: [PATCH] Fix some finalizer thread race conditions Resent-From: Andrew Whatson Original-Sender: "Debbugs-submit" Resent-CC: bug-guix@gnu.org Resent-Date: Sat, 08 May 2021 13:50:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 41948 X-GNU-PR-Package: guix X-GNU-PR-Keywords: To: 41948@debbugs.gnu.org Received: via spool by 41948-submit@debbugs.gnu.org id=B41948.16204817875708 (code B ref 41948); Sat, 08 May 2021 13:50:02 +0000 Received: (at 41948) by debbugs.gnu.org; 8 May 2021 13:49:47 +0000 Received: from localhost ([127.0.0.1]:50425 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lfNL0-0001U0-LH for submit@debbugs.gnu.org; Sat, 08 May 2021 09:49:46 -0400 Received: from mail-pf1-f170.google.com ([209.85.210.170]:44955) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lfNKy-0001Tu-Us for 41948@debbugs.gnu.org; Sat, 08 May 2021 09:49:45 -0400 Received: by mail-pf1-f170.google.com with SMTP id a5so2849122pfa.11 for <41948@debbugs.gnu.org>; Sat, 08 May 2021 06:49:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=RxrH24KBt454bZ1I+OTTU6d+JAjesLNdHGLUa8r0qAU=; b=Lhg53Gc1tea5uwSl584f4kpaBEH+Nslz4HOUDAjsMcP9THW73b/aLXaiVuUR4wBShd sN+aRBC4CpHbnUU4jH1Fu2ODIYu0t70RaHj4wDM/56+p9+Jqr2yLXTvdHs7rAiYtc8j5 9FXNvEMC5GCcAGiCKrSS+8IGxNN19ZXXZvB6YXENxVkWibywrVQ1gb0IxmKimzMsJtJc zH3gXpraPL+NLIrKbkmedrmq3gIiaCDBdmm2mpu6uqNgo9armax0PzXxm8wir4XHdt/v HBDSFNoQRbjnHrPGzcj2OOmKDlO5MHD8dmCMvXO+7kpeIRfKZHG7QqCNv6HEElHx+Jyq D3xQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RxrH24KBt454bZ1I+OTTU6d+JAjesLNdHGLUa8r0qAU=; b=XD0ACuDYu6SGWnugckoGJQDiMXWrASt36rA5iMVwN4rCWAcOCd1e0kyll5yLxqgxZr orqvzqBB0DHHFnL8tUj38AOZd3Xb11n4riJ0ZLXmMaIxYeMf+9J7pa+W0aNvvnVNVWqy U9cKiiYexrPbiIlIIvgpZycjWRUh600PuZDhsyLH7bz8cJeBaCrj88tFbfJ6UNRZTvwK NLQh1fUwY0MQcju2pZYjAxFpfpUsnA8gc+ONBhdaVM5nSkb6lshRKx94PtPC8BVHrlDL R0FwUrsSvX7YRCew0rTIpfHi47/fCEKH8LTieolxtyUVpMtcejvq5OPeWDTYHznTtF0X gNOQ== X-Gm-Message-State: AOAM5303sGWXbMTQ89paDIPBgsOXb1dD28AxP4rOkdD+KRp8XERZS0I4 SwoPRXASy6Eg1gI8TRitcPQ192SBMb4= X-Google-Smtp-Source: ABdhPJzO+bL/BcIiuAOyTIZGqiJMepOwH+fFzBEoMBJcmci8y8jMdse5VOku0QTQt2/P7Ihx6CXIHA== X-Received: by 2002:a63:da10:: with SMTP id c16mr15317659pgh.221.1620481779009; Sat, 08 May 2021 06:49:39 -0700 (PDT) Received: from muon.fritz.box (220-235-201-222.tpgi.com.au. [220.235.201.222]) by smtp.gmail.com with ESMTPSA id u21sm6858442pfm.89.2021.05.08.06.49.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 May 2021 06:49:38 -0700 (PDT) From: Andrew Whatson Date: Sat, 8 May 2021 23:49:08 +1000 Message-Id: <20210508134908.3133397-2-whatson@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210508134908.3133397-1-whatson@gmail.com> References: <87im3ta7dg.fsf@gnu.org> <20210508134908.3133397-1-whatson@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-Mailman-Approved-At: Sat, 08 May 2021 17:51:31 -0400 X-BeenThere: bug-guix@gnu.org List-Id: Bug reports for GNU Guix List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andrew Whatson Errors-To: bug-guix-bounces+larch=yhetil.org@gnu.org Sender: "bug-Guix" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1620510719; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=RxrH24KBt454bZ1I+OTTU6d+JAjesLNdHGLUa8r0qAU=; b=PtUlxdWZ4SYiuC6iY0KHilxp7iIRnbBXHRAxJgXprO6QoBPJRpZ7sF68QFHMz7QY9eG8l+ k8JKaRbdNZOP4boC+69wLYLMsPEB2SF5s+otysp00DswRVI+GtCcID414cb4zykTAvSHfE +tBMcDymoRDiB4IK+gG4/ZsYHHLNodc5sgguWUYoF9tfM+dpj1OmUoQP/xaw/lqe9zRm50 97mP+me4KSNxtcfBKWLxeFj+K0pt72tlQ4QzfVgsxkwVN567lQmxujsxFNOsqRa9cZPClR N5kxjGekMTElj9DUy7+qe58R1sGhaRccxplubOjTUECx2VG3zzmZtJq7knwM0Q== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1620510719; a=rsa-sha256; cv=none; b=Z6nDcwNPpQU/aHEijmBjtM45pSjYXSFc5Bu7Ey6WPn3SFeEWAS+Uev3HRXYGMo/Q0B9i0d y1wgzq7A/ipc4gFl1MyDfSnDu0CPEjiOrv9BUowuyrLTTIYdy9EEu6MSUnN1riqESCiWvA LOGJfeHJDagR6uuwewGn5jD50Ij9ThNfLk+ki+z9uYakIK7kIqN5au7sk29uSGKAtQOO+t ayJgeezgGFWUZNnS2OMzHrq4B1OH/ctFBLcDWZQPa8sxggRoYCIUDhKFKBiqzBA/arE1Nz iisH8xDpPvbpioR6xJW1PdnDyP1zEK9wYrlwyxjzwZ3/2NyIOCp2AIqRb6vMmA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20161025 header.b=Lhg53Gc1; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of bug-guix-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=bug-guix-bounces@gnu.org X-Migadu-Spam-Score: 3.65 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20161025 header.b=Lhg53Gc1; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of bug-guix-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=bug-guix-bounces@gnu.org X-Migadu-Queue-Id: 769D617D14 X-Spam-Score: 3.65 X-Migadu-Scanner: scn0.migadu.com X-TUID: xupSgFoxkY96 * libguile/finalizers.c (finalization_pipe): Initialize. (reset_finalization_pipe): Factored out. (start_finalization_thread): Create the pipe immediately before launching the thread. Ensure the pipe is cleaned up if thread creation fails. Update the finalizer callback if thread creation succeeds. (stop_finalization_thread): Clean up the pipe after stopping the thread. (spawn_finalizer_thread): Remove finalizer callback logic. (scm_set_automatic_finalization_enabled): Remove pipe management. (scm_init_finalizer_thread): Remove pipe management. --- libguile/finalizers.c | 45 +++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/libguile/finalizers.c b/libguile/finalizers.c index 0ae165fd1..5122e5fe3 100644 --- a/libguile/finalizers.c +++ b/libguile/finalizers.c @@ -24,6 +24,7 @@ # include #endif +#include #include #include #include @@ -170,7 +171,7 @@ queue_finalizer_async (void) #if SCM_USE_PTHREAD_THREADS -static int finalization_pipe[2]; +static int finalization_pipe[2] = { -1, -1 }; static scm_i_pthread_mutex_t finalization_thread_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER; static pthread_t finalization_thread; @@ -190,6 +191,15 @@ notify_about_to_fork (void) full_write (finalization_pipe[1], &byte, 1); } +static void +reset_finalization_pipe (void) +{ + close (finalization_pipe[0]); + close (finalization_pipe[1]); + finalization_pipe[0] = -1; + finalization_pipe[1] = -1; +} + struct finalization_pipe_data { char byte; @@ -254,15 +264,25 @@ start_finalization_thread (void) scm_i_pthread_mutex_lock (&finalization_thread_lock); if (!finalization_thread_is_running) { + assert (finalization_pipe[0] == -1); + /* Use the raw pthread API and scm_with_guile, because we don't want to block on any lock that scm_spawn_thread might want to take, and we don't want to inherit the dynamic state (fluids) of the caller. */ - if (pthread_create (&finalization_thread, NULL, - run_finalization_thread, NULL)) - perror ("error creating finalization thread"); + if (pipe2 (finalization_pipe, O_CLOEXEC) != 0) + perror ("error creating finalization pipe"); + else if (pthread_create (&finalization_thread, NULL, + run_finalization_thread, NULL)) + { + reset_finalization_pipe (); + perror ("error creating finalization thread"); + } else - finalization_thread_is_running = 1; + { + GC_set_finalizer_notifier (notify_finalizers_to_run); + finalization_thread_is_running = 1; + } } scm_i_pthread_mutex_unlock (&finalization_thread_lock); } @@ -276,6 +296,8 @@ stop_finalization_thread (void) notify_about_to_fork (); if (pthread_join (finalization_thread, NULL)) perror ("joining finalization thread"); + + reset_finalization_pipe (); finalization_thread_is_running = 0; } scm_i_pthread_mutex_unlock (&finalization_thread_lock); @@ -284,7 +306,6 @@ stop_finalization_thread (void) static void spawn_finalizer_thread (void) { - GC_set_finalizer_notifier (notify_finalizers_to_run); start_finalization_thread (); } @@ -368,8 +389,6 @@ scm_set_automatic_finalization_enabled (int enabled_p) if (enabled_p) { #if SCM_USE_PTHREAD_THREADS - if (pipe2 (finalization_pipe, O_CLOEXEC) != 0) - scm_syserror (NULL); GC_set_finalizer_notifier (spawn_finalizer_thread); #else GC_set_finalizer_notifier (queue_finalizer_async); @@ -381,10 +400,6 @@ scm_set_automatic_finalization_enabled (int enabled_p) #if SCM_USE_PTHREAD_THREADS stop_finalization_thread (); - close (finalization_pipe[0]); - close (finalization_pipe[1]); - finalization_pipe[0] = -1; - finalization_pipe[1] = -1; #endif } @@ -423,10 +438,6 @@ scm_init_finalizer_thread (void) { #if SCM_USE_PTHREAD_THREADS if (automatic_finalization_p) - { - if (pipe2 (finalization_pipe, O_CLOEXEC) != 0) - scm_syserror (NULL); - GC_set_finalizer_notifier (spawn_finalizer_thread); - } + GC_set_finalizer_notifier (spawn_finalizer_thread); #endif } -- 2.31.1