[racket] Sandboxed evaluation with 2htdp/image requires

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Feb 5 12:35:47 EST 2012

The `make-module-evaluator' function should attach an instance of
`racket/gui/base' in its module's namespace to the newly created
sandboxed namespace, but there is a problem...

The decision of whether `racket/gui/base' is available is made through
`gui-available?'. That the check happens once at the top level of
`racket/sandbox', which unfortunately makes it sensitive to the order
of modules in the program that creates sandboxes.

As a result,

 #lang racket
 (require racket/sandbox
          racket/gui/base)

 (call-with-trusted-sandbox-configuration 
  (lambda ()
    (make-module-evaluator '(module m racket/gui))))

works, while

 #lang racket
 (require racket/gui/base
          racket/sandbox)

 (call-with-trusted-sandbox-configuration 
  (lambda ()
    (make-module-evaluator '(module m racket/gui))))

fails.

No, I didn't get the examples backwards. The one that lists
`racket/gui/base' first fails, because the order of instantiation for
required modules is not specified in that example, and it happens to be
reverse of the textual order in this case. (In other words, it's bad
when a module depends on the instantiation order of a module that it
doesn't import directly or indirectly.)

One workaround is to start your script with something like

 racket -l racket/gui/base -t prog

instead of

 racket -t prog

which explains how `racket/sandbox' got into its current state: at the
time that `racket/sandbox' was written, you had to select GUInees
up-front with `mred' or `mred-text', and so the early test made sense.

We should look into fixing the sandbox so that the choice of GUI or not
is made later, but I hope that this information points you in the
direction of a workaround, for now.

(It would be even better to fix `racket/gui/base' so that it can be
instantiated multiple times, but I think that improvement is much
further away.)

At Sun, 5 Feb 2012 11:02:48 -0500, Nadeem Abdul Hamid wrote:
> I'm having a problem with the error "cannot instantiate
> `racket/gui/base' a second time in the same process" in a script that
> I have to perform automated testing of student BSL programs. My script
> requires 2htdp/image for its own purposes and uses
> make-module-evaluator to create an evaluator for a student program.
> The problem is that if the student program also has a (require
> 2htdp/image), it attempts to instantiate racket/gui/base which is
> problematic (http://docs.racket-lang.org/gui/Startup_Actions.html ).
> Any suggestions for dealing with this?
> 
> Thanks,
> nadeem
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

Posted on the users mailing list.