Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df31ade

Browse files
committed
fix: Destroy isolates and scripts in tests and when Worker completes
1 parent 3a7b3b8 commit df31ade

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

packages/test/src/mock-native-worker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ export function isolateFreeWorker(
203203
getContext() {
204204
throw new Error('Not implemented');
205205
},
206+
destroy() {
207+
/* Nothing to destroy */
208+
},
206209
},
207210
resolvedActivities,
208211
compileWorkerOptions(addDefaults(options))

packages/test/src/test-workflows.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ test.before(async (t) => {
3131
['@activities/failure-tester', failureTesterActivityFunctions],
3232
]);
3333
const builder = new WorkflowIsolateBuilder(logger, nodeModulesPath, workflowsPath, activities);
34-
t.context.contextProvider = await RoundRobinIsolateContextProvider.create(builder, 2, 1024);
34+
t.context.contextProvider = await RoundRobinIsolateContextProvider.create(builder, 1, 1024);
35+
});
36+
37+
test.after.always((t) => {
38+
t.context.contextProvider.destroy();
3539
});
3640

3741
test.beforeEach(async (t) => {

packages/worker/src/isolate-context-provider.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export interface IsolateContextProvider {
99
* Get an isolate context for running a Workflow
1010
*/
1111
getContext(): Promise<ivm.Context>;
12+
13+
/**
14+
* Destroy and cleanup any resources
15+
*/
16+
destroy(): void;
1217
}
1318

1419
/**
@@ -52,4 +57,13 @@ export class RoundRobinIsolateContextProvider implements IsolateContextProvider
5257
}
5358
return new this(poolSize, isolates, scripts);
5459
}
60+
61+
public destroy(): void {
62+
for (const script of this.scripts) {
63+
script.release();
64+
}
65+
for (const isolate of this.isolates) {
66+
isolate.dispose();
67+
}
68+
}
5569
}

packages/worker/src/worker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,7 @@ export class Worker<T extends WorkerSpec = DefaultWorkerSpec> {
11681168
.toPromise();
11691169
} finally {
11701170
await this.nativeWorker.completeShutdown();
1171+
this.isolateContextProvider.destroy();
11711172
}
11721173
}
11731174

0 commit comments

Comments
 (0)