Skip to content

Commit 6029c75

Browse files
clydindgp1130
authored andcommitted
refactor(@angular-devkit/build-angular): workaround for piscina worker destroy recreation
When destroying a `piscina` package worker pool, the minimum workers must be set to 0 first to prevent the recreation of the minimum number of workers. These workers would then be cleaned up once the worker pool goes out of scope but the recreation and initialization of each worker is unneeded processing that should be avoided.
1 parent 8321463 commit 6029c75

File tree

7 files changed

+26
-2
lines changed

7 files changed

+26
-2
lines changed

packages/angular_devkit/build_angular/src/builders/app-shell/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ async function _renderUniversal(
123123
}
124124
}
125125
} finally {
126+
// Workaround piscina bug where a worker thread will be recreated after destroy to meet the minimum.
127+
renderWorker.options.minThreads = 0;
128+
126129
await renderWorker.destroy();
127130
}
128131

packages/angular_devkit/build_angular/src/builders/prerender/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ async function _renderUniversal(
244244
}
245245
}
246246
} finally {
247+
// Workaround piscina bug where a worker thread will be recreated after destroy to meet the minimum.
248+
worker.options.minThreads = 0;
247249
void worker.destroy();
248250
}
249251

packages/angular_devkit/build_angular/src/tools/esbuild/i18n-inliner.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ export class I18nInliner {
144144
* @returns A void promise that resolves when closing is complete.
145145
*/
146146
close(): Promise<void> {
147+
// Workaround piscina bug where a worker thread will be recreated after destroy to meet the minimum.
148+
this.#workerPool.options.minThreads = 0;
149+
147150
return this.#workerPool.destroy();
148151
}
149152
}

packages/angular_devkit/build_angular/src/tools/esbuild/javascript-transformer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export class JavaScriptTransformer {
3232
constructor(options: JavaScriptTransformerOptions, maxThreads: number) {
3333
this.#workerPool = new Piscina({
3434
filename: require.resolve('./javascript-transformer-worker'),
35+
minThreads: 1,
3536
maxThreads,
3637
});
3738

@@ -102,6 +103,9 @@ export class JavaScriptTransformer {
102103
* @returns A void promise that resolves when closing is complete.
103104
*/
104105
close(): Promise<void> {
106+
// Workaround piscina bug where a worker thread will be recreated after destroy to meet the minimum.
107+
this.#workerPool.options.minThreads = 0;
108+
105109
return this.#workerPool.destroy();
106110
}
107111
}

packages/angular_devkit/build_angular/src/tools/webpack/plugins/javascript-optimizer-plugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ export class JavaScriptOptimizerPlugin {
231231

232232
await Promise.all(tasks);
233233
} finally {
234+
// Workaround piscina bug where a worker thread will be recreated after destroy to meet the minimum.
235+
workerPool.options.minThreads = 0;
234236
void workerPool.destroy();
235237
}
236238

packages/angular_devkit/build_angular/src/utils/action-executor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ export class BundleActionExecutor {
6464
}
6565

6666
stop(): void {
67-
void this.workerPool?.destroy();
67+
if (this.workerPool) {
68+
// Workaround piscina bug where a worker thread will be recreated after destroy to meet the minimum.
69+
this.workerPool.options.minThreads = 0;
70+
void this.workerPool.destroy();
71+
}
6872
}
6973
}

packages/angular_devkit/build_angular/src/utils/server-rendering/prerender.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ export async function prerenderPages(
144144

145145
await Promise.all(renderingPromises);
146146
} finally {
147+
// Workaround piscina bug where a worker thread will be recreated after destroy to meet the minimum.
148+
renderWorker.options.minThreads = 0;
147149
void renderWorker.destroy();
148150
}
149151

@@ -208,7 +210,11 @@ async function getAllRoutes(
208210

209211
const { routes: extractedRoutes, warnings }: RoutersExtractorWorkerResult = await renderWorker
210212
.run({})
211-
.finally(() => void renderWorker.destroy());
213+
.finally(() => {
214+
// Workaround piscina bug where a worker thread will be recreated after destroy to meet the minimum.
215+
renderWorker.options.minThreads = 0;
216+
void renderWorker.destroy();
217+
});
212218

213219
for (const route of extractedRoutes) {
214220
routes.add(route);

0 commit comments

Comments
 (0)