Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#50324 closed defect (bug) (fixed)

Can't create new blog in multisite if database insert happens on clean_site_cache

Reported by: trepmal's profile trepmal Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 5.5 Priority: normal
Severity: normal Version: 5.3
Component: Networks and Sites Keywords: has-patch
Focuses: multisite Cc:

Description

In multisite, you cannot create a new blog if there is a database insert happening on the clean_site_cache action.

Introduced in r45910

https://core.trac.wordpress.org/browser/trunk/src/wp-includes/ms-site.php#L68-L74

On L68, the blog data is inserted into the blogs table. The new blog ID is in $wpdb->insert_id. This ID is then needed shortly after at L74 to get the WP_Site object.
However, clean_blog_cache() has an action hook. If something hooked in there does a database insert, the value of $wpdb->insert_id changes. This is likely to cause a Could not retrieve site data. but there are some other scenarios:

  • If $wpdb->insert_id does not match any blog ID: Error: Could not retrieve site data.
  • If $wpdb->insert_id matches a valid blog: potentially perform actions against the wrong blog. Anything run on wp_initialize_site will run against the preexisting blog, not the newly created one.
  • If $wpdb->insert_id matches a half-installed blog (such as from case 1): The original blog will be fully installed since L74 now returns truthy. However, there will be some mixed values (home/siteurl will match wp_blogs values of older site, blogname will match the newly created blog.)
  • If $wpdb->insert_id matches by pure chance the correct blog_id: Task failed successfully.

Attachments (3)

50324.diff (1.6 KB) - added by trepmal 4 years ago.
patch with test
plugin-reproduce-50324.php (355 bytes) - added by trepmal 4 years ago.
A plugin to reproduce the issue
50324.1.diff (1.6 KB) - added by trepmal 4 years ago.
fix the ticket reference in the test

Download all attachments as: .zip

Change History (5)

@trepmal
4 years ago

patch with test

@trepmal
4 years ago

A plugin to reproduce the issue

@trepmal
4 years ago

fix the ticket reference in the test

#1 @SergeyBiryukov
4 years ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 5.5

#2 @SergeyBiryukov
4 years ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 47932:

Networks and Sites: Ensure the site ID in wp_insert_site() is still correct after calling clean_blog_cache().

By saving $wpdb->insert_id to a variable, we make sure the ID does not change if another database insert is performed on the clean_site_cache action.

Props trepmal.
Fixes #50324.

Note: See TracTickets for help on using tickets.