From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Olivier Dion Newsgroups: gmane.lisp.guile.devel Subject: [PATCH] Fix possible deadlock. Date: Tue, 1 Nov 2022 14:34:41 -0400 Message-ID: <20221101183441.15833-1-olivier.dion@polymtl.ca> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="34695"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Olivier Dion To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Tue Nov 01 19:35:25 2022 Return-path: Envelope-to: guile-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1opw6d-0008iT-JC for guile-devel@m.gmane-mx.org; Tue, 01 Nov 2022 19:35:23 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1opw6G-000882-6D; Tue, 01 Nov 2022 14:35:00 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1opw6E-00087F-Cw for guile-devel@gnu.org; Tue, 01 Nov 2022 14:34:58 -0400 Original-Received: from smtp.polymtl.ca ([132.207.4.11]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1opw6C-0001WQ-OV for guile-devel@gnu.org; Tue, 01 Nov 2022 14:34:58 -0400 Original-Received: from laura.ht.home (modemcable094.169-200-24.mc.videotron.ca [24.200.169.94]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 2A1IYg85015361 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Tue, 1 Nov 2022 14:34:49 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 2A1IYg85015361 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=polymtl.ca; s=default; t=1667327689; bh=w4tLj5O8/XRYj8g0dRJLHaRrmfXSg/ED/Ww5QurgW/Q=; h=From:To:Cc:Subject:Date:From; b=GWAMVZOP5vI2FFgWuW1ZcPR/j5+pl9kbXsBbD3DuKEoxx+GLzG1BzmL9IOLHKPAeh Gd+kNzV1079AUa8uCFh9+9vNI27CryalsMnnmfWQ9dy2kC+Z0fwBhcsZ6jD16m979/ m7tmpBF35gVsys0ZO9A4ttTMs+oit2X6xmG/4amI= X-Mailer: git-send-email 2.38.0 X-Poly-FromMTA: (modemcable094.169-200-24.mc.videotron.ca [24.200.169.94]) at Tue, 1 Nov 2022 18:34:42 +0000 Received-SPF: pass client-ip=132.207.4.11; envelope-from=olivier.dion@polymtl.ca; helo=smtp.polymtl.ca X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: "guile-devel" Errors-To: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.devel:21457 Archived-At: If we got interrupted while waiting on our condition variable, we unlock the kernel mutex momentarily while executing asynchronous operations before putting us back into the waiting queue. However, we have to retry acquiring the mutex before getting back into the queue, otherwise it's possible that we wait indefinitely since nobody could be the owner for a while. * libguile/threads.c (lock_mutex): Try acquring the mutex after signal interruption. --- libguile/threads.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/libguile/threads.c b/libguile/threads.c index 280d306bf..0f5cf2ed5 100644 --- a/libguile/threads.c +++ b/libguile/threads.c @@ -1022,14 +1022,7 @@ lock_mutex (enum scm_mutex_kind kind, struct scm_mutex *m, if (err == 0) { - if (scm_is_eq (m->owner, SCM_BOOL_F)) - { - m->owner = current_thread->handle; - scm_i_pthread_mutex_unlock (&m->lock); - return SCM_BOOL_T; - } - else - continue; + goto maybe_acquire; } else if (err == ETIMEDOUT) { @@ -1041,7 +1034,7 @@ lock_mutex (enum scm_mutex_kind kind, struct scm_mutex *m, scm_i_pthread_mutex_unlock (&m->lock); scm_async_tick (); scm_i_scm_pthread_mutex_lock (&m->lock); - continue; + goto maybe_acquire; } else { @@ -1050,6 +1043,14 @@ lock_mutex (enum scm_mutex_kind kind, struct scm_mutex *m, errno = err; SCM_SYSERROR; } + + maybe_acquire: + if (scm_is_eq (m->owner, SCM_BOOL_F)) + { + m->owner = current_thread->handle; + scm_i_pthread_mutex_unlock (&m->lock); + return SCM_BOOL_T; + } } } #undef FUNC_NAME -- 2.38.0