[plt-scheme] thread shutting down its own custodian

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Sat May 22 10:11:35 EDT 2010

Anyone who knows custodians and threads well have a minute to 
sanity-check this for me?

In a conventional TCP server that has one request per connection and 
creates a thread for each request... I am currently establishing a new 
current-custodian for the block of code that calls "tcp-accept", and 
having the thread created within that block call 
"custodian-shutdown-all" on that custodian (as the thread's final act).  
A simplified example:

    (let loop ()
      (let ((request-cust (make-custodian my-listener-cust)))
        (parameterize ((current-custodian request-cust))
          (let-values (((in out) (tcp-accept my-listener)))
            (thread (lambda ()
                        (lambda () #f)
                        (lambda ()
                          (my-handle-request in out))
                        (lambda ()
                          (custodian-shutdown-all request-cust)))))))

This seems to work, but I'm wondering whether there's a race condition 
(e.g., custodian shutdown code is executed in the thread itself, and 
that code can kill its own evaluation prematurely), and whether there's 
a better way to do it.


Posted on the users mailing list.