Skip to content

Commit

Permalink
fix(core): keep plugin workers until main process shutdown (#22860)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Apr 17, 2024
1 parent 18070cf commit e7c2ebd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
17 changes: 9 additions & 8 deletions packages/nx/src/project-graph/plugins/isolation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { PluginConfiguration } from '../../../config/nx-json';
import { LoadedNxPlugin } from '../internal-api';
import { loadRemoteNxPlugin } from './plugin-pool';

const remotePluginCache = new Map<
string,
[Promise<LoadedNxPlugin>, () => void]
>();
/**
* Used to ensure 1 plugin : 1 worker
*/
const remotePluginCache = new Map<string, Promise<LoadedNxPlugin>>();

export function loadNxPluginInIsolation(
plugin: PluginConfiguration,
Expand All @@ -15,10 +15,11 @@ export function loadNxPluginInIsolation(
const cacheKey = JSON.stringify(plugin);

if (remotePluginCache.has(cacheKey)) {
return remotePluginCache.get(cacheKey);
return [remotePluginCache.get(cacheKey), () => {}];
}

const [loadingPlugin, cleanup] = loadRemoteNxPlugin(plugin, root);
remotePluginCache.set(cacheKey, [loadingPlugin, cleanup]);
return [loadingPlugin, cleanup];
const loadingPlugin = loadRemoteNxPlugin(plugin, root);
remotePluginCache.set(cacheKey, loadingPlugin);
// We clean up plugin workers when Nx process completes.
return [loadingPlugin, () => {}];
}
22 changes: 8 additions & 14 deletions packages/nx/src/project-graph/plugins/isolation/plugin-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface PendingPromise {
export function loadRemoteNxPlugin(
plugin: PluginConfiguration,
root: string
): [Promise<LoadedNxPlugin>, () => void] {
): Promise<LoadedNxPlugin> {
// this should only really be true when running unit tests within
// the Nx repo. We still need to start the worker in this case,
// but its typescript.
Expand Down Expand Up @@ -60,19 +60,13 @@ export function loadRemoteNxPlugin(

cleanupFunctions.add(cleanupFunction);

return [
new Promise<LoadedNxPlugin>((res, rej) => {
worker.on(
'message',
createWorkerHandler(worker, pendingPromises, res, rej)
);
worker.on('exit', exitHandler);
}),
() => {
cleanupFunction();
cleanupFunctions.delete(cleanupFunction);
},
] as const;
return new Promise<LoadedNxPlugin>((res, rej) => {
worker.on(
'message',
createWorkerHandler(worker, pendingPromises, res, rej)
);
worker.on('exit', exitHandler);
});
}

async function shutdownPluginWorker(
Expand Down

0 comments on commit e7c2ebd

Please sign in to comment.