Race during the first use of pthread_rwlock_{rd,wr}lock from PTHREAD_RWLOCK_INITIALIZER
Mark Geisert
mark@maxrnd.com
Sat Oct 3 08:12:53 GMT 2026
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.
..mark
More information about the Cygwin
mailing list