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

fix: The setStatus() of the HMR module should not return an array, which may cause infinite recursion. #17556

Merged
merged 2 commits into from
Feb 5, 2024

Conversation

System233
Copy link
Contributor

@System233 System233 commented Aug 2, 2023

Summary

🤖 Generated by Copilot at d293083

Fix unhandled promise rejections in HotModuleReplacement.runtime.js. Add an empty callback to Promise.all in setStatus to handle any status without handlers.

The setStatus() of the HMR module should not return an array, which may cause infinite recursion.

Related issues

Fixes #16972, #13857, #13876, #13877

Details

🤖 Generated by Copilot at d293083

  • Add an empty callback to Promise.all in setStatus to prevent unhandled rejections (link)

PR #13576 Incorrectly makes the setStatus method in the HMR module return an array.

function setStatus(newStatus) {
currentStatus = newStatus;
var results = [];
for (var i = 0; i < registeredStatusHandlers.length; i++)
results[i] = registeredStatusHandlers[i].call(null, newStatus);
return Promise.all(results);
}

before that:
function setStatus(newStatus) {
currentStatus = newStatus;
for (var i = 0; i < registeredStatusHandlers.length; i++)
registeredStatusHandlers[i].call(null, newStatus);
}

In the absence of $hmrDownloadManifest$, this array will be passed to the next then function, causing the if(!update) condition to fail,

return setStatus("check")
.then($hmrDownloadManifest$)
.then(function (update) {
if (!update) {
return setStatus(applyInvalidatedModules() ? "ready" : "idle");
}

image
image
resulting in infinite recursion in here:

webpack/hot/poll.js

Lines 13 to 24 in 1f99ad6

var checkForUpdate = function checkForUpdate(fromUpdate) {
if (module.hot.status() === "idle") {
module.hot
.check(true)
.then(function (updatedModules) {
if (!updatedModules) {
if (fromUpdate) log("info", "[HMR] Update applied.");
return;
}
require("./log-apply-result")(updatedModules, updatedModules);
checkForUpdate(true);
})

Furthermore, since updatedModules is an empty array, this code will endlessly print [HMR] Nothing hot updated. to the console:

if (!renewedModules || renewedModules.length === 0) {
log("info", "[HMR] Nothing hot updated.");
} else {

image

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Aug 2, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

@webpack-bot
Copy link
Contributor

For maintainers only:

  • This needs to be documented (issue in webpack/webpack.js.org will be filed when merged)
  • This needs to be backported to webpack 4 (issue will be created when merged)

@System233 System233 changed the title Fix: The setStatus() of the HMR module should not return an array. fix: The setStatus() of the HMR module should not return an array, which may cause infinite recursion. Aug 2, 2023
Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

Can you provide reproducble example with the problem? I am not sure it is really fix the problem in right way

@System233
Copy link
Contributor Author

Can you provide reproducble example with the problem? I am not sure it is really fix the problem in right way

Take a look: https://github.com/System233/webpack-hmr-bug-replay.git

@System233
Copy link
Contributor Author

Under certain target configurations, when the workerChunkLoading default value is interpreted as false or when the ModuleChunkLoadingRuntimeModule is utilized, it leads to hmrDownloadManifest being undefined. Consequently, this renders the subsequent 'then' call ineffective.

@rupasix
Copy link

rupasix commented Sep 15, 2023

I'm testing that PR because of random Nothing hot updated infinite loops when project is on idle for few minutes. My observations is: hmr and manual page reload is so much, MUCH faster and no infinite loops.

@callmeteus
Copy link

I'm actually facing this issue and this PR solved it for me.
I think it should be merged ASAP.

@alexander-akait alexander-akait merged commit f478066 into webpack:main Feb 5, 2024
20 checks passed
@alexander-akait
Copy link
Member

Sorry for delay and thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Infinity recursion in hot/poll / [HMR] Nothing hot updated again in 5.78.0.
5 participants