2

I want to use pysandbox to allow users to run code on my server. I'm using Django's internal server (manage.py runserver) with this request handler:

def try_sandbox(request):
    from sandbox import Sandbox
    def func(a, b):
        return a + b
    sandbox = Sandbox()
    result = sandbox.call(func, 1, 2)
    return HttpResponse(result)

When accessing the page I get a ValueError:

Request Method: GET
Exception Type: ValueError
Exception Value:   signal only works in main thread
Exception Location: /Library/Python/2.7/site-packages/sandbox/timeout.py in limitedTime, line 45
Python Executable:  /usr/bin/python

Traceback:

Django Version: 1.3.1
Python Version: 2.7.1
File "[...]views.py" in try_sandbox
  77.     result = sandbox.call(func, 1, 2)
File "/Library/Python/2.7/site-packages/sandbox/sandbox_class.py" in call
  44.         return self._call(func, args, kw)
File "/Library/Python/2.7/site-packages/sandbox/sandbox_class.py" in _call
  31.                 return limitedTime(timeout, func, *args, **kw)
File "/Library/Python/2.7/site-packages/sandbox/timeout.py" in limitedTime
  45.             old_alarm = signal(SIGALRM, signalHandler)

Is it possible to use pysandbox in this environment?

(I'm think I'm using pysandbox 1.1 - that's what the version.py in the download says. The download folder says 1.0.1. I'm running Mac OS 10.7.2.)

fgm2r
  • 4,490
  • 1
  • 17
  • 14
  • 1
    Please describe your environment in more detail. What web server are you using? It works without any exceptions with django internal server (manage.py runserver) on Python 2.6.6 x64, Django 1.3.0 and pysandbox 1.0.3. – utapyngo Dec 09 '11 at 08:25
  • @utapyngo I added some info. I'll try a non-master pysandbox branch later today. – fgm2r Dec 09 '11 at 12:35
  • At least you should paste the full detailed error message... – Felix Yan Dec 09 '11 at 12:40
  • @FelixYan Sorry. I accidentally removed it when I updated the question. If you also need the Python path, Installed Applications and Installed Middleware: http://pasteit.com/16244 – fgm2r Dec 09 '11 at 14:36
  • @utapyngo I tried to install pysandbox 1.0.3, but I probably did it wrong. I now get a "_sandbox version 1 is not supported" error, the version.py now says "1.0.3" (which would normally be an earlier version). – fgm2r Dec 09 '11 at 17:58

1 Answers1

2

I've not encountered this error myself so I may be absolutely wrong - but it seems like if pysandbox doesn't like running on non-main threads then you should ensure that it is running on the main thread. If you look at the django runserver documentation, it seems that a --nothreading option has been introduced in the development version of django. If this is what you're running, then it may be worth checking out. For example:

manage.py runserver --nothreading

Alternatively, if you still can't get it working (and don't mind that this idea is probably very inefficient) you could spawn off a separate process using subprocess.check_output or similar, and run your sandboxed code from there. This really isn't a brilliant idea for various reasons, but would definitely cause the sandbox code to be run from a main thread.

obmarg
  • 9,369
  • 36
  • 59
  • I've installed the development version and used --nothreading but it doesn't make a difference. I might use a separate process if necessary. (I'd either have to run a lot of small chunks of code and create many processes, or try and make the entire system run in the sandbox.) – fgm2r Dec 14 '11 at 09:22