Make WordPress Core

Opened 4 years ago

Last modified 4 years ago

#51962 new defect (bug)

Race condition in wp_get_custom_css_post() would clear the custom CSS entry

Reported by: candrei's profile candrei Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7
Component: Customize Keywords: reporter-feedback
Focuses: Cc:

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.

Change History (1)

#1 @dlh
4 years ago

  • Component changed from General to Customize
  • Keywords reporter-feedback added
  • Version changed from 5.5.3 to 4.7

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.

Note: See TracTickets for help on using tickets.