Skip to content

Conversation

@dev-robin-hood
Copy link
Contributor

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.

@manfe
Copy link
Contributor

manfe commented Jan 26, 2026

How did you test the race condition? To simulate it?

@dev-robin-hood
Copy link
Contributor Author

How did you test the race condition? To simulate it?

I ran two built scripts at the same time; if you want, I can create two Chrome extensions, each with a different console log and using the global WPP.

@dev-robin-hood dev-robin-hood marked this pull request as draft January 28, 2026 00:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants