Timeouts and `lscpu: command not found` on SSR execution

I am receiving timeouts along with /bin/sh: lscpu: command not found errors in SSR function in my application. This is a Gatsby application.

Site name: dazzling-lovelace-19c3a1

Here is the full log:

Mar 7, 08:02:42 PM: INFO   Preparing Gatsby filesystem {
  from: '/var/task/.cache',
  to: '/tmp/gatsby/.cache',
  rewrites: [
    [ '/var/task/.cache/caches', '/tmp/gatsby/.cache/caches' ],
    [
      '/var/task/.cache/caches-lmdb',
      '/tmp/gatsby/.cache/caches-lmdb'
    ],
    [ '/var/task/.cache/data', '/tmp/gatsby/.cache/data' ]
  ]
}Mar 7, 08:02:42 PM: INFO   Start copying dataMar 7, 08:02:43 PM: INFO   End copying dataMar 7, 08:02:44 PM: /bin/sh: lscpu: command not foundMar 7, 08:02:52 PM: INIT_REPORT Init Duration: 10097.03 ms	Phase: init	Status: timeoutMar 7, 08:02:53 PM: INFO   Preparing Gatsby filesystem {
  from: '/var/task/.cache',
  to: '/tmp/gatsby/.cache',
  rewrites: [
    [ '/var/task/.cache/caches', '/tmp/gatsby/.cache/caches' ],
    [
      '/var/task/.cache/caches-lmdb',
      '/tmp/gatsby/.cache/caches-lmdb'
    ],
    [ '/var/task/.cache/data', '/tmp/gatsby/.cache/data' ]
  ]
}Mar 7, 08:02:53 PM: INFO   directory already existsMar 7, 08:02:55 PM: /bin/sh: lscpu: command not foundMar 7, 08:03:03 PM: INIT_REPORT Init Duration: 10581.34 ms	Phase: invoke	Status: error	Error Type: Runtime.ExitErrorMar 7, 08:03:03 PM: RequestId: 753ebbb7-5ba3-4613-bd90-dfe0e7d72c17 Error: Runtime exited with error: signal: killed
Runtime.ExitErrorMar 7, 08:03:03 PM: 753ebbb7 Duration: 11109.41 ms	Memory Usage: 1024 MB

HI @pavlos163 ,

Thank you for reporting your timeout concern here. I’ve checked your site and I can see you’re using Gatsby 5.14.1, and based on the information you provided, the errors appear to be caused by a combination of memory constraints and dependencies during your Gatsby build process on Netlify.

These include out-of-memory (OOM) errors due to resource-intensive tasks like image processing, JavaScript bundle creation, and HTML serialization, as well as the absence of the lscpu utility, which is likely being invoked by a dependency to detect system hardware capabilities.

To address these challenges, here are some potential solutions:
Disclaimer
Please note that these solutions were compiled based on available documentation and community-shared best practices found online. While I hope these will resolve the issue, there may be variables specific to your project or environment that require further customization.

1. Optimize Node.js Memory Limits:
Set a memory cap to avoid OOM crashes. Add this to your Netlify build settings or netlify.toml:

[build.environment]  
  NODE_OPTIONS = "--max-old-space-size=1536"  # Allocates 1.5GB  

Reference:
Improving Build Performance Guide | Gatsby

2. Reduce Image Processing Load:
Minimize memory spikes caused by large images and AVIF processing:

module.exports = {  
  plugins: [  
    { resolve: `gatsby-plugin-sharp`, options: { defaults: { quality: 70 } } },  // Lower image quality  
    { resolve: `gatsby-plugin-image`, options: { avifOptions: { quality: 0 } } },  // Disable AVIF  
  ],  
};

References:

3. Limit CPU Parallelism:
Restrict parallel task execution by setting a fixed CPU count:

[build]  
  command = "GATSBY_CPU_COUNT=2 gatsby build"  

Reference:
https://www.gatsbyjs.com/docs/how-to/performance/improving-build-performance

4. Address lscpu Errors:
Suppress CPU detection using GATSBY_CPU_COUNT, or optionally install the missing utility:

[build]  
  command = "apt-get update && apt-get install -y util-linux && GATSBY_CPU_COUNT=2 gatsby build"  

Reference:
https://docs.netlify.com/configure-builds/manage-dependencies

5. Enable Build Caching:
Improve build speed by caching the .cache directory:

npm install -D gatsby-plugin-netlify  

Add this to gatsby-config.js:

module.exports = {  
  plugins: ['gatsby-plugin-netlify'],  
};

References:

6. Reduce Bundle Sizes:
Disable source maps and optimize JavaScript bundle creation:

module.exports = {  
  flags: { PRESERVE_WEBPACK_CACHE: false },  
  plugins: [`gatsby-plugin-no-sourcemaps`],  
};  

References:

Thank you and I’d love to hear how these recommendations work for you! If you face any additional challenges or discover alternative solutions, please don’t hesitate to share your feedback. This will help others encountering the same issue find valuable insights and solutions here.