Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): keep plugin workers until main process shutdown #22860

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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