gc goes crazy with faulty finalizers
;; It seems like the gc stops working correctly after an exception
;; from a faulty finalizer gets caught (and not crash the whole
;; program).
(define n 2000000000)
(define vector-size 100)
(define (make-objects n gc-msg)
(let loop [(i 0)]
(let [(o (make-vector vector-size))]
(print "making " i)
(set-finalizer! o (lambda (ob) (print gc-msg " " i)))
(if (< i n)
(loop (+ 1 i))))))
(set-finalizer! (make-vector 100) (lambda (ob) (+ i 'a)))
(handle-exceptions
exn
(begin
(print "Caught an exception" exn))
(make-objects n "gc'ing"))
(print "done")
;; Result when compiled: 3 cases
;; Case 1: out of memory, tweak n, vector-size to avoid this
;; ...
;; [panic] out of memory - cannot allocate heap segment - execution terminated
;; Case 2: 100% cpu with no apparent progress
;; Output with -:g
;; ...
;; [GC] 2 finalized item(s) still pending
;; [GC] 2680 finalizer value(s) marked
;; [GC] level 1 gcs(minor) 379 gcs(major) 22
;; [GC] stack 0xbfef7880 0xbff36a00 0xbff37880
;; [GC] from 0x773d9008 0xb731f744 0xb73d9000 0x3ff4673c
;; [GC] to 0x09123008 0x09123008 0x49123000
;; [GC] 0 locatives (from 32)
;; 681
;; [GC] 2 finalized item(s) still pending
;; [GC] 2681 finalizer value(s) marked
;; [GC] level 1 gcs(minor) 0 gcs(major) 23
;; [GC] stack 0xbfef7880 0xbff36a00 0xbff37880
;; [GC] from 0x09123008 0x490cb1d8 0x49123000 0x3ffa81d0
;; [GC] to 0x773d9008 0x773d9008 0xb73d9000
;; [GC] 0 locatives (from 32)
;; <- stuck here with 100% cpu
;; Case 3: 100% at the end
;; making 1
;; making 2
;; ...
;; ...
;; gc'ing 103
;; gc'ing 102
;; gc'ing 101
;; Caught an exception#<condition: (exn type)>
;; done
;; <- stuck here with 100% cpu
The attached patch adds catching any errors during finalizer execution, but there still seems to be a problem - running the test sometimes crashes (seems to depend on memory layout or something similar).