Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement sandbox execution in separated child_process to prevent shared event loop leaks #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
change callback into promise
ceelsoin committed Apr 14, 2023
commit c0903757b78e913effa25ac6dbdbac1c56af2b5e
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -67,6 +67,8 @@ const req = {
body: { x: 10, y: 10}
};

const taskId = `${namespace}/${id}-${Date.now()}`; //or can be any uniq id

executeFunctionInSandbox(taskId, {
env: {
MY_VAR: 'TRUE', // environment variable will be available on Backstage.env.MY_VAR
@@ -79,14 +81,14 @@ executeFunctionInSandbox(taskId, {
namespace: "foo",
functionName: "bar",
options,
}, (result) => { // when result callback was called, the child process of sandbox execution will be died
if(result.error) {
console.error({ error: err.message || err }) //print error return from function execution
}

console.log(result) //print return from function execution
})

/* can return result using callback or using async await */
.then(result => {
console.log(result)
})
.catch(err => {
console.log(err)
})
```

## Configuration
34 changes: 22 additions & 12 deletions lib/ForkSandbox.js
Original file line number Diff line number Diff line change
@@ -3,25 +3,28 @@ const child_process = require('child_process');

const Sandbox = require("./Sandbox");

const tasks = {};
const childs = {};

const codeFileName = (namespace, codeId) => {
return `${namespace}/${codeId}.js`;
}

const executeFunctionInSandbox = (id, data, callback) => {

childs[id] = child_process.fork(path.resolve(__filename)); //require this file to execute process.on('message') below
const executeFunctionInSandbox = (id, data) => {
return new Promise((resolve, reject) => {
childs[id] = child_process.fork(path.resolve(__filename)); //require this file to execute process.on('message') below

childs[id].send({ id, data });

childs[id].send({ id, data });
tasks[id] = callback;
childs[id].on('message', (message) => {
const result = message;
if (result.error) {
reject(result.error);
}

resolve(result.data);
delete childs[id];

childs[id].on('message', (message) => {
const result = message.data;
tasks[message.id](result);
delete tasks[id];
delete childs[id];
});
});
}

@@ -36,8 +39,15 @@ process.on('message', async (message) => {
const sandbox = new Sandbox({ env, globalModules, asyncTimeout, syncTimeout, config });
const filename = codeFileName(namespace, functionName);

const _req = Object.assign({
method: null,
headers: {},
body: {},
query: {},
}, req);

const script = sandbox.compileCode(filename, preCode.code)
const result = await sandbox.runScript(script, req, options);
const result = await sandbox.runScript(script, _req, options);
process.send({ id, data: result });
process.exit(0);
} catch (ex) {