Skip to content

Commit

Permalink
sandbox timeouts and flagging
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Nov 11, 2012
1 parent cac9754 commit a2409bf
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
2 changes: 0 additions & 2 deletions browser/ui/playerRepl.js
@@ -1,5 +1,3 @@
var wrap = require("../../wrap")

module.exports = PlayerRepl

function PlayerRepl(row) {
Expand Down
15 changes: 10 additions & 5 deletions init.js
@@ -1,14 +1,14 @@
var model = require('./model')
var api = require('./logic')
var wrap = require("./wrap")
var vm = require('vm')

var net = require('net')

setInterval(function () {
//don't remove this, this is how the server knows that
//this process is still alive (not inifinte looping)

process.stdout.write('.') //heartbeat
process.stdout.write('[]\n') //heartbeat
}, 500)

var stream = net.connect(66666)
Expand All @@ -23,16 +23,21 @@ model.on('create', function (row) {
var _fn
row.on('change', function (ch) {
var fn
if(ch.source || ch.cast) {
if(ch.source || ch.cast && row.state.run !== false) {
console.log(JSON.stringify([ 'start', row.id ]))
try {
fn = wrap(ch.source || ch.cast)(row.api)
fn = vm.runInNewContext(ch.source || ch.cast, {
self : row.api
})
} catch (err) {
row.set('message', {
text: err.toString(),
stroke: 'red', fill: 'black'
})
console.log(row.id, err)
console.log(JSON.stringify([ 'error', row.id, String(err) ]))
return
}
console.log(JSON.stringify([ 'end', row.id ]))
}
})

Expand Down
5 changes: 4 additions & 1 deletion logic.js
@@ -1,5 +1,4 @@
var model = require('./model')
var wrap = require('./wrap')

module.exports = api

Expand Down Expand Up @@ -65,14 +64,18 @@ function api (row) {

function safe (fun) {
return function () {
console.log(JSON.stringify([ 'start', row.id ]))
try {
return fun.apply(this, arguments)
} catch (err) {
console.log(JSON.stringify([ 'error', row.id ]))
//TODO: present this back to the user somehow...
//or, make the entity die.
row.set('message', {text: err.toString().toUpperCase(), stroke: 'red', fill: 'black'})
console.error('user-error', err.toString().toUpperCase())
return
}
console.log(JSON.stringify([ 'end', row.id ]))
}
}

Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -37,7 +37,9 @@
"point-generator": "~0.1.0",
"deck": "0.0.4",
"idle": "0.0.0",
"deep-equal": "~0.0.0"
"deep-equal": "~0.0.0",
"event-stream": "~3.0.7",
"through": "~1.1.1"
},
"engines": {
"node": "0.8.x"
Expand Down
27 changes: 27 additions & 0 deletions server.js
Expand Up @@ -18,16 +18,43 @@ var DEBUG = false


var spawn = require('child_process').spawn
var split = require('event-stream').split
var through = require('through')

function start () {
var cp = spawn(process.execPath, [__dirname +'/init.js'])
cp.stdout.pipe(process.stdout, {end: false})

var lastId = null
cp.stdout.pipe(split()).pipe(through(function (line) {
try { var msg = JSON.parse(line) }
catch (e) { return }

if (!msg || !msg.length) return // heartbeat or noise
if (msg[0] === 'start') {
lastId = msg[1]
}
if (msg[0] === 'end') {
lastId = null
}
if (msg[0] === 'error') {
lastId = null
}
}))

cp.stderr.pipe(process.stderr, {end: false})

var listener
idle(cp.stdout, 'data', 1e3, listener = function () {
console.log("KILL", cp.pid)
cp.stdout.removeListener('data', listener)
cp.kill('SIGTERM')

if (lastId) {
console.log('marking ' + lastId + ' as not runnable')
// mark the process that didn't exit as bad...
model.get(lastId).set('run', false)
}
})
cp.on('error', end)
cp.on('exit', end)
Expand Down

0 comments on commit a2409bf

Please sign in to comment.