From patchwork Wed Jul 16 14:42:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Yano X-Patchwork-Id: 116419 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 2A5FD3857C7F for ; Wed, 16 Jul 2025 14:43:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2A5FD3857C7F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1752676981; bh=AjcCHKhC/kxBFrvlCf/TpCRCjZ+LDCKIuGwok3VT2RU=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=oC3cx9sYcRDNbFPy+N2dNeTXqLNukbYquWKOYbkOatiJ2SbZSYIsYlhmN1MB0UMiw 4JgGyURAPIeQ4XXaQV9xPBC1s3iWfrWz428L9SnW04PsznOybQuQqMgWmwwHVR0Oa9 c4lP92d7RHVoJzaf6tmadTmfhY6AIm/hmmKzFSM0= X-Original-To: newlib@sourceware.org Delivered-To: newlib@sourceware.org Received: from mta-snd-w09.mail.nifty.com (mta-snd-w09.mail.nifty.com [106.153.227.41]) by sourceware.org (Postfix) with ESMTPS id 256113858402 for ; Wed, 16 Jul 2025 14:42:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 256113858402 ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 256113858402 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1752676957; cv=none; b=obgRhKgDVoyjhCwlMEpUvaNojACBxjXASe7zvPs0Bt5Pnj1II24sB8EjscAxLj+fXA8dK0WYMbZ7IUPNPbYZ6ChYbxZCu4DCoR7G+Br4j55GxKCIUUdipizDZpitAoUnJakMhyWMkmC+aB2NTdz8Fxl1os29/P2zt+LsX8fLG+0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1752676957; c=relaxed/simple; bh=0sWnFsn5SQQffCKw8Kd9Opqyn1BtPkTiscYZpUd1B/s=; h=From:To:Subject:Date:Message-ID:MIME-Version:DKIM-Signature; b=HVXhR5Hg+RkmK/7t/6tz/A01TUFJlX1UCeN+qZlqt8ilF74bKMjUGZiKqXUfhgGN6QWvtPs7Mmosa5nxH4YZmQbFN84nlwOup8CAvClWOWdM3/gA2MSrkhNU+odTess72Ct937xpl/w7YRnXOzxgocRN4Gcz22TiU1wI5NGZM9M= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 256113858402 Received: from localhost.localdomain by mta-snd-w09.mail.nifty.com with ESMTP id <20250716144235272.EWPD.116672.localhost.localdomain@nifty.com>; Wed, 16 Jul 2025 23:42:35 +0900 To: newlib@sourceware.org Cc: Takashi Yano Subject: [PATCH] newlib: fclose: Use sfp lock while fp lock is active Date: Wed, 16 Jul 2025 23:42:02 +0900 Message-ID: <20250716144219.12884-1-takashi.yano@nifty.ne.jp> X-Mailer: git-send-email 2.45.1 MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, 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: , X-Patchwork-Original-From: Takashi Yano via Newlib From: Takashi Yano Reply-To: Takashi Yano Errors-To: newlib-bounces~patchwork=sourceware.org@sourceware.org With the commit 656df313e08a, if a thread acquires sfp lock after another thread calls fclose() and fp lock is acquired, the first thread falls into deadlock if it trys to acquire fp lock. This can happen if the first thread calls __sft_lock_all() while the second thread calls fclose(). This patch reverts the changes for newlib/libc/stdio/fclose.c in the commit 656df313e08a. Addresses: https://cygwin.com/pipermail/cygwin/2025-June/258323.html Fixes: 656df313e08a ("* libc/stdio/fclose.c: Only use sfp lock to guard non-atomic changes of flags and fp lock.") Reported-by: Takashi Yano Reviewed-by: Signed-off-by: Takashi Yano --- newlib/libc/stdio/fclose.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/newlib/libc/stdio/fclose.c b/newlib/libc/stdio/fclose.c index 983ae2cc4..4c81cbf4b 100644 --- a/newlib/libc/stdio/fclose.c +++ b/newlib/libc/stdio/fclose.c @@ -72,6 +72,7 @@ _fclose_r (struct _reent *rptr, int __oldcancel; pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &__oldcancel); #endif + __sfp_lock_acquire (); if (!(fp->_flags2 & __SNLK)) _flockfile (fp); @@ -79,6 +80,7 @@ _fclose_r (struct _reent *rptr, { if (!(fp->_flags2 & __SNLK)) _funlockfile (fp); + __sfp_lock_release (); #ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT pthread_setcancelstate (__oldcancel, &__oldcancel); #endif @@ -101,7 +103,6 @@ _fclose_r (struct _reent *rptr, FREEUB (rptr, fp); if (HASLB (fp)) FREELB (rptr, fp); - __sfp_lock_acquire (); fp->_flags = 0; /* release this FILE for reuse */ if (!(fp->_flags2 & __SNLK)) _funlockfile (fp);