[vbox-dev] Web server and virtualbox api

Joao Milasch jfilho at gmail.com
Fri Jul 20 00:55:26 GMT 2012


Hi, I have a simple web server that basically returns a screenshot or a
particular running vm.

It's an application running on top of Flask, a python MVC framework, and on
an nginx/uwsgi web server:

*__init__.py*

from flask import Flask, g, make_responseimport sys, ospython_path =
'/usr/lib/python2.7/site-packages'if os.path.exists(python_path):
    sys.path.append(python_path)import vboxapi
app = Flask(__name__)app.config.update(DEBUG=True)
mgr = vboxapi.VirtualBoxManager(None, None)
class VMLock(object):
    def __init__(self, vm, locktype=mgr.constants.LockType_Shared):
        self.session = mgr.mgr.getSessionObject(mgr.vbox)
        self.locktype = locktype
        self.vm = vm

    def __enter__(self):
        self.vm.lockMachine(self.session, self.locktype)
        if self.session.type == mgr.constants.SessionType_Null:
            raise Exception('session type is null')
        return self.session.console

    def __exit__(self, type, value, traceback):
        self.session.unlockMachine()

@app.route('/screenshot')def screenshot():
    vm = mgr.vbox.findMachine("My VM")
    with VMLock(vm) as console:
        display = console.display
        current_x, current_y, bpp = display.getScreenResolution(0)
        response = make_response(display.takeScreenShotPNGToArray(0,
current_x, current_y))
        response.headers['Content-Type'] = 'image/png'
        return response

if __name__ == '__main__':
    try:
        app.run('0.0.0.0', debug=True)
    except:
        app.run('0.0.0.0', port=4999, debug=True)


It works just fine when I run it standalone. The problem I'm having
starts when I decide to host this micro-application on an nginx
webserver. Again, all works fine until I enable more than one worker
to listen to more request at once. Then vbox api starts throwing weird
exceptions:

Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1360, in
full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1358, in
full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1344, in
dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/milasch/dev/nginx_test/__init__.py", line 36, in screenshot
    vm = mgr.vbox.findMachine("My VM")
  File "<XPCOMObject method 'findMachine'>", line 3, in
findMachinexpcom.Exception: 0x80470007 (Stream operation would block
(NS_BASE_STREAM_WOULD_BLOCK))


And these are kind of random, but happen 60-70% of the requests.

Would this be a shared lock issue when I'm locking the VM?

Please refer to: http://pastebin.com/fbWXzMMp for full uwsgi output.

Any advice is appreciated.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.virtualbox.org/pipermail/vbox-dev/attachments/20120719/113ed599/attachment.html>


More information about the vbox-dev mailing list