Race condition on reporting beacon
-
Hello,
I think I may have found a possible race condition in
ajax_frontend_timing().The table
wp_speedix_responseshas a unique index on:time_bucketrequest_urirequest_type
However,
ajax_frontend_timing()currently does:$updated = $wpdb->query( ... UPDATE ... ); if ( $updated === 0 ) { $wpdb->insert(...); }Occasionally I get the following database error:
Duplicate entry '...' for key 'unique_bucket_uri'The stack trace points to:
do_action('wp_ajax_nopriv_speedix_frontend_timing') WP_Hook->do_action() WP_Hook->apply_filters() Speedix->ajax_frontend_timing()I noticed that
save_response_data()already usesINSERT ... ON DUPLICATE KEY UPDATE, whileajax_frontend_timing()uses anUPDATEfollowed by anINSERT.Could this be a race condition? For example:
- Two frontend timing beacons arrive almost simultaneously.
- Both
UPDATEstatements affect 0 rows. - The first request performs the
INSERT. - The second request attempts the same
INSERTand fails with the duplicate key error.
I’m not certain this is the root cause, but it seems like a possible explanation.
Has anyone else seen this, or is there a reason why
ajax_frontend_timing()does not use an atomic upsert likesave_response_data()?
You must be logged in to reply to this topic.