vercel / next.js

The React Framework
https://nextjs.org
MIT License
126.99k stars 26.99k forks source link

Memory Leak in next-worker-router? #53571

Closed c0ntradicti0n closed 1 year ago

c0ntradicti0n commented 1 year ago

Verify canary release

Provide environment information

Operating System:
      Platform: linux
      Arch: x64
      Version: #1 SMP PREEMPT_DYNAMIC Debian 6.3.7-1kali1 (2023-06-29)
    Binaries:
      Node: 18.13.0
      npm: 9.2.0
      Yarn: 1.22.19
      pnpm: N/A
    Relevant Packages:
      next: 13.4.12
      eslint-config-next: 13.4.12
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.5
    Next.js Config:
      output: 

/**
 * @type {(config?: import('next').NextConfig) => import('next').NextConfig}
 */
const withBundleAnalyzer =
  process.env.ANALYZE === 'true'
    ? require('@next/bundle-analyzer')()
    : (nextConfig) => nextConfig

const withNx = require('@nx/next/plugins/with-nx')

process.env.I18NEXT_DEFAULT_CONFIG_PATH = `${__dirname}/next-i18next.config.js`
const { i18n } = require('./next-i18next.config')

/**
 * @type {import('@nx/next/plugins/with-nx').WithNxOptions}
 **/
const nextConfig = {
  nx: {
    // Set this to true if you would like to to use SVGR
    // See: https://github.com/gregberge/svgr
    svgr: false,
  },
  webpack: (config) => {
    config.watchOptions = {
      poll: 800,
      aggregateTimeout: 300,
    }
    return config
  },
  i18n,
  experimental: {
    appDir: false,
  },
  eslint: {
    dirs: ['src'],
  },
  pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],
  useWorkers: false,
  images: {
    domains: ['datalab.stackfuel.com', 'stackfuel.com'],
    remotePatterns: [
      {
        protocol: 'https',
        hostname:
          process.env['TRAINING_CONTENT_ASSETS_URL']?.replace(
            /https:\/\/([^:/]+).*/,
            '$1'
          ) || 'assets.testing.toolboxdatenkompetenz.de',
        pathname: '**',
      },
    ],
  },
}

// https://github.com/vercel/next.js/issues/39161#issuecomment-1221473625
// nextjs is working on the use of plugins for the config
module.exports = withNx(withBundleAnalyzer(nextConfig))

Which area(s) of Next.js are affected? (leave empty if unsure)

App Router

Link to the code that reproduces this issue or a replay of the bug

I can't. Neither I can show the code, nor I can signIn with credentials in a special browser.

To Reproduce

We are using the old "pages" dir, with dynamic routes in nested folders, happens in developmentmode.

Seems to be the the case, when looking at other issues, that this might be also happening somewhere else, but I don't have the time to reproduce such an intricate app as we have just for reproducing.

Is certainly also connected to this: https://github.com/vercel/next.js/issues/50332

NextLink is also built in and regarding it's prefetching default (https://nextjs.org/docs/pages/api-reference/components/link) this may cause further workload for the router.

We have less than 30 pages there.

Describe the Bug

System is getting super busy and it's slow (1min to wait for it for start, for page navigation) It's unbearable.

image

Maybe there is in general a searching issue and there is, when the router reads the pages from the server and from the dynamics a chance to reduce results to query and one could use a normal search algorithm to get the wanted page.

But why does it start multiple such processes?

I saw from inspecting the process, that each process opens up this file .next/trace. What does it collect and why is it so big? Can we disable it?

Where are the different workers started?

After some time I sometimes get running out of file watchers, that are appearently listening to my whole home dir:

Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home//apps/tbdk' Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home//Work/web-frontend/apps' Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home//Work/web-frontend' Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home//Work' Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home/*' Watchpack Error (watcher): Error: EMFILE: too many open files, watch '/home'

Expected Behavior

Not starting many processes occupying 1.5GB of RAM and being slow.

Which browser are you using? (if relevant)

does not matter, firefox and chrome

How are you deploying your application? (if relevant)

nx run proji:serve:development

balazsorban44 commented 1 year ago

Hi, this is fixed in #53495 already, available in v13.4.13-canary.12 and above! I'll let the team know that we should cut a new stable release soon.

duducv commented 1 year ago

This happens to me to the point where I have to restart WSL due to low memory. In previous versions the same thing happened, but the process was invoked by jest-workers/proccessChild.js

image

balazsorban44 commented 1 year ago

@duducv please read https://github.com/vercel/next.js/issues/53571#issuecomment-1665602130

If this is still happening after upgrading, please open a new issue with a reproduction! :pray:

github-actions[bot] commented 1 year ago

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.