Skip to content

Commit 873b5e7

Browse files
committed
ported to execFile, which works much better
1 parent 038efcf commit 873b5e7

1 file changed

Lines changed: 42 additions & 18 deletions

File tree

‎v4-cokapi/cokapi.js‎

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,38 @@ var assert = require('assert');
3737
var child_process = require('child_process');
3838
var express = require('express');
3939
var serveStatic = require('serve-static');
40+
//var util = require('util');
41+
42+
43+
// We use this to execute since it supports utf8 and also an optional
44+
// timeout, but it needs the exact location of binaries because it doesn't
45+
// spawn a shell
46+
// http://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback
47+
48+
var NODE_BIN = '/usr/local/bin/node';
49+
var PYTHON2_BIN = '/usr/local/bin/python';
50+
var PYTHON3_BIN = '/usr/local/bin/python3';
51+
52+
var TIMEOUT_SECS = 3;
53+
54+
55+
function postExecHandler(res, err, stdout, stderr) {
56+
if (err) {
57+
var errTrace;
58+
if (err.killed) {
59+
// timeout!
60+
errTrace = {code: '', trace: [{'event': 'uncaught_exception',
61+
'exception_msg': 'Timeout error. Your code ran for more than ' + TIMEOUT_SECS + ' seconds. Please shorten and try again.'}]};
62+
res.send(JSON.stringify(errTrace));
63+
} else {
64+
errTrace = {code: '', trace: [{'event': 'uncaught_exception',
65+
'exception_msg': "Unknown error. Report a bug to philip@pgbovine.net by clicking on the\n'Generate URL' button at the bottom and including a URL in your email."}]};
66+
res.send(JSON.stringify(errTrace));
67+
}
68+
} else {
69+
res.send(stdout);
70+
}
71+
}
4072

4173

4274
var app = express();
@@ -47,15 +79,15 @@ app.get('/exec_py2', function(req, res) {
4779
if (USE_DOCKER_SANDBOX) {
4880
assert(false);
4981
} else {
50-
executePython('python', req, res);
82+
executePython(PYTHON2_BIN, req, res);
5183
}
5284
});
5385

5486
app.get('/exec_py3', function(req, res) {
5587
if (USE_DOCKER_SANDBOX) {
5688
assert(false);
5789
} else {
58-
executePython('python3', req, res);
90+
executePython(PYTHON3_BIN, req, res);
5991
}
6092
});
6193

@@ -69,14 +101,10 @@ app.get('/exec_js', function(req, res) {
69101
'--jsondump=true',
70102
'--code=' + usrCod];
71103

72-
var child = child_process.spawn('node', args);
73-
var stdoutDat = '';
74-
child.stdout.on('data', function(data) {
75-
stdoutDat += data;
76-
});
77-
child.on('close', function(code) {
78-
res.send(String(stdoutDat));
79-
});
104+
child_process.execFile(NODE_BIN, args,
105+
{timeout: TIMEOUT_SECS * 1000 /* milliseconds */,
106+
maxBuffer: 2000 * 1024, /* 2MB data max */},
107+
postExecHandler.bind(null, res));
80108
}
81109
});
82110

@@ -100,14 +128,10 @@ function executePython(pythonExe, req, res) {
100128
}
101129
args.push('--code=' + usrCod);
102130

103-
var child = child_process.spawn(pythonExe, args);
104-
var stdoutDat = '';
105-
child.stdout.on('data', function(data) {
106-
stdoutDat += data;
107-
});
108-
child.on('close', function(code) {
109-
res.send(String(stdoutDat));
110-
});
131+
child_process.execFile(pythonExe, args,
132+
{timeout: TIMEOUT_SECS * 1000 /* milliseconds */,
133+
maxBuffer: 2000 * 1024, /* 2MB data max */},
134+
postExecHandler.bind(null, res));
111135
}
112136

113137
var server = app.listen(3000, function() {

0 commit comments

Comments
 (0)