fix(webpack): prevent state race on multiple script injections #3258
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #3222
Added a global lock in
window.__WPP_INIT_LOCK__that tracks initialization state across script re-executions. The lock has three states: not_started, initializing, and completed.When
injectLoader()runs, it checks this global state. If init already finished, it syncs local variables from global state and returns. If init is currently running, it waits for the stored Promise to complete then syncs state. Only starts a new init if nothing has run yet.Refactored the initialization code into separate functions (performMetaLoaderInit, performWebpackLoaderInit, performInitialization) and made sure state updates go to both local variables and the global lock. Timer IDs are tracked globally so we can clean them up instead of leaving orphaned timers from previous script executions.
This stops multiple instances from racing with conflicting timers and state. Works whether you re-inject before, during, or after init completes. No changes to the public API.