From: d4ryus via "Bug reports for GUILE, GNU's Ubiquitous Extension Language" <bug-guile@gnu.org>
To: 40681@debbugs.gnu.org
Subject: bug#40681: srfi-18 mutex-unlock! breaks mutex on timeout
Date: Fri, 17 Apr 2020 16:21:52 +0200 [thread overview]
Message-ID: <20200417142152.GA2619432@gandalf.d4ryus.net> (raw)
[-- Attachment #1: Type: text/plain, Size: 2507 bytes --]
hi,
when mutex-unlock! is called (master: module/srfi/srfi-18.scm +306) with
a condition and a timeout, it first resets the owner and then waits for
the condition. If the timeout is reached
(threads:wait-condition-variable returns #f) the cond's else condition
returns #f. Now the owner is #f, which prevents one from unlocking the
mutex, due to the early return if owner is #f.
Reproducer (also attached to the email):
(use-modules (srfi srfi-18))
(let ((condition (make-condition-variable))
(mutex (make-mutex))
(timeout (seconds->time
(+ (time->seconds (current-time)) 1)))) ;; 1 second timeout
(mutex-lock! mutex)
(when (mutex-unlock! mutex condition timeout)
;; Mutex is unlocked, everything is fine. This is not reached
;; though, as nobody signals the condition
(error "Not reached"))
;; Timeout was reached
;; Now the mutex has no owner but is stilled locked
;; Calling mutex-unlock! has no effect
(mutex-unlock! mutex)
;; And a call to lock will now block forever
(mutex-lock! mutex))
This affects guile 2.2 and master.
A fix would be to only reset the owner when the mutex is truly unlocked,
e.g. something along the lines of (also attached to the email):
diff --git a/module/srfi/srfi-18.scm b/module/srfi/srfi-18.scm
index 79aedb8d1..7da6254d4 100644
--- a/module/srfi/srfi-18.scm
+++ b/module/srfi/srfi-18.scm
@@ -307,17 +307,19 @@ object (absolute point in time), or #f."
(timeout %unlock-sentinel))
(let ((timeout (timeout->absolute-time timeout)))
(when (mutex-owner mutex)
- (set-mutex-owner! mutex #f)
(cond
((eq? cond-var %unlock-sentinel)
+ (set-mutex-owner! mutex #f)
(threads:unlock-mutex (mutex-prim mutex)))
((eq? timeout %unlock-sentinel)
(threads:wait-condition-variable (condition-variable-prim cond-var)
(mutex-prim mutex))
+ (set-mutex-owner! mutex #f)
(threads:unlock-mutex (mutex-prim mutex)))
((threads:wait-condition-variable (condition-variable-prim cond-var)
(mutex-prim mutex)
timeout)
+ (set-mutex-owner! mutex #f)
(threads:unlock-mutex (mutex-prim mutex)))
(else #f)))))
If you need any additional information let me know.
Thanks for your great work!
- d4ryus
[-- Attachment #2: reproducer.scm --]
[-- Type: application/vnd.lotus-screencam, Size: 803 bytes --]
[-- Attachment #3: potential-fix.patch --]
[-- Type: text/plain, Size: 1105 bytes --]
diff --git a/module/srfi/srfi-18.scm b/module/srfi/srfi-18.scm
index 79aedb8d1..7da6254d4 100644
--- a/module/srfi/srfi-18.scm
+++ b/module/srfi/srfi-18.scm
@@ -307,17 +307,19 @@ object (absolute point in time), or #f."
(timeout %unlock-sentinel))
(let ((timeout (timeout->absolute-time timeout)))
(when (mutex-owner mutex)
- (set-mutex-owner! mutex #f)
(cond
((eq? cond-var %unlock-sentinel)
+ (set-mutex-owner! mutex #f)
(threads:unlock-mutex (mutex-prim mutex)))
((eq? timeout %unlock-sentinel)
(threads:wait-condition-variable (condition-variable-prim cond-var)
(mutex-prim mutex))
+ (set-mutex-owner! mutex #f)
(threads:unlock-mutex (mutex-prim mutex)))
((threads:wait-condition-variable (condition-variable-prim cond-var)
(mutex-prim mutex)
timeout)
+ (set-mutex-owner! mutex #f)
(threads:unlock-mutex (mutex-prim mutex)))
(else #f)))))
reply other threads:[~2020-04-17 14:21 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200417142152.GA2619432@gandalf.d4ryus.net \
--to=bug-guile@gnu.org \
--cc=40681@debbugs.gnu.org \
--cc=d4ryus@teknik.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).