Opened 6 years ago
Last modified 6 years ago
#51962 new defect (bug)
Race condition in wp_get_custom_css_post() would clear the custom CSS entry
| Reported by: | candrei | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Customize | Version: | 4.7 |
| Severity: | normal | Keywords: | reporter-feedback |
| Cc: | Focuses: |
Description
Our production system was recently affected by a (what it looks to me) race condition in wp_get_custom_css_post(); for reference here's the code in question:
if ( get_stylesheet() === $stylesheet ) { $post_id = get_theme_mod( 'custom_css_post_id' ); if ( $post_id > 0 && get_post( $post_id ) ) { $post = get_post( $post_id ); } // `-1` indicates no post exists; no query necessary. if ( ! $post && -1 !== $post_id ) { $query = new WP_Query( $custom_css_query_vars ); $post = $query->post; /* * Cache the lookup. See wp_update_custom_css_post(). * @todo This should get cleared if a custom_css post is added/removed. */ set_theme_mod( 'custom_css_post_id', $post ? $post->ID : -1 ); } } else {
Our database went down after retrieving the $post_id from options. get_post() failed silently followed by new WP_Query( $custom_css_query_vars ); that failed too while the DB was down. Eventually the DB became available and the custom_css_post_id setting was set to -1.
As a result our website's styling was affected until we were able to restore the custom CSS stored in the custom_css post.
I think better error handling within the wp_get_custom_css_post() could prevent such side effect from taking place.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Hi @candrei, and welcome to WordPress Trac!
Did you have any ideas for what kind of error handling could be added to
wp_get_custom_css_post()that would have prevented this issue? I confess that I'm not sure I see the race condition yet.