Skip to content

Commit ffe8e8e

Browse files
committed
rotor: fix for isolated-vm leak
1 parent 1f1079f commit ffe8e8e

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

libs/core-functions/src/functions/lib/profiles-udf-wrapper.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export const ProfileUDFWrapper = (
202202
throw new Error(`import is not allowed: ${specifier}`);
203203
});
204204
wrapper.evaluateSync();
205-
const wrapperFunc = wrap(fullId, isolate, context, wrapper);
205+
const wrapperFunc = wrap(fullId, isolate, context, wrapper, refs);
206206
log.atInfo().log(`[CON:${fullId}] ${functions.length} UDF functions compiled in: ${sw.elapsedPretty()}`);
207207
return wrapperFunc;
208208
} catch (e) {
@@ -220,7 +220,9 @@ export const ProfileUDFWrapper = (
220220
r.release();
221221
}
222222
context.release();
223-
isolate.dispose();
223+
if (!isolate.isDisposed) {
224+
isolate.dispose();
225+
}
224226
log.atDebug().log(`[${fullId}] isolate closed`);
225227
}
226228
} catch (e) {
@@ -231,7 +233,7 @@ export const ProfileUDFWrapper = (
231233
}
232234
};
233235

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

237239
const ref = exported.getSync("wrappedFunctionChain", {
@@ -328,6 +330,9 @@ function wrap(connectionId: string, isolate: Isolate, context: Context, wrapper:
328330
close: () => {
329331
try {
330332
if (isolate) {
333+
for (const r of refs) {
334+
r.release();
335+
}
331336
context.release();
332337
if (!isolate.isDisposed) {
333338
isolate.dispose();
@@ -514,5 +519,7 @@ export async function ProfileUDFTestRun({
514519
store: !realStore && store ? memoryStoreDump(store) : {},
515520
logs,
516521
};
522+
} finally {
523+
wrapper?.close();
517524
}
518525
}

libs/core-functions/src/functions/lib/udf_wrapper.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export const UDFWrapper = (
191191
throw new Error(`import is not allowed: ${specifier}`);
192192
});
193193
wrapper.evaluateSync();
194-
const wrapperFunc = wrap(connectionId, isolate, context, wrapper);
194+
const wrapperFunc = wrap(connectionId, isolate, context, wrapper, refs);
195195
log.atInfo().log(`[CON:${connectionId}] ${functions.length} UDF functions compiled in: ${sw.elapsedPretty()}`);
196196
return wrapperFunc;
197197
} catch (e) {
@@ -209,7 +209,9 @@ export const UDFWrapper = (
209209
r.release();
210210
}
211211
context.release();
212-
isolate.dispose();
212+
if (!isolate.isDisposed) {
213+
isolate.dispose();
214+
}
213215
log.atDebug().log(`[${connectionId}] isolate closed`);
214216
}
215217
} catch (e) {
@@ -220,7 +222,7 @@ export const UDFWrapper = (
220222
}
221223
};
222224

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

226228
const ref = exported.getSync("wrappedFunctionChain", {
@@ -304,6 +306,9 @@ function wrap(connectionId: string, isolate: Isolate, context: Context, wrapper:
304306
close: () => {
305307
try {
306308
if (isolate) {
309+
for (const r of refs) {
310+
r.release();
311+
}
307312
context.release();
308313
if (!isolate.isDisposed) {
309314
isolate.dispose();
@@ -499,5 +504,7 @@ export async function UDFTestRun({
499504
store: !realStore && store ? memoryStoreDump(store) : {},
500505
logs,
501506
};
507+
} finally {
508+
wrapper?.close();
502509
}
503510
}

0 commit comments

Comments
 (0)