mutex-lock! in finalizer causes infinite gc loop pasted by megane on Fri Feb 15 16:04:32 2019

(cond-expand
 (chicken-5 (import (chicken base))
            (import (chicken gc))
            (import (srfi 18)))
 (else (use srfi-18)))

(define *mtx* (make-mutex))

(define threads (list (make-thread
                       (lambda ()
                         (mutex-lock! *mtx*)

                         ;; I actually wanted to do this:
                         ;; (set-finalizer! (list 'foo) (lambda (_)
                         ;;                               (when (mutex-lock! *mtx* 0)
                         ;;                                 ;; do stuff
                         ;;                                 (mutex-unlock! *mtx*))))

                         ;; But this causes the same error
                         (set-finalizer! (list 'foo) (lambda (_) (mutex-lock! *mtx*)))

                         (gc #t)
                         (mutex-unlock! *mtx*) ; this can be removed
                         ))
                      (make-thread (lambda () (gc #t)))))
(for-each thread-start! threads)
(for-each thread-join! threads)
(print "done")

;; $ csc gcloop.scm  && ./gcloop -:g 
;; [GC] 1 finalizer(s) pending (1 live)
;; [GC] queueing 1 finalizer(s)
;; [GC] level  1	gcs(minor)  3	gcs(major)  1
;; [GC] stack	0x00007ffe4e9a9470	0x000000004ea9f920	0x00007ffe4eaa9470
;; [GC]  from	0x00007f250b100010	0x00007f250b140330	0x00007f250b180010	0x0000000000040320
;; [GC]    to	0x00007f250b181010	0x00007f250b181010	0x00007f250b201010 
;; [GC] 0 locatives (from 32)
;; [GC] 1 finalized item(s) still pending
;; [GC] level  1	gcs(minor)  0	gcs(major)  2
;; [GC] stack	0x00007ffe4e9a9470	0x000000004eaa7120	0x00007ffe4eaa9470
;; [GC]  from	0x00007f250b181010	0x00007f250b1c16f0	0x00007f250b201010	0x00000000000406e0
;; [GC]    to	0x00007f250b100010	0x00007f250b100010	0x00007f250b180010 
;; [GC] 0 locatives (from 32)
;; [GC] 1 finalized item(s) still pending
;; [GC] level  1	gcs(minor)  0	gcs(major)  3
;; [GC] stack	0x00007ffe4e9a9470	0x000000004eaa9140	0x00007ffe4eaa9470
;; [GC]  from	0x00007f250b100010	0x00007f250b1406f0	0x00007f250b180010	0x00000000000406e0
;; [GC]    to	0x00007f250b181010	0x00007f250b181010	0x00007f250b201010 
;; [GC] 0 locatives (from 32)
;; [GC] 1 finalized item(s) still pending
;; [GC] level  1	gcs(minor)  0	gcs(major)  4
;; [GC] stack	0x00007ffe4e9a9470	0x000000004eaa9140	0x00007ffe4eaa9470
;; [GC]  from	0x00007f250b181010	0x00007f250b1c16f0	0x00007f250b201010	0x00000000000406e0
;; [GC]    to	0x00007f250b100010	0x00007f250b100010	0x00007f250b180010 
;; [GC] 0 locatives (from 32)
;; [GC] 1 finalized item(s) still pending
;; [GC] level  1	gcs(minor)  0	gcs(major)  5
;; [GC] stack	0x00007ffe4e9a9470	0x000000004eaa9140	0x00007ffe4eaa9470
;; [GC]  from	0x00007f250b100010	0x00007f250b1406f0	0x00007f250b180010	0x00000000000406e0
;; [GC]    to	0x00007f250b181010	0x00007f250b181010	0x00007f250b201010 
;; [GC] 0 locatives (from 32)
;; [GC] 1 finalized item(s) still pending
;; [GC] level  1	gcs(minor)  0	gcs(major)  6
;; [GC] stack	0x00007ffe4e9a9470	0x000000004eaa9140	0x00007ffe4eaa9470
;; [GC]  from	0x00007f250b181010	0x00007f250b1c16f0	0x00007f250b201010	0x00000000000406e0
;; ...

it's the second gc that's troubling... added by C-Keen on Fri Feb 15 16:48:15 2019

./gcloop -:r
t.scm:4: chicken.load#load-extension
t.scm:7: srfi-18#make-mutex
t.scm:9: srfi-18#make-thread
t.scm:25: srfi-18#make-thread
t.scm:9: scheme#list
t.scm:26: scheme#for-each
t.scm:27: scheme#for-each
t.scm:11: srfi-18#mutex-lock!
t.scm:20: scheme#list
t.scm:20: chicken.gc#set-finalizer!
t.scm:22: chicken.gc#gc
t.scm:20: srfi-18#mutex-lock!
t.scm:25: chicken.gc#gc