delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2026/10/04/00:43:00

DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 6944h0ec2464832
Authentication-Results: delorie.com; dmarc=pass (p=none dis=none) header.from=cygwin.com
Authentication-Results: delorie.com; spf=pass smtp.mailfrom=cygwin.com
DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 6944h0ec2464832
Authentication-Results: delorie.com;
dkim=pass (1024-bit key, unprotected) header.d=cygwin.com header.i=@cygwin.com header.a=rsa-sha256 header.s=default header.b=q6OoQDZF
X-Recipient: archive-cygwin AT delorie DOT com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D0BFF4BA900B
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
s=default; t=1791088962;
bh=RMSDD75Hy84y9SatRWVldXWEAs4oJxhIPt7CZregkPk=;
h=Date:Subject:To:References:In-Reply-To:List-Id:List-Unsubscribe:
List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:
From;
b=q6OoQDZFjQk+r4Lo+LZXr82NBc1Rqd+LII8H+utlVwOZu70eM97bSUBBwgcBDUvis
qXNdzTmi6c3t6eoHceepttcTeYCRx7ywJVApP4EV/wcE2VSv9dAHDclo6kRZI/LkzZ
x4+h6ID9QIMKPLDxkHnfYup8LFK7mNJIMmpjeFSE=
X-Original-To: cygwin AT cygwin DOT com
Delivered-To: cygwin AT cygwin DOT com
DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3DD7E4BA79AB
ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3DD7E4BA79AB
ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1791088914; cv=none;
b=EtG9umGpjFrbKf5Q+A4ViUpmhJL0s3J1lr1LOJ+2pWqBf4fOEgBmGOmcqIQsUSUd9YVegtulE8NGmvgY8gidVwcSzIJmgjY+gSeAWD4yL6iTqPWa6kSWSdjbZUSkJDqeFKUAmT98Lu88Lbw1bXiFfp5hikchjxghDR4+MV2c07M=
ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key;
t=1791088914; c=relaxed/simple;
bh=4T0Kq7lCbpjaJsAtLEeCINik0Whd5FdTcw3FvdzcSao=;
h=Message-ID:Date:MIME-Version:Subject:To:From;
b=iUXyhK/1S6/JFSLOEmbHQf5XFhvgJGYWLKDtPyBmOJ+OlolWPORjLsTBoHO5A9aMGV5GPY32wbmtgEPZtJyaJq0VU0qagfZj/MR0oQounS4uavJwUxsS5rSxquKaispG+tqky5EKRqZbLj58EosSfGb9zJ0Mo67+ppGj8OveLR0=
ARC-Authentication-Results: i=1; sourceware.org
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3DD7E4BA79AB
Message-ID: <b9898bd6-5d42-49b3-9d93-362ef4ebe11d@maxrnd.com>
Date: Sat, 3 Oct 2026 21:41:47 -0700
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: Race during the first use of pthread_rwlock_{rd,wr}lock from
PTHREAD_RWLOCK_INITIALIZER
To: cygwin AT cygwin DOT com
References: <897197ae-26c9-498e-b275-da16047182d1 AT maxrnd DOT com>
<1791015922865 DOT 3643366309 DOT 1677325721 AT gmail DOT com>
In-Reply-To: <1791015922865.3643366309.1677325721@gmail.com>
X-BeenThere: cygwin AT cygwin DOT com
X-Mailman-Version: 2.1.30
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Unsubscribe: <https://cygwin.com/mailman/options/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=unsubscribe>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-request AT cygwin DOT com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=subscribe>
From: Mark Geisert via Cygwin <cygwin AT cygwin DOT com>
Reply-To: Mark Geisert <mark AT maxrnd DOT com>
Errors-To: cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com
Sender: "Cygwin" <cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com>
X-MIME-Autoconverted: from base64 to 8bit by delorie.com id 6944h0ec2464832

Hello kikairoya,

On 10/3/2026 2:09 AM, kikairoya via Cygwin wrote:
> Hi Mark,
> 
> Thank you for the investigation.
> 
> 
> 2026年10月3日 17:12:53 (+09:00) で、Mark Geisert via Cygwin さんが書きました:
> 
>> Hello kikairoya,
>>
>> On 9/20/2026 2:19 AM, Mark Geisert via Cygwin wrote:
>>> Hello kikairoya,
>>> On 9/19/2026 8:34 PM, kikairoya via Cygwin wrote:
>>>> Hello,
>>>>
>>>> I've found a race during the first use of pthread_rwlock_{rd,wr}lock
>>>> (reproducer attached; requires -lsynchronization to build).
>>>>
>>>> Concurrent calls to pthread_rwlock_{rd,wr}lock on a rwlock initialized
>>>> with PTHREAD_RWLOCK_INITIALIZER can cause multiple internal
>>>> initializations, resulting in some locks being lost.
>>>>
>>>> It seems that pthread_rwlock::init (cygwin/thread.cc:2441) needs to
>>>> check whether the rwlock is still PTHREAD_RWLOCK_INITIALIZER while
>>>> holding rwlock_initialization_lock, as pthread_mutex::init does.
>>>>
>>>> pthread_cond::init appears to need the same fix as well.
>>> Thanks for the report and especially for the STC (simple test case).
>>> I will investigate further.
>> I've tried to fix the behavior you're seeing in two ways.  First by using ::is_initialized(rwlock) and second by seeing if *rwlock == PTHREAD_RWLOCK_INITIALIZER, each while holding rwlock_initialization_lock.
>>
>> Restarting Cygwin with the first fix didn't get very far.. bash partially starts but exits immediately.  With the second fix bash seems OK but X Window clients can't start and sshd can't start either.  Your STC does operate correctly with the second fix to Cygwin.
>>
>> I believe your STC is relying on undefined behavior of pthread_rwlock_init().  Because you have four threads contending for the same location in memory, the following sentence from the POSIX standard defining that function applies:
>>> Results are undefined if pthread_rwlock_init() is called specifying
>>> an already initialized read-write lock.
>>
>> I assume (or you've already said ;-) you're comparing Cygwin with Linux.  Linux may well have a defined action for the situation that Cygwin doesn't.  I'm not sure how to proceed just yet.
> 
> In my reproducer, pthread_rwlock_init is called once before launching threads when INIT_LOCK_EXPLICITLY is defined; otherwise, it is never called. Therefore, the UB doesn't apply here.

I could have been more accurate in my comment.  When your STC does not 
#define INIT_LOCK_EXPLICITLY, the initialization of rwl is handled on 
first use.  So one of the threads is able to successfully initialize 
rwl.  Then the other threads encounter an initialized rwl during their 
initialization attempts and this is why the undefined case applies.
> pthread_rwlock::init should handle the following situations:
> - the first use (lock) of PTHREAD_RWLOCK_INITIALIZER
> - call of pthread_rwlock_init on PTHREAD_RWLOCK_INITIALIZER
> - call of pthread_rwlock_init on a zero-initialized rwlock
> - call of pthread_rwlock_init on a destroyed rwlock
> 
> I believe the third and fourth situations can't be handled by simply checking if is_initializer(rwlock), or *rwlock == PTHREAD_RWLOCK_INITIALIZER.

Friendly question: POSIX doesn't seem to mention the latter two 
situations, so why are you depending on them?  Making the changes for 
pthread_rwlock_init implies the same should be done everywhere it makes 
sense, i.e., pthread_*_init.  If you know of an OS where this works the 
way you say, can you tell us?  I'm not promising anything; I'm looking 
for supporting examples :-).

> Given that pthread_mutex_lock passes *mutex as the third argument to pthread_mutex::init, I speculate that the parameter is intended to handle such situations.
> If so, pthread_rwlock_{rd,wr}lock may also need to pass *rwlock to pthread_rwlock::init.

I do concur with this, modulo example(s) from some other OS I can 
examine source for.

..mark

-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2026   by DJ Delorie     Updated Sep 20 2026