From patchwork Tue Nov 18 13:31:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Yano X-Patchwork-Id: 124622 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0A1E03857003 for ; Tue, 18 Nov 2025 13:32:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0A1E03857003 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=nifty.ne.jp header.i=@nifty.ne.jp header.a=rsa-sha256 header.s=default-1th84yt82rvi header.b=etXmijbC X-Original-To: newlib@sourceware.org Delivered-To: newlib@sourceware.org Received: from mta-snd-w07.mail.nifty.com (mta-snd-w07.mail.nifty.com [IPv6:2001:268:fa30:831:6a:99:e3:27]) by sourceware.org (Postfix) with ESMTPS id DA23C3858CD9 for ; Tue, 18 Nov 2025 13:31:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DA23C3858CD9 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=nifty.ne.jp Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nifty.ne.jp ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DA23C3858CD9 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:268:fa30:831:6a:99:e3:27 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1763472712; cv=none; b=fJKHBNYQ6AQWnkcrQpG/RdFCp4uMhE6IjMG6XS0uCAQQ7PrSOwS/WtI4Y9Wu0sLR29lqsQZoadCYYS60DxvVNnnb3gr49jmpj+z/SUV77u+sLo+mD+bkg7t4JWiI/73bA4Ncv1YLX7SK1Ubqn27J5c2rZwhnrmdoFZYGpc4d5Mk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1763472712; c=relaxed/simple; bh=EXQUjeRg6tPqBBI1kjnHsaYHh+n22olDHgSbUYSSTTM=; h=From:To:Subject:Date:Message-ID:MIME-Version:DKIM-Signature; b=dtHM1+ZwOXoDXrdj0TVm9JTFYqU3ABEdET+gfDQVYIKbWUU8PkoU3rjvDqHUDhCj0yDZ4bYJ3otWvp2hMd0SUHwinWCmMbcutSgojgumjrkVDWya/TSPXSWZEynsHfr1AcufeVvKC3B916BjUdI/Hw5aq3WcDX+SFGHGYLDOQ6Y= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DA23C3858CD9 Received: from HP-Z230 by mta-snd-w07.mail.nifty.com with ESMTP id <20251118133150205.KUMP.19957.HP-Z230@nifty.com>; Tue, 18 Nov 2025 22:31:50 +0900 From: Takashi Yano To: newlib@sourceware.org Cc: Takashi Yano , Tomohiro Kashiwada Subject: [PATCH] newlib: Unlock the mutex while calling atexit()'ed functions Date: Tue, 18 Nov 2025 22:31:33 +0900 Message-ID: <20251118133140.78-1-takashi.yano@nifty.ne.jp> X-Mailer: git-send-email 2.51.0 MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.ne.jp; s=default-1th84yt82rvi; t=1763472710; bh=/awL8EgvjuLynG4f/gJv0rx+u+qIuqyCEhMsx1BzhV0=; h=From:To:Cc:Subject:Date; b=etXmijbCS1ObWJ2O+mQKmU4l6q+GWoAQygrAlQMbDphZqKpMlUKcP8zSVGogICsXJtqv8dFU 99JA+a1KsIHaiFjp4jHmk+5IoIY7wMjGrfa4nJ3PfV9ggtXe3NQ+3Vs807g1mnKELQrAAmG2cM ytxAInjwOGcYzxtRV3spJuJ8GpDPIzIDTX3DuEaxzv9XLBc7Bga5cE+q8V4jzLvmJThO4j4dyA oW18+U8lXiB8HOw4nfW73XEpfFePlu5cCrBzwspRIBesQn3XfqykrjTqCjdEYwU/UsfoKC72Do DYa489gUsDT0vWUjrFEn8piShqIFsIKpDhex6NM/hsZsVtYQ== X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: newlib-bounces~patchwork=sourceware.org@sourceware.org The atexit()'ed function may deadlock if it waits for another thread which calls atexit() in the current __call_atexit.c code. This patch unlock __atexit_recursive_mutex while calling atexit()'ed functions to avoid the deadlock mentioned above. glibc and Darwin do the same, so it sounds reasonable. Addresses: https://cygwin.com/pipermail/cygwin/2025-October/258930.html Reported-by: Tomohiro Kashiwada Reviewed-by: Signed-off-by: Takashi Yano --- newlib/libc/stdlib/__call_atexit.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/newlib/libc/stdlib/__call_atexit.c b/newlib/libc/stdlib/__call_atexit.c index 710440389..44f1f6acc 100644 --- a/newlib/libc/stdlib/__call_atexit.c +++ b/newlib/libc/stdlib/__call_atexit.c @@ -114,6 +114,11 @@ __call_exitprocs (int code, void *d) ind = p->_ind; +#ifndef __SINGLE_THREAD__ + /* Unlock __atexit_recursive_mutex; otherwise, the function fn() may + deadlock if it waits for another thread which calls atexit(). */ + __lock_release_recursive(__atexit_recursive_mutex); +#endif /* Call the function. */ if (!args || (args->_fntypes & i) == 0) fn (); @@ -121,6 +126,9 @@ __call_exitprocs (int code, void *d) (*((void (*)(int, void *)) fn))(code, args->_fnargs[n]); else (*((void (*)(void *)) fn))(args->_fnargs[n]); +#ifndef __SINGLE_THREAD__ + __lock_acquire_recursive(__atexit_recursive_mutex); +#endif /* The function we called call atexit and registered another function (or functions). Call these new functions before