Skip to content

Commit

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

/**
* Used to ensure 1 plugin : 1 worker
*/
const remotePluginCache = new Map<
string,
[Promise<LoadedNxPlugin>, () => void]
Expand All @@ -18,7 +21,8 @@ export function loadNxPluginInIsolation(
return remotePluginCache.get(cacheKey);
}

const [loadingPlugin, cleanup] = loadRemoteNxPlugin(plugin, root);
const loadingPlugin = loadRemoteNxPlugin(plugin, root);
remotePluginCache.set(cacheKey, [loadingPlugin, cleanup]);
return [loadingPlugin, cleanup];
// 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 b2f0a86

Please sign in to comment.