Skip to content

Commit

Permalink
Merge pull request #13647 from webpack/perf/memory-improvements
Browse files Browse the repository at this point in the history
improve disposing of lazy compilation backend server
  • Loading branch information
sokra committed Jun 28, 2021
2 parents 3bcd719 + 291321c commit 18e93eb
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
18 changes: 17 additions & 1 deletion lib/hmr/lazyCompilationBackend.js
Expand Up @@ -52,6 +52,16 @@ module.exports = (compiler, client, callback) => {
}
if (moduleActivated && compiler.watching) compiler.watching.invalidate();
});
let isClosing = false;
/** @type {Set<import("net").Socket>} */
const sockets = new Set();
server.on("connection", socket => {
sockets.add(socket);
socket.on("close", () => {
sockets.delete(socket);
});
if (isClosing) socket.destroy();
});
server.listen(err => {
if (err) return callback(err);
const addr = server.address();
Expand All @@ -67,7 +77,13 @@ module.exports = (compiler, client, callback) => {
);
callback(null, {
dispose(callback) {
server.close(callback);
isClosing = true;
server.close(err => {
callback(err);
});
for (const socket of sockets) {
socket.destroy(new Error("Server is disposing"));
}
},
module(originalModule) {
const key = `${encodeURIComponent(
Expand Down
2 changes: 1 addition & 1 deletion lib/optimize/ModuleConcatenationPlugin.js
Expand Up @@ -450,7 +450,7 @@ class ModuleConcatenationPlugin {
},
err => {
logger.timeEnd("create concatenated modules");
process.nextTick(() => callback(err));
process.nextTick(callback.bind(null, err));
}
);
}
Expand Down
18 changes: 15 additions & 3 deletions test/ConfigTestCases.template.js
Expand Up @@ -492,9 +492,21 @@ const describeCases = config => {
}
};

results.push(
_require(outputDirectory, optionsArr[i], bundlePath)
);
if (Array.isArray(bundlePath)) {
for (const bundlePathItem of bundlePath) {
results.push(
_require(
outputDirectory,
optionsArr[i],
"./" + bundlePathItem
)
);
}
} else {
results.push(
_require(outputDirectory, optionsArr[i], bundlePath)
);
}
}
}
// give a free pass to compilation that generated an error
Expand Down
1 change: 1 addition & 0 deletions test/HotTestCases.template.js
Expand Up @@ -39,6 +39,7 @@ const describeCases = config => {
let compiler;
afterAll(callback => {
compiler.close(callback);
compiler = undefined;
});

it(
Expand Down
24 changes: 21 additions & 3 deletions test/helpers/createLazyTestEnv.js
Expand Up @@ -4,7 +4,13 @@ const createOnceFn = fn => {
if (!fn) return null;
if (fn.length >= 1) {
return done => {
fn(done);
try {
fn(done);
} catch (e) {
// avoid leaking memory
e.stack;
throw e;
}
fn = null;
};
}
Expand All @@ -22,11 +28,23 @@ const createDisposableFn = fn => {
let rfn;
if (fn.length >= 1) {
rfn = done => {
fn(done);
try {
fn(done);
} catch (e) {
// avoid leaking memory
e.stack;
throw e;
}
};
} else {
rfn = () => {
return fn();
try {
return fn();
} catch (e) {
// avoid leaking memory
e.stack;
throw e;
}
};
}
rfn.dispose = () => {
Expand Down

0 comments on commit 18e93eb

Please sign in to comment.