[plt-scheme] sync deadlock weirdness

From: Dimitris Vyzovitis (vyzo at media.mit.edu)
Date: Mon Feb 5 04:31:37 EST 2007

It is easier to see if you add a print after the unless in foo:

(define (foo me ch)
  (let* ((pinfo (channel-get ch))
         (peer (car pinfo))
         (peer-ch (cdr pinfo)))
    (sleep 1)
    (let lp ((n 0))
      (printf "~a~n" n)
      (unless (> n 100000)
        (sync (handle-evt ch (lambda (v)
                               (printf "~a: received ~a~n" me v)
                               (unless (eq? v 'die) (lp n))))
              (handle-evt (choice-evt (thread-dead-evt peer)
                                      (channel-put-evt peer-ch (random)))
                          (lambda (evt) (lp (add1 n))))
              (handle-evt (wrap-evt (thread-dead-evt peer)
                                    (lambda (evt) (void)))
                          (lambda (evt) (printf "~a: my peer died~n" me))))))))

(define (make-foos foo)
  (let* ((ch1 (make-channel))
         (ch2 (make-channel))
         (thr1 (thread (lambda () (foo 'jekyll ch1))))
         (thr2 (thread (lambda () (foo 'hyde ch2)))))
    (channel-put ch1 (cons thr2 ch2))
    (channel-put ch2 (cons thr1 ch1))
    (values (cons thr1 ch1) (cons thr2 ch2))))

;; deadlocks!
(define-values (foo1 foo2) (make-foos foo))
(sync (thread-dead-evt (car foo1)) (thread-dead-evt (car foo2)))

-- vyzo



Posted on the users mailing list.