Skip to content

Instantly share code, notes, and snippets.

@tonyg
Created January 16, 2013 17:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonyg/4548874 to your computer and use it in GitHub Desktop.
Save tonyg/4548874 to your computer and use it in GitHub Desktop.
Racket custodians and namespaces for rebootable racket programs

Demonstrates the use of Racket's custodians and namespaces to create enough isolation to "reboot" programs without restarting the whole of Racket, including possibly recompiling and reloading code.

To experiment with this:

$ racket ~/src/racket-experiments$ racket
Welcome to Racket v5.3.1.4.
-> (require "reloader.rkt")
-> (load-and-foo)
Starting worker...
test-mod: "foo"
other-mod: "foo"
other-mod: "foo"
other-mod: "foo"
other-mod: "foo"
Terminating worker...
-> 

Subsequent runs of (load-and-foo) will reload the code from scratch. If you've edited one of test-mod.rkt or other-mod.rkt in the meantime, the changes will show up.

Note that other-mod.rkt runs a loop "forever", but that reloader.rkt terminates all threads in the nested custodian.

#lang racket/base
(require "test-mod.rkt")
(let loop ()
(printf "other-mod: ~v\n" (foo))
(sleep 1)
(loop))
#lang racket/base
(define (load-and-foo)
(define sub-custodian (make-custodian))
(printf "Starting worker...\n")
(parameterize ((current-custodian sub-custodian))
(thread (lambda ()
(parameterize ((current-namespace (make-base-namespace)))
(dynamic-require "other-mod.rkt" #f)))))
(sleep 3.5)
(printf "Terminating worker...\n")
(custodian-shutdown-all sub-custodian))
(provide load-and-foo)
#lang racket/base
(provide foo)
(define (foo) "foo")
(printf "test-mod: ~v\n" (foo))
@Metaxal
Copy link

Metaxal commented May 27, 2013

Hi,

This is wonderful.
I have one problem when test-mod.rkt requires racket/gui instead of racket/base:

Terminating worker...
Starting worker...
cannot instantiate `racket/gui/base' a second time in the same process

By any chance, would you know how to solve this issue?

Laurent

@Metaxal
Copy link

Metaxal commented May 27, 2013

I may have found a workaround, but I'm not really sure it works as it should:

(require racket/gui/base)
(...)

(define-namespace-anchor this-namespace-anchor)
(define this-namespace (namespace-anchor->namespace this-namespace-anchor))
(...)
      (parameterize ([current-custodian sub-custodian]
                     [current-namespace (make-base-namespace)])
        (namespace-attach-module this-namespace 'racket/gui/base)
        (dynamic-require (...))
        (...)

This way, racket/gui/base is instanciated only once, and reused for every dynamic-require,
but then maybe some other things are not properly shut down with the custodian?

Laurent

@zenspider
Copy link

Following up... the "correct" way instead of a named anchor is to use make-gui-namespace instead of make-namespace... I'm gonna sprinkle this around since there are so few hits when searching for this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment