GNU bug report logs - #55356
join-thread unusable if timeout was hit

Previous Next

Package: guile;

Reported by: angry rectangle <angryrectangle <at> cock.li>

Date: Wed, 11 May 2022 02:30:01 UTC

Severity: normal

Done: Ludovic Courtès <ludo <at> gnu.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 55356 in the body.
You can then email your comments to 55356 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-guile <at> gnu.org:
bug#55356; Package guile. (Wed, 11 May 2022 02:30:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to angry rectangle <angryrectangle <at> cock.li>:
New bug report received and forwarded. Copy sent to bug-guile <at> gnu.org. (Wed, 11 May 2022 02:30:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: angry rectangle <angryrectangle <at> cock.li>
To: bug-guile <at> gnu.org
Subject: join-thread unusable if timeout was hit
Date: Tue, 10 May 2022 20:42:03 -0400
If join-thread is called after a previous call to join-thread hits the timeout, a "mutex already locked by thread" error is thrown.

Working example:

(use-modules (ice-9 threads))
(define t (call-with-new-thread
           (lambda _
             (let r ()
               (sleep 1)
               (r)))))
(join-thread t (current-time))
(join-thread t (current-time)) ;; exception here

my guile version: latest git. cc455976813ab94de121395982435430874cbf58
my OS: guix on amd64




Information forwarded to bug-guile <at> gnu.org:
bug#55356; Package guile. (Sun, 16 Oct 2022 15:37:02 GMT) Full text and rfc822 format available.

Message #8 received at 55356 <at> debbugs.gnu.org (full text, mbox):

From: Olivier Dion <olivier.dion <at> polymtl.ca>
To: 55356 <at> debbugs.gnu.org
Cc: Olivier Dion <olivier-dion <at> proton.me>
Subject: [PATCH] Always release thread data mutex in join-thread.
Date: Sun, 16 Oct 2022 11:36:02 -0400
From: Olivier Dion <olivier-dion <at> proton.me>

Currently the mutex is only unlocked when results are available.
However, it is not unlocked when we get a timeout from the condition
variable.

* module/ice-9/threads.scm (join-thread): Use with-mutex to ensure that
the thread data mutex is always unlocked.
---
 module/ice-9/threads.scm | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/module/ice-9/threads.scm b/module/ice-9/threads.scm
index c42bd266f..8993596e4 100644
--- a/module/ice-9/threads.scm
+++ b/module/ice-9/threads.scm
@@ -186,18 +186,17 @@ terminates, unless the target @var{thread} has already terminated."
   (match (thread-join-data thread)
     (#f (error "foreign thread cannot be joined" thread))
     ((cv . mutex)
-     (lock-mutex mutex)
-     (let lp ()
-       (cond
-        ((%thread-results cv)
-         => (lambda (results)
-              (unlock-mutex mutex)
-              (apply values results)))
-        ((if timeout
-             (wait-condition-variable cv mutex timeout)
-             (wait-condition-variable cv mutex))
-         (lp))
-        (else timeoutval))))))
+     (with-mutex mutex
+       (let lp ()
+         (cond
+          ((%thread-results cv)
+           => (lambda (results)
+                (apply values results)))
+          ((if timeout
+               (wait-condition-variable cv mutex timeout)
+               (wait-condition-variable cv mutex))
+           (lp))
+          (else timeoutval)))))))
 
 (define* (try-mutex mutex)
   "Try to lock @var{mutex}.  If the mutex is already locked, return
-- 
2.37.3





Reply sent to Ludovic Courtès <ludo <at> gnu.org>:
You have taken responsibility. (Thu, 25 Jan 2024 22:15:02 GMT) Full text and rfc822 format available.

Notification sent to angry rectangle <angryrectangle <at> cock.li>:
bug acknowledged by developer. (Thu, 25 Jan 2024 22:15:02 GMT) Full text and rfc822 format available.

Message #13 received at 55356-done <at> debbugs.gnu.org (full text, mbox):

From: Ludovic Courtès <ludo <at> gnu.org>
To: Olivier Dion <olivier.dion <at> polymtl.ca>
Cc: 55356-done <at> debbugs.gnu.org, 61058-done <at> debbugs.gnu.org
Subject: Re: bug#61058: [PATCH v2] Fix asymetric mutex locking when joining
 thread.
Date: Thu, 25 Jan 2024 23:14:26 +0100
Hello,

Olivier Dion <olivier.dion <at> polymtl.ca> skribis:

> If `join-thread' timeout, the thread mutex is not unlocked, resulting in
> deadlock to the next call to it or deadlock of the thread itself when it
> terminates.
>
> Thus, always unlock the mutex.
>
> Fix: #55356
>
> * module/ice-9/threads.scm (join-thread): Always unlock thread mutex.
> * test-suite/tests/threads.test (join-thread): New test to ensure the
> mutex is released

Pushed as 455ee49f5573baa1bc5237a8d49083ce588a13ee with a ‘NEWS’ entry
and an additional comment in the test.

Thanks!

Ludo’.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Fri, 23 Feb 2024 12:24:12 GMT) Full text and rfc822 format available.

This bug report was last modified 84 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.