Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -387,18 +387,26 @@ export class CustomLogicBlock {
tablesPack
},
});
// Terminate the worker after the final 'done' (or on error) so
// the V8 isolate is released. Without this the worker thread
// stays alive after the script body returns, leaking ~30 MB per
// custom-logic invocation.
const cleanup = () => { worker.terminate().catch(() => { /* noop */ }); };
Comment thread
vshvets-bc marked this conversation as resolved.
Outdated
worker.on('error', (error) => {
cleanup();
reject(error);
});
worker.on('message', async (data) => {
try {
if (data?.type === 'done') {
await done(data.result, data.final);
if (data.final) cleanup();
}
if (data?.type === 'debug') {
ref.debug(data.message);
}
} catch (error) {
cleanup();
reject(error);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,32 @@ export class DataTransformationAddon {
},
});

// Terminate the worker after the final result / on error so the V8
// isolate is released. Without this the worker thread stays alive
// after the script body returns, leaking ~30 MB per transformation.
Comment thread
vshvets-bc marked this conversation as resolved.
Outdated
const cleanup = () => { worker.terminate().catch(() => { /* noop */ }); };
Comment thread
vshvets-bc marked this conversation as resolved.
Outdated

Comment thread
vshvets-bc marked this conversation as resolved.
Outdated
const done = async (result: any | any[], final: boolean) => {
if (!result) {
if (final) {
cleanup();
resolve(null);
}
return;
}
if (final) cleanup();
resolve(result);
}

worker.on('error', (error) => {
cleanup();
reject(error);
});
worker.on('message', async (result: any) => {
try {
await done(result.result, result.final);
} catch (error) {
cleanup();
reject(error);
}
});
Expand Down
6 changes: 6 additions & 0 deletions policy-service/src/policy-engine/blocks/math-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,21 @@ export class MathBlock {
return new Promise<IPolicyDocument>(async (resolve, reject) => {
const workerFile = path.join(path.dirname(filename), '..', 'helpers', 'workers', 'math-worker.js');
const worker = new Worker(workerFile, { workerData });
// Terminate the worker once it finishes so the V8 isolate is released.
// Otherwise every formula evaluation leaks a worker thread (~30 MB).
const cleanup = () => { worker.terminate().catch(() => { /* noop */ }); };
Comment thread
vshvets-bc marked this conversation as resolved.
Outdated
worker.on('error', (error) => {
cleanup();
reject(error);
});
worker.on('message', async (data) => {
try {
if (data?.type === 'done') {
cleanup();
resolve(data.result)
}
} catch (error) {
cleanup();
reject(error);
}
});
Expand Down
Loading