#lang scheme/base
(require scheme/async-channel)

(define main-custodian (current-custodian))
(current-custodian (make-custodian))

(define (report-items msg)
  (let ([managed-items (custodian-managed-list (current-custodian) main-custodian)])
    (printf "~a: ~s [~s]~n" msg managed-items (weak-box-value wb))
    (when (pair? managed-items)
      (print-thread-context (list-ref managed-items 0)))))

(define (print-thread-context thd)
  (printf "~s~n" (continuation-mark-set->context (continuation-marks thd))))

(define wb (make-weak-box #f))

(report-items 'start)

(define chn (make-async-channel))
(set! wb (make-weak-box chn))

(report-items 'alloc)

(set! chn #f)

(report-items 'clear)

(collect-garbage)

(report-items 'gc)