Cherry pick 51735 into release/9.4 (#52121)

* Add a retry mechanism to obtain the lock for inserting products through the API (#51735)

* Add a retry mechanism to the query to obtain the lock for inserting products in the API

* Add changefile(s) from automation for the following project(s): woocommerce

* Log errors after failed attempts and delay 10ms for each attempt

* Fix lint

---------

Co-authored-by: github-actions <github-actions@github.com>

* Prep for cherry pick 51735

---------

Co-authored-by: Alba Rincón <albarin@users.noreply.github.com>
Co-authored-by: github-actions <github-actions@github.com>
Co-authored-by: WooCommerce Bot <no-reply@woocommerce.com>
This commit is contained in:
github-actions[bot] 2024-10-17 09:33:27 -07:00 committed by GitHub
parent 69d43a62df
commit 7d65885bbb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 2 deletions

View File

@ -145,8 +145,35 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
return boolval( $locked );
}
// The insert query can potentially result in a deadlock if there is high concurrency
// when trying to insert products, which will result in a false negative for SKU lock
// and incorrectly products not being created.
// To mitigate this, we will retry the query 3 times before giving up.
for ( $attempts = 0; $attempts < 3; $attempts++ ) {
if ( $attempts > 1 ) {
usleep( 10000 );
}
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$result = $wpdb->query( $query );
if ( false !== $result ) {
break;
}
}
if ( false === $result ) {
wc_get_logger()->warning(
sprintf(
'Failed to obtain SKU lock for product: ID "%d" with SKU "%s" after %d attempts.',
$product_id,
$sku,
$attempts,
),
array(
'error' => $wpdb->last_error,
)
);
}
return (bool) $result;
}

View File

@ -173,6 +173,7 @@ WooCommerce comes with some sample data you can use to see how products look; im
**WooCommerce**
* Fix - Add a retry mechanism to the query to obtain the SKU lock for product creation through the API. [#51735](https://github.com/woocommerce/woocommerce/pull/51735)
* Fix - Fix `Error: Failed opening required '.../wp-content/plugins/woocommerce/src/StoreApi/Schemas/V1/AI/ProductsSchema.php` fatal error [#51912](https://github.com/woocommerce/woocommerce/pull/51912)
* Fix - Resolved fatal error when applying Brands-restricted coupon [#51577](https://github.com/woocommerce/woocommerce/pull/51577)
* Fix - Fix CSS issue with Safari 18.0 on the product form page. [#51734](https://github.com/woocommerce/woocommerce/pull/51734)