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

memory: fix 2 memory leaks in next-dev #43859

Merged
merged 3 commits into from Dec 8, 2022
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
Expand Up @@ -10,6 +10,8 @@ type WebpackPluginInstance = webpack.WebpackPluginInstance
const originModules = [
require.resolve('../../../server/require'),
require.resolve('../../../server/load-components'),
require.resolve('../../../server/next-server'),
require.resolve('../../../compiled/react-server-dom-webpack/client'),
]

const RUNTIME_NAMES = ['webpack-runtime', 'webpack-api-runtime']
Expand Down
15 changes: 15 additions & 0 deletions packages/next/server/dev/next-dev-server.ts
Expand Up @@ -109,6 +109,7 @@ export default class DevServer extends Server {
private edgeFunctions?: RoutingItem[]
private verifyingTypeScript?: boolean
private usingTypeScript?: boolean
private originalFetch?: typeof fetch

protected staticPathsWorker?: { [key: string]: any } & {
loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths
Expand Down Expand Up @@ -149,6 +150,7 @@ export default class DevServer extends Server {

constructor(options: Options) {
super({ ...options, dev: true })
this.persistPatchedGlobals()
this.renderOpts.dev = true
;(this.renderOpts as any).ErrorDebug = ReactDevOverlay
this.devReady = new Promise((resolve) => {
Expand Down Expand Up @@ -1385,6 +1387,14 @@ export default class DevServer extends Server {
return this.hotReloader!.ensurePage({ page: pathname, clientOnly: false })
}

private persistPatchedGlobals(): void {
this.originalFetch = global.fetch
}

private restorePatchedGlobals(): void {
global.fetch = this.originalFetch!
}

protected async findPageComponents({
pathname,
query,
Expand Down Expand Up @@ -1418,6 +1428,11 @@ export default class DevServer extends Server {
this.serverCSSManifest = super.getServerCSSManifest()
}
this.fontLoaderManifest = super.getFontLoaderManifest()
// before we re-evaluate a route module, we want to restore globals that might
// have been patched previously to their original state so that we don't
// patch on top of the previous patch, which would keep the context of the previous
// patched global in memory, creating a memory leak.
this.restorePatchedGlobals()
Copy link
Member

Choose a reason for hiding this comment

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

Q: Should we change the patching strategy? like only patch once instead of restoring the fetch?


return super.findPageComponents({ pathname, query, params, isAppPath })
} catch (err) {
Expand Down