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

feat(core): lock graph creation when running in another process #29408

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

AgentEnder
Copy link
Member

Current Behavior

Running Nx in multiple processes at the same time with the daemon disabled can cripple a system due to excess memory usage when creating the graph. This is due to plugin workers being started per-parent process when there is no daemon. This change enables a file lock to prevent the simultaneous processing, and read from the cache when the first run completes.

Currently, running nx show projects 30 times in parallel looks something like this:

30 processes exited within 37535ms

Expected Behavior

30 processes exited within 6435ms

Test Script

//@ts-check

const { spawn } = require('child_process');

let alive = new Set();

let start = Date.now();
let iterations = 30;

for (let i = 0; i < iterations; i++) {
  const cp = spawn('npx nx show projects', [], {
    shell: true,
    env: {
      ...process.env,
      NX_DAEMON: 'false',
      NX_VERBOSE_LOGGING: 'true',
    },
  });
  alive.add(i);
  //   cp.stdout.on('data', (data) => {
  //     console.log(`stdout [${i}]: ${data}`);
  //   });
  cp.stderr.on('data', (data) => {
    console.error(`stderr [${i}]: ${data}`);
  });
  cp.on('exit', (code) => {
    console.log(`child process ${i} exited with code ${code}`);
    alive.delete(i);
  });
}

const i = setInterval(() => {
  if (alive.size > 0) {
  } else {
    clearInterval(i);
    console.log(
      `${iterations} processes exited within ${Date.now() - start}ms`
    );
  }
}, 1);

@AgentEnder AgentEnder requested a review from a team as a code owner December 18, 2024 22:33
@AgentEnder AgentEnder requested a review from xiongemi December 18, 2024 22:33
Copy link

vercel bot commented Dec 18, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Updated (UTC)
nx-dev ⬜️ Ignored (Inspect) Visit Preview Dec 19, 2024 11:22pm

Copy link

nx-cloud bot commented Dec 18, 2024

View your CI Pipeline Execution ↗ for commit 41b174c.

Command Status Duration Result
nx affected --targets=lint,test,build,e2e,e2e-c... ❌ Failed 36m 30s View ↗
nx run-many -t check-imports check-commit check... ✅ Succeeded 59s View ↗
nx-cloud record -- nx-cloud conformance:check ✅ Succeeded 1s View ↗
nx-cloud record -- nx format:check --base=16c8b... ✅ Succeeded 18s View ↗
nx-cloud record -- nx sync:check ✅ Succeeded 16s View ↗
nx documentation --no-dte ✅ Succeeded 4m 9s View ↗

☁️ Nx Cloud last updated this comment at 2024-12-20 00:05:25 UTC

@AgentEnder AgentEnder force-pushed the file-lock-graph branch 2 times, most recently from d1f6a60 to 8bb1c07 Compare December 19, 2024 16:14
@@ -290,6 +316,7 @@ export async function createProjectGraphAndSourceMapsAsync(
'create-project-graph-async:start',
'create-project-graph-async:end'
);
lock.unlock();
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be in a finally at least

import { existsSync, rmSync, watch, writeFileSync, mkdirSync } from 'fs';
import { dirname } from 'path';

export class FileLock {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Write this in rust

}

wait(timeout?: number) {
return new Promise<void>((res, rej) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use polling

@AgentEnder AgentEnder requested a review from a team as a code owner December 19, 2024 20:16
@AgentEnder AgentEnder requested a review from Cammisuli December 19, 2024 20:16
packages/nx/src/native/utils/file_lock.rs Outdated Show resolved Hide resolved
packages/nx/src/native/utils/file_lock.rs Outdated Show resolved Hide resolved
} catch {}
}

process.on('exit', cleanupFileLock);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We might not need this

);
}
return {
projectGraph: await readCachedGraphAndHydrateFileMap(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should throw an error if the graph is in a bad state

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants