Skip to content

Commit

Permalink
rotor: fix for isolated-vm leak
Browse files Browse the repository at this point in the history
  • Loading branch information
absorbb committed Dec 20, 2024
1 parent 1f1079f commit ffe8e8e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
13 changes: 10 additions & 3 deletions libs/core-functions/src/functions/lib/profiles-udf-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 202,7 @@ export const ProfileUDFWrapper = (
throw new Error(`import is not allowed: ${specifier}`);
});
wrapper.evaluateSync();
const wrapperFunc = wrap(fullId, isolate, context, wrapper);
const wrapperFunc = wrap(fullId, isolate, context, wrapper, refs);
log.atInfo().log(`[CON:${fullId}] ${functions.length} UDF functions compiled in: ${sw.elapsedPretty()}`);
return wrapperFunc;
} catch (e) {
Expand All @@ -220,7 220,9 @@ export const ProfileUDFWrapper = (
r.release();
}
context.release();
isolate.dispose();
if (!isolate.isDisposed) {
isolate.dispose();
}
log.atDebug().log(`[${fullId}] isolate closed`);
}
} catch (e) {
Expand All @@ -231,7 233,7 @@ export const ProfileUDFWrapper = (
}
};

function wrap(connectionId: string, isolate: Isolate, context: Context, wrapper: Module) {
function wrap(connectionId: string, isolate: Isolate, context: Context, wrapper: Module, refs: Reference[]) {
const exported = wrapper.namespace;

const ref = exported.getSync("wrappedFunctionChain", {
Expand Down Expand Up @@ -328,6 330,9 @@ function wrap(connectionId: string, isolate: Isolate, context: Context, wrapper:
close: () => {
try {
if (isolate) {
for (const r of refs) {
r.release();
}
context.release();
if (!isolate.isDisposed) {
isolate.dispose();
Expand Down Expand Up @@ -514,5 519,7 @@ export async function ProfileUDFTestRun({
store: !realStore && store ? memoryStoreDump(store) : {},
logs,
};
} finally {
wrapper?.close();
}
}
13 changes: 10 additions & 3 deletions libs/core-functions/src/functions/lib/udf_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 191,7 @@ export const UDFWrapper = (
throw new Error(`import is not allowed: ${specifier}`);
});
wrapper.evaluateSync();
const wrapperFunc = wrap(connectionId, isolate, context, wrapper);
const wrapperFunc = wrap(connectionId, isolate, context, wrapper, refs);
log.atInfo().log(`[CON:${connectionId}] ${functions.length} UDF functions compiled in: ${sw.elapsedPretty()}`);
return wrapperFunc;
} catch (e) {
Expand All @@ -209,7 209,9 @@ export const UDFWrapper = (
r.release();
}
context.release();
isolate.dispose();
if (!isolate.isDisposed) {
isolate.dispose();
}
log.atDebug().log(`[${connectionId}] isolate closed`);
}
} catch (e) {
Expand All @@ -220,7 222,7 @@ export const UDFWrapper = (
}
};

function wrap(connectionId: string, isolate: Isolate, context: Context, wrapper: Module) {
function wrap(connectionId: string, isolate: Isolate, context: Context, wrapper: Module, refs: Reference[]) {
const exported = wrapper.namespace;

const ref = exported.getSync("wrappedFunctionChain", {
Expand Down Expand Up @@ -304,6 306,9 @@ function wrap(connectionId: string, isolate: Isolate, context: Context, wrapper:
close: () => {
try {
if (isolate) {
for (const r of refs) {
r.release();
}
context.release();
if (!isolate.isDisposed) {
isolate.dispose();
Expand Down Expand Up @@ -499,5 504,7 @@ export async function UDFTestRun({
store: !realStore && store ? memoryStoreDump(store) : {},
logs,
};
} finally {
wrapper?.close();
}
}

0 comments on commit ffe8e8e

Please sign in to comment.