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): ensure process is kept alive when plugin communication in progress #28948

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions packages/nx/src/project-graph/plugins/isolation/plugin-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ const MINUTES = 10;

const MAX_MESSAGE_WAIT =
process.env.NX_PLUGIN_NO_TIMEOUTS === 'true'
? undefined
? // Registering a timeout prevents the process from exiting
// if the call to a plugin happens to be the only thing
// keeping the process alive. As such, even if timeouts are disabled
// we need to register one. 2147483647 is the max timeout
// that Node.js allows, and is equivalent to 24.8 days....
// This does mean that the NX_PLUGIN_NO_TIMEOUTS env var
// would still timeout after 24.8 days, but that seems
// like a reasonable compromise.
2147483647
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite a random time.

: 1000 * 60 * MINUTES; // 10 minutes

interface PendingPromise {
Expand Down Expand Up @@ -73,16 +81,15 @@ export async function loadRemoteNxPlugin(
});
// logger.verbose(`[plugin-worker] started worker: ${worker.pid}`);

const loadTimeout = MAX_MESSAGE_WAIT
? setTimeout(() => {
rej(
new Error(
`Loading "${plugin}" timed out after ${MINUTES} minutes. ${PLUGIN_TIMEOUT_HINT_TEXT}`
)
);
}, MAX_MESSAGE_WAIT)
: undefined;

const loadTimeout = setTimeout(() => {
rej(
new Error(
`Loading "${
typeof plugin === 'string' ? plugin : plugin.plugin
}" timed out after ${MINUTES} minutes. ${PLUGIN_TIMEOUT_HINT_TEXT}`
)
);
}, MAX_MESSAGE_WAIT);
socket.on(
'data',
consumeMessagesFromSocket(
Expand Down Expand Up @@ -263,21 +270,21 @@ function registerPendingPromise(
operation: string;
}
): Promise<any> {
let resolver, rejector, timeout;
let resolver: (x: unknown) => void,
rejector: (e: Error | unknown) => void,
timeout: NodeJS.Timeout;

const promise = new Promise((res, rej) => {
rejector = rej;
resolver = res;

timeout = MAX_MESSAGE_WAIT
? setTimeout(() => {
rej(
new Error(
`${context.plugin} timed out after ${MINUTES} minutes during ${context.operation}. ${PLUGIN_TIMEOUT_HINT_TEXT}`
)
);
}, MAX_MESSAGE_WAIT)
: undefined;
timeout = setTimeout(() => {
rej(
new Error(
`${context.plugin} timed out after ${MINUTES} minutes during ${context.operation}. ${PLUGIN_TIMEOUT_HINT_TEXT}`
)
);
}, MAX_MESSAGE_WAIT);

callback();
}).finally(() => {
Expand Down