From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 0djkK9Irhmk8fSwAWB0awg (envelope-from ) for ; Fri, 06 Feb 2026 12:58:42 -0500 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=polymtl.ca header.i=@polymtl.ca header.a=rsa-sha256 header.s=oct2025 header.b=JibXoNz6; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id AE1041E08D; Fri, 06 Feb 2026 12:58:42 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 1BAF61E08D for ; Fri, 06 Feb 2026 12:58:42 -0500 (EST) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 793B649B0BAF for ; Fri, 6 Feb 2026 17:58:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 793B649B0BAF Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=polymtl.ca header.i=@polymtl.ca header.a=rsa-sha256 header.s=oct2025 header.b=JibXoNz6 Received: from smtp.polymtl.ca (smtp.polymtl.ca [132.207.4.11]) by sourceware.org (Postfix) with ESMTPS id 9C1654CF2FEC for ; Fri, 6 Feb 2026 17:58:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9C1654CF2FEC Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=polymtl.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=polymtl.ca ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9C1654CF2FEC Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=132.207.4.11 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1770400695; cv=none; b=DwptWfjbZeIXAXCNETw7CewIcwgUEBwBj+auWQzn0fK5hNgIkpknrqSARQkr43mY3wb7cPUqVBVAWf+su0TBwVxx59T+/yp57cAQzFIsBepZDuOIKZXRGKmXG1zwpi2MvmvLcWvtObd5BPdFFjNjD8VwLqT2MuTjkXV45ecOP1w= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1770400695; c=relaxed/simple; bh=x2Qyqs1fXhXZeyhe3ci9H7KCp+ouUgdFzvkCj73gWpQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=mxYShadpBePdHgdKoJxmXBj94qQcyxB0ucUwOIVNwdlREfm2A6tP635ltVvwstY18TZOHQoPTrLZYYex06F4U1ht/U30/DGuoMkuZAsb0Eb4D7m3hDjsE6hWG8Cw+iUhlTxc2Y8WQghWWmCorkGwB1mFRdLC3i4Au1bmYOoYxZM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9C1654CF2FEC Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 616Hw8ca058841 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 6 Feb 2026 12:58:13 -0500 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 616Hw8ca058841 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=polymtl.ca; s=oct2025; t=1770400694; bh=Vo25PboeLhxNJQu7BmdpUNZjoMRXod1TRWQOOzMkFAQ=; h=From:To:Cc:Subject:Date:From; b=JibXoNz6Fq1ZE8WOZh/+nl5zG/XIRsxzBqWAdtrwJHFJ0Y/fIXuI/ZFBJLBzc5qM0 lkoEF4oJsnNi2A6GaGLdxibmx/gUnp+Fpf1GM/ZaQm6XtQ4d+XD/klYhoZ0Fpq+wuJ TvEGJad/ZYfd9hyzdzppG5bO8d30/pVBcaSoc9nskn0D9tynO1xZXXB4hV+LDTWSsS yqSQ5jkLPUcB0zHXjwlKUfOlnpDm6S+N37Z38mH18DdExmcqJdWH1xJ9HvRuuwdooJ Eo5QVLg0eK4/H0GT/WEgufTfpuMfbCt48dW3v5ULQCz5gU1W4wpvEkFS8jfugxbWii jq8V+BpC4oOJg== Received: by simark.ca (Postfix) id 7E05F1E08D; Fri, 06 Feb 2026 12:58:08 -0500 (EST) From: simon.marchi@polymtl.ca To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH] gdb/testsuite: fix race condition in gdb.replay/missing-thread.c Date: Fri, 6 Feb 2026 12:57:56 -0500 Message-ID: <20260206175807.631814-1-simon.marchi@polymtl.ca> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Fri, 6 Feb 2026 17:58:09 +0000 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org From: Simon Marchi I get frequent failures in gdb.replay/missing-thread.exp, starting with: FAIL: gdb.replay/missing-thread.exp: non_stop=on: record_initial_logfile: continue (timeout) I tracked this down to a race condition in the test program, causing it not to generate the expected SIGTRAP. This can be reproduced by adding a sleep(1) just after the pthread_create. The expected behavior is: - The main thread starts a second thread - The main thread blocks on pthread_cond_wait, waiting to be notified by the second thread (to make sure the second thread had time to start) - The second thread notifies the main thread using pthread_cond_signal - The main thread sends a SIGTRAP to the second thread However, this can happen: - The main thread starts a second thread - The second thread calls pthread_cond_signal, while no one is waiting to the condvar - The main thread blocks on pthread_cond_wait forever Fix it by introducing a separate boolean predicate protected by the shared mutex, and looping until it is true. This is the way pthread_cond_wait is meant to be used. From pthread_cond_wait(3): When using condition variables there is always a boolean predicate involving shared variables associated with each condition wait that is true if the thread should proceed. Spurious wakeups from the pthread_cond_wait() or pthread_cond_timedwait() functions may occur. Since the return from pthread_cond_wait() or pthread_cond_timedwait() does not imply anything about the value of this predicate, the predicate should be re-evaluated upon such return. Finally, make things static, just out of principle, and fix minor formatting issues. Change-Id: I62ba2085a2d506dc3d91e32cd5de48c43a3ff55e --- gdb/testsuite/gdb.replay/missing-thread.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/gdb/testsuite/gdb.replay/missing-thread.c b/gdb/testsuite/gdb.replay/missing-thread.c index 0a17be1c7dd1..aad6396f24c2 100644 --- a/gdb/testsuite/gdb.replay/missing-thread.c +++ b/gdb/testsuite/gdb.replay/missing-thread.c @@ -22,23 +22,28 @@ #include #include -pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; -pthread_cond_t g_condvar = PTHREAD_COND_INITIALIZER; +static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t g_condvar = PTHREAD_COND_INITIALIZER; +static bool g_ready = false; -void * +static void * worker_function (void *arg) { printf ("In worker, about to notify\n"); + + pthread_mutex_lock (&g_mutex); + g_ready = true; + pthread_mutex_unlock (&g_mutex); pthread_cond_signal (&g_condvar); while (true) - sleep(1); + sleep (1); return NULL; } int -main() +main () { pthread_t my_thread; @@ -46,7 +51,8 @@ main() assert (result == 0); pthread_mutex_lock (&g_mutex); - pthread_cond_wait (&g_condvar, &g_mutex); + while (!g_ready) + pthread_cond_wait (&g_condvar, &g_mutex); printf ("In main, have been woken.\n"); pthread_mutex_unlock (&g_mutex); base-commit: deb47060b5812c6c41ecc790cb28898fcbc45c93 -- 2.53.0