Bugzilla will be upgraded to version 5.0. The upgrade date is tentatively scheduled for 2 December 2018, pending final testing and feedback.
Bug 149056 - stack overflow while using mutexes in recursion
stack overflow while using mutexes in recursion
Product: Fedora
Classification: Fedora
Component: guile (Show other bugs)
All Linux
medium Severity low
: ---
: ---
Assigned To: Phil Knirsch
Depends On:
  Show dependency treegraph
Reported: 2005-02-18 05:54 EST by Paul Osmialowski
Modified: 2015-03-04 20:14 EST (History)
1 user (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2005-09-02 05:56:07 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description Paul Osmialowski 2005-02-18 05:54:36 EST
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5) Gecko/20041111 Firefox/1.0

Description of problem:
I've started such code:
(define finite-loop (lambda (n m mutex)
    (lock-mutex mutex)
    (cond ((not (= 0 n)) (yield) (display m) (display ", ") (display n) (newline) (unlock-mutex mutex) (finite-loop (- n 1) m mutex)))
    (unlock-mutex mutex)

(define thread-error-handler (lambda (tag . args)
    (display tag) (display " ") (display args) (newline)

(define thread1 
	    (mutex (make-mutex))
	    (thread-function (lambda () (finite-loop 1000000 1 mutex)))
	(cons (call-with-new-thread thread-function thread-error-handler) mutex)
(display thread1)
(finite-loop 100 2 (cdr thread1))
(join-thread (car thread1))

After a while stack overflow inside of the finite-loop function occurs. When I remove mutex locking/unlocking functions program is working properly.

Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:
1. Create file threads-with-mutex.scm
2. Paste program shown above into that file
3. run: guile -s threads-with-mutex.scm

Actual Results:  After a while you may see something like that:
1, 9012
1, 9011
1, 9010
stack-overflow (#f Stack overflow #f #f)

Expected Results:  Further program execution

Additional info:

The workaround is to use 'do' iteration instead of recursion:
(define finite-loop (lambda (n m mutex)
    (do ((i n (- i 1))) ((= 0 i))
        (lock-mutex mutex)
        (display m) (display " ") (display i) (newline)
        (unlock-mutex mutex)
However, iteration is not always a solution! That makes mutexes dangerous to use...
Comment 1 Paul Osmialowski 2005-02-18 06:14:01 EST
I've just found another workaround. The problem is that mutex should be unlocked
insite the cond expression:
(define finite-loop (lambda (n m mutex)
    (lock-mutex mutex)
        ((not (= 0 n))
            (display m) (display ", ") (display n) (newline)
            (unlock-mutex mutex)
            (finite-loop (- n 1) m mutex)
        (else (unlock-mutex mutex))
But let someone tell me which scheme rule says that previous code was wrong?!
Comment 2 Phil Knirsch 2005-09-02 05:56:07 EDT
Maybe you get one more mutex for each iteration run and they are possibly put on
the internal guile stack and therefore cause in one case an overflow but not in
the other.

But this is just a wield speculative guess.

I'm closing this bug as it's really most likely something very specific and
internal of guile and should rather be handled and fixed properly by the
upstream folks.

Read ya, Phil

Note You need to log in before you can comment on or make changes to this bug.