Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WC_Product_Data_Store_CPT::update_product_stock race condition #26039

Merged
merged 1 commit into from Apr 6, 2020

Conversation

soulseekah
Copy link
Contributor

@soulseekah soulseekah commented Mar 31, 2020

There's a race condition in WC_Product_Data_Store_CPT::update_product_stock and a @todo to get rid of it. This PR addresses this.

Full details, proof and tests at https://codeseekah.com/2020/03/31/woocommerce-cant-count-either

This PR contains a commit that modifies the increment and decrement SQL query in WC_Product_Data_Store_CPT::update_product_stock to be atomic.

All Submissions:

  • Have you followed the WooCommerce Contributing guideline?
  • Does your code follow the WordPress' coding standards?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?

Testing instructions

Note that this patch does not change the normal behavior, it just makes it more atomic. To test follow this normal flow:

  1. Create a product with managed stock.
  2. Checkout and create an order using that product. Then manually set the order to cancelled. Product stock should be increased correctly. (Check by going to product edit page -> Inventory settings)
  3. Manually set the order to complete again. Product stock should be decreased correctly.

Changelog entry

Dev - Make WC_Product_Data_Store_CPT::update_product_stock operations atomic.

Modify the increment and decrement SQL query in
WC_Product_Data_Store_CPT::update_product_stock to be atomic. This fixes
a race condition in concurrent order placement.
Copy link
Contributor

@vedanshujain vedanshujain left a comment

Tested and works well. Thanks for the assist 🙌

@vedanshujain vedanshujain merged commit dff7d8f into woocommerce:master Apr 6, 2020
1 check passed
@woocommercebot woocommercebot added release: add changelog release: add testing instructions labels Apr 6, 2020
@vedanshujain vedanshujain removed release: add changelog release: add testing instructions labels Apr 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants