Skip to content

Fix script module dequeue race condition#76170

Merged
youknowriad merged 2 commits intotrunkfrom
fix/script-module-dequeue-race
Mar 18, 2026
Merged

Fix script module dequeue race condition#76170
youknowriad merged 2 commits intotrunkfrom
fix/script-module-dequeue-race

Conversation

@adamsilverstein
Copy link
Copy Markdown
Member

Summary

  • gutenberg_register_script_modules() is hooked on wp_default_scripts, which fires multiple times per request (once per WP_Scripts instantiation)
  • Each invocation calls wp_deregister_script_module()wp_register_script_module() for every module
  • Since deregister() also calls dequeue(), any module enqueued between invocations (e.g. @wordpress/vips/loader enqueued on enqueue_block_editor_assets) has its enqueue silently destroyed
  • Adds a static $already_registered guard so the deregister/register cycle only runs once
  • Updates the build template so npm run build regenerates build/modules.php with the fix
  • Enables client-side media processing in the E2E test plugin

Test plan

  • Open the block editor and check the browser console for an import map containing @wordpress/vips/loader and @wordpress/vips/worker
  • Upload an image — should not throw Failed to resolve module specifier '@wordpress/vips/worker'
  • Verify npm run build regenerates build/modules.php with the static guard

🤖 Generated with Claude Code

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 5, 2026

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: adamsilverstein <[email protected]>
Co-authored-by: ramonjd <[email protected]>
Co-authored-by: mcsf <[email protected]>
Co-authored-by: youknowriad <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@adamsilverstein adamsilverstein added the [Type] Bug An existing feature does not function as intended label Mar 5, 2026
if ( $already_registered ) {
return;
}
$already_registered = true;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great spotting!

I tested on top of WordPress 6.9 so I could also check the lazy loaded @wordpress/latex-to-mathml

Before this PR I saw the following module errors for vip and latex:

Image

Now, no errors 🎉

Copy link
Copy Markdown
Member

@ramonjd ramonjd left a comment

Choose a reason for hiding this comment

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

Thanks for spotting this! I also went through the test steps in #75909, to make sure that still works.

See: #76182 (comment)

cc @dlind1 for visibility

@mcsf
Copy link
Copy Markdown
Contributor

mcsf commented Mar 6, 2026

gutenberg_register_script_modules() is hooked on wp_default_scripts, which fires multiple times per request (once per WP_Scripts instantiation)

Is this correct? Or is the repeated firing a result of the fact that WP_Scripts::init() is called on both instantiation of the $wp_scripts global and on global init?

class WP_Scripts extends WP_Dependencies {
	public function __construct() {
		$this->init();
		add_action( 'init', array( $this, 'init' ), 0 );
	}
	public function init() {
		do_action_ref_array( 'wp_default_scripts', array( &$this ) );
	}

@youknowriad
Copy link
Copy Markdown
Contributor

Can we rebase and land this PR. Would be good to get a gutenberg patch release out with this fix.

It seems the PR can also impact Core no? cc @ellatrix

wp_default_scripts fires multiple times per request. Each
invocation was calling wp_deregister_script_module() which
also dequeues the module, silently destroying enqueue state
set between invocations (e.g. @wordpress/vips/loader
enqueued on enqueue_block_editor_assets). Add a static guard
so the deregister/register cycle only runs once.
@ramonjd ramonjd force-pushed the fix/script-module-dequeue-race branch from d385e56 to 983f504 Compare March 17, 2026 21:02
@ramonjd
Copy link
Copy Markdown
Member

ramonjd commented Mar 17, 2026

I rebased this PR and smoked tested again.

Is there anything blocking it?

@github-actions
Copy link
Copy Markdown

Flaky tests detected in 983f504.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/23216283995
📝 Reported issues:

@youknowriad youknowriad merged commit d779041 into trunk Mar 18, 2026
39 checks passed
@youknowriad youknowriad deleted the fix/script-module-dequeue-race branch March 18, 2026 09:23
@youknowriad youknowriad added the Backport to Gutenberg Minor Release Pull request that needs to be backported to a Gutenberg minor release label Mar 18, 2026
@github-actions github-actions bot added this to the Gutenberg 22.8 milestone Mar 18, 2026
youknowriad added a commit that referenced this pull request Mar 18, 2026
Co-authored-by: adamsilverstein <[email protected]>
Co-authored-by: ramonjd <[email protected]>
Co-authored-by: mcsf <[email protected]>
Co-authored-by: youknowriad <[email protected]>
@youknowriad
Copy link
Copy Markdown
Contributor

Cherry-picked into Gutenberg 22.7 release branch.

@youknowriad youknowriad removed the Backport to Gutenberg Minor Release Pull request that needs to be backported to a Gutenberg minor release label Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants