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

Reduced memory footprint by dereferencing bootCode #10

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
unreleased:
fixed bugs:
- GH-1037 Reduced memory footprint by dereferencing bootCode
5.1.1:
date: 2024-08-01
fixed bugs:
Expand Down
31 changes: 8 additions & 23 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const _ = require('lodash'),
bootcode = require('./bootcode'),
PostmanSandbox = require('./postman-sandbox'),
PostmanSandboxFleet = require('./postman-sandbox-fleet');

Expand All @@ -17,14 +16,7 @@ module.exports = {
}

options = _.clone(options);
bootcode((err, code) => {
if (err) { return callback(err); }
if (!code) { return callback(new Error('sandbox: bootcode missing!')); }

options.bootCode = code; // assign the code in options

new PostmanSandbox().initialize({}, options, callback);
});
new PostmanSandbox().initialize({}, options, callback);
},

/**
Expand Down Expand Up @@ -60,20 +52,13 @@ module.exports = {

connectOptions = _.clone(connectOptions);

bootcode((err, code) => {
if (err) { return callback(err); }
if (!code) { return callback(new Error('sandbox: bootcode missing!')); }

connectOptions.bootCode = code;
try {
const sandboxFleet = new PostmanSandboxFleet(registry, initOptions, connectOptions);

try {
const sandboxFleet = new PostmanSandboxFleet(registry, initOptions, connectOptions);

return callback(null, sandboxFleet);
}
catch (err) {
return callback(err);
}
});
return callback(null, sandboxFleet);
}
catch (err) {
return callback(err);
}
}
};
47 changes: 33 additions & 14 deletions lib/postman-sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@ const _ = require('lodash'),
UniversalVM = require('uvm'),
PostmanEvent = require('postman-collection').Event,
teleportJS = require('teleport-javascript'),
bootcode = require('./bootcode'),

TO_WAIT_BUFFER = 500, // time to wait for sandbox to declare timeout
CONSOLE_EVENT_NAME = 'execution.console',
EXECUTION_TIMEOUT_ERROR_MESSAGE = 'sandbox not responding',
BRIDGE_DISCONNECTING_ERROR_MESSAGE = 'sandbox: execution interrupted, bridge disconnecting.';


function getBootCode (callback) {
bootcode((err, code) => {
if (err) { return callback(err); }
if (!code) { return callback(new Error('sandbox: bootcode missing!')); }

callback(null, code);
});
}

class PostmanSandbox extends UniversalVM {
constructor () {
super();
Expand All @@ -28,27 +39,35 @@ class PostmanSandbox extends UniversalVM {
// set the dispatch timeout of UVM based on what is set in options unless original options sends the same
_.isFinite(this.connectOptions.timeout) && (this.executionTimeout = this.connectOptions.timeout);

super.connect(this.connectOptions, (err, context) => {
getBootCode((err, bootCode) => {
if (err) { return callback(err); }

this.once('initialize', (err) => {
this.isReady = true;
this.hasTimedOut = false;
this.connectOptions.bootCode = bootCode;

super.connect(this.connectOptions, (err, context) => {
delete this.connectOptions.bootCode;

this.on(CONSOLE_EVENT_NAME, (cursor, level, args) => {
if (this.connectOptions.serializeLogs) {
return this.emit('console', cursor, level, args);
}
if (err) { return callback(err); }

this.emit('console', cursor, level, ...teleportJS.parse(args));
this.once('initialize', (err) => {
this.isReady = true;
this.hasTimedOut = false;

this.on(CONSOLE_EVENT_NAME, (cursor, level, args) => {
if (this.connectOptions.serializeLogs) {
return this.emit('console', cursor, level, args);
}

this.emit('console', cursor, level, ...teleportJS.parse(args));
});

// eslint-disable-next-line n/callback-return
callback(err, context);
context = null;
});

// eslint-disable-next-line n/callback-return
callback(err, context);
context = null;
this.dispatch('initialize', this.initOptions);
});

this.dispatch('initialize', this.initOptions);
});
}

Expand Down
11 changes: 11 additions & 0 deletions test/unit/sandbox-sanity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,15 @@ describe('sandbox', function () {
});
});
});

it('should not hold reference to bootCode after initialization', function (done) {
// TODO: Add a memory footprint test to ensure bootCode is not retained elsewhere
Sandbox.createContext({ debug: true }, function (err, ctx) {
if (err) { return done(err); }
ctx.on('error', done);

expect(ctx.connectOptions.bootCode).to.be.undefined;
done();
});
});
});
Loading