Skip to content

Commit 8061f22

Browse files
authored
feat(core): expose opt-in env variable for SSG thread recycling (#11166)
1 parent b7cd106 commit 8061f22

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

packages/docusaurus/src/ssg/ssgEnv.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,14 @@ export const SSGWorkerThreadTaskSize: number = process.env
2727
.DOCUSAURUS_SSG_WORKER_THREAD_TASK_SIZE
2828
? parseInt(process.env.DOCUSAURUS_SSG_WORKER_THREAD_TASK_SIZE, 10)
2929
: 10; // TODO need fine-tuning
30+
31+
// Controls worker thread recycling behavior (maxMemoryLimitBeforeRecycle)
32+
// See https://github.com/facebook/docusaurus/pull/11166
33+
// See https://github.com/facebook/docusaurus/issues/11161
34+
export const SSGWorkerThreadRecyclerMaxMemory: number | undefined = process.env
35+
.DOCUSAURUS_SSG_WORKER_THREAD_RECYCLER_MAX_MEMORY
36+
? parseInt(process.env.DOCUSAURUS_SSG_WORKER_THREAD_RECYCLER_MAX_MEMORY, 10)
37+
: // TODO we should probably provide a default value here
38+
// 2gb is a quite reasonable max that should work well even for large sites
39+
// we'd rather ask community feedback first
40+
undefined;

packages/docusaurus/src/ssg/ssgExecutor.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import _ from 'lodash';
1212
import logger, {PerfLogger} from '@docusaurus/logger';
1313
import {createSSGParams} from './ssgParams';
1414
import {renderHashRouterTemplate} from './ssgTemplate';
15-
import {SSGWorkerThreadCount, SSGWorkerThreadTaskSize} from './ssgEnv';
15+
import {
16+
SSGWorkerThreadCount,
17+
SSGWorkerThreadRecyclerMaxMemory,
18+
SSGWorkerThreadTaskSize,
19+
} from './ssgEnv';
1620
import {generateHashRouterEntrypoint} from './ssgUtils';
1721
import {createGlobalSSGResult} from './ssgGlobalResult';
1822
import {executeSSGInlineTask} from './ssgWorkerInline';
@@ -124,6 +128,16 @@ const createPooledSSGExecutor: CreateSSGExecutor = async ({
124128
runtime: 'worker_threads',
125129
isolateWorkers: false,
126130
workerData: {params},
131+
132+
// WORKER MEMORY MANAGEMENT
133+
// Allows containing SSG memory leaks with a thread recycling workaround
134+
// See https://github.com/facebook/docusaurus/pull/11166
135+
// See https://github.com/facebook/docusaurus/issues/11161
136+
maxMemoryLimitBeforeRecycle: SSGWorkerThreadRecyclerMaxMemory,
137+
resourceLimits: {
138+
// For some reason I can't figure out how to limit memory on a worker
139+
// See https://x.com/sebastienlorber/status/1920781195618513143
140+
},
127141
});
128142
},
129143
);

0 commit comments

Comments
 (0)