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

fix unnecessary memory retention paths #1232

Merged
merged 2 commits into from
Jul 2, 2022
Merged
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
58 changes: 46 additions & 12 deletions packages/ses/src/error/tame-v8-error-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
regexpExec,
regexpTest,
weakmapGet,
weakmapHas,
weakmapSet,
weaksetAdd,
weaksetHas,
Expand Down Expand Up @@ -165,6 +164,9 @@ export const tameV8ErrorConstructor = (
errorTaming,
stackFiltering,
) => {
// TODO: Proper CallSite types
/** @typedef {{}} CallSite */

const originalCaptureStackTrace = OriginalError.captureStackTrace;

// const callSiteFilter = _callSite => true;
Expand All @@ -190,9 +192,23 @@ export const tameV8ErrorConstructor = (
'',
);

// Mapping from error instance to the structured stack trace capturing the
// stack for that instance.
const ssts = new WeakMap();
/**
* @typedef {object} StructuredStackInfo
* @property {CallSite[]} callSites
* @property {undefined} [stackString]
*/

/**
* @typedef {object} ParsedStackInfo
* @property {undefined} [callSites]
* @property {string} stackString
*/

// Mapping from error instance to the stack for that instance.
// The stack info is either the structured stack trace
// or the generated tamed stack string
/** @type {WeakMap<Error, ParsedStackInfo | StructuredStackInfo} */
const stackInfos = new WeakMap();

// Use concise methods to obtain named functions without constructors.
const tamedMethods = {
Expand All @@ -214,23 +230,41 @@ export const tameV8ErrorConstructor = (
// string associated with an error.
// See https://tc39.es/proposal-error-stacks/
getStackString(error) {
if (!weakmapHas(ssts, error)) {
let stackInfo = weakmapGet(stackInfos, error);

if (stackInfo === undefined) {
// The following will call `prepareStackTrace()` synchronously
// which will populate stackInfos
// eslint-disable-next-line no-void
void error.stack;
stackInfo = weakmapGet(stackInfos, error);
if (!stackInfo) {
stackInfo = { stackString: '' };
weakmapSet(stackInfos, error, stackInfo);
}
}
const sst = weakmapGet(ssts, error);
if (!sst) {
return '';

// prepareStackTrace() may generate the stackString
// if errorTaming === 'unsafe'

if (stackInfo.stackString !== undefined) {
return stackInfo.stackString;
}
return stackStringFromSST(error, sst);

const stackString = stackStringFromSST(error, stackInfo.callSites);
weakmapSet(stackInfos, error, { stackString });

return stackString;
},
prepareStackTrace(error, sst) {
weakmapSet(ssts, error, sst);
if (errorTaming === 'unsafe') {
const stackString = stackStringFromSST(error, sst);
weakmapSet(stackInfos, error, { stackString });
return `${error}${stackString}`;
} else {
weakmapSet(stackInfos, error, { callSites: sst });
return '';
}
return '';
},
};

Expand Down Expand Up @@ -265,7 +299,7 @@ export const tameV8ErrorConstructor = (
// Use concise methods to obtain named functions without constructors.
const systemMethods = {
prepareStackTrace(error, sst) {
weakmapSet(ssts, error, sst);
weakmapSet(stackInfos, error, { callSites: sst });
return inputPrepareFn(error, safeV8SST(sst));
},
};
Expand Down
14 changes: 13 additions & 1 deletion packages/stream-node/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,23 @@ export const makeNodeWriter = writer => {

const finalIteration = new Promise((resolve, reject) => {
const finalize = () => {
// eslint-disable-next-line no-use-before-define
cleanup();
resolve({ done: true, value: undefined });
};
const error = err => {
// eslint-disable-next-line no-use-before-define
cleanup();
reject(err);
};
const cleanup = () => {
writer.off('error', error);
writer.off('finish', finalize);
writer.off('close', finalize);
};
// Streams should emit either error or finish and then may emit close.
// So, watching close is redundant but makes us feel safer.
writer.on('error', reject);
writer.on('error', error);
writer.on('finish', finalize);
writer.on('close', finalize);
});
Expand Down