unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#61058: [PATCH] Fix asymetric mutex locking when joining thread.
@ 2023-01-25 15:24 Olivier Dion via Bug reports for GUILE, GNU's Ubiquitous Extension Language
  2024-01-25 21:45 ` bug#61058: [PATCH v2] " Olivier Dion
  0 siblings, 1 reply; 3+ messages in thread
From: Olivier Dion via Bug reports for GUILE, GNU's Ubiquitous Extension Language @ 2023-01-25 15:24 UTC (permalink / raw)
  To: 61058; +Cc: Olivier Dion

From: Olivier Dion <olivier-dion@proton.me>

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.
---
 module/ice-9/threads.scm | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/module/ice-9/threads.scm b/module/ice-9/threads.scm
index c42bd266f..962caee70 100644
--- a/module/ice-9/threads.scm
+++ b/module/ice-9/threads.scm
@@ -197,7 +197,9 @@ terminates, unless the target @var{thread} has already terminated."
              (wait-condition-variable cv mutex timeout)
              (wait-condition-variable cv mutex))
          (lp))
-        (else timeoutval))))))
+        (else
+         (unlock-mutex mutex)
+         timeoutval))))))
 
 (define* (try-mutex mutex)
   "Try to lock @var{mutex}.  If the mutex is already locked, return
-- 
2.39.1






^ permalink raw reply related	[flat|nested] 3+ messages in thread

* bug#61058: [PATCH v2] Fix asymetric mutex locking when joining thread.
  2023-01-25 15:24 bug#61058: [PATCH] Fix asymetric mutex locking when joining thread Olivier Dion via Bug reports for GUILE, GNU's Ubiquitous Extension Language
@ 2024-01-25 21:45 ` Olivier Dion
  2024-01-25 22:14   ` Ludovic Courtès
  0 siblings, 1 reply; 3+ messages in thread
From: Olivier Dion @ 2024-01-25 21:45 UTC (permalink / raw)
  To: 61058; +Cc: Olivier Dion

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
---
 module/ice-9/threads.scm      |  4 +++-
 test-suite/tests/threads.test | 13 ++++++++++++-
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/module/ice-9/threads.scm b/module/ice-9/threads.scm
index 048d8b085..a1e43b9fa 100644
--- a/module/ice-9/threads.scm
+++ b/module/ice-9/threads.scm
@@ -204,7 +204,9 @@ terminates, unless the target @var{thread} has already terminated."
              (wait-condition-variable cv mutex timeout)
              (wait-condition-variable cv mutex))
          (lp))
-        (else timeoutval))))))
+        (else
+         (unlock-mutex mutex)
+         timeoutval))))))
 
 (define* (try-mutex mutex)
   "Try to lock @var{mutex}.  If the mutex is already locked, return
diff --git a/test-suite/tests/threads.test b/test-suite/tests/threads.test
index efdf36db2..af8c8c75b 100644
--- a/test-suite/tests/threads.test
+++ b/test-suite/tests/threads.test
@@ -332,7 +332,18 @@
 	      (sleep 2)
 	      (system-async-mark aproc)
 	      (join-thread other-thread)))
-	  #t))
+	  #t)
+
+        (pass-if "do not throw exception if trying to join after timeout"
+          (let ((other-thread (begin-thread (pause))))
+            (dynamic-wind
+              (const #f)
+              (lambda ()
+                (join-thread other-thread 1)
+                (join-thread other-thread 1)
+                #t)
+              (lambda ()
+                (cancel-thread other-thread))))))
 
       ;;
       ;; thread cancellation
-- 
2.41.0






^ permalink raw reply related	[flat|nested] 3+ messages in thread

* bug#61058: [PATCH v2] Fix asymetric mutex locking when joining thread.
  2024-01-25 21:45 ` bug#61058: [PATCH v2] " Olivier Dion
@ 2024-01-25 22:14   ` Ludovic Courtès
  0 siblings, 0 replies; 3+ messages in thread
From: Ludovic Courtès @ 2024-01-25 22:14 UTC (permalink / raw)
  To: Olivier Dion; +Cc: 55356-done, 61058-done

Hello,

Olivier Dion <olivier.dion@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’.





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-01-25 22:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-25 15:24 bug#61058: [PATCH] Fix asymetric mutex locking when joining thread Olivier Dion via Bug reports for GUILE, GNU's Ubiquitous Extension Language
2024-01-25 21:45 ` bug#61058: [PATCH v2] " Olivier Dion
2024-01-25 22:14   ` Ludovic Courtès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).