Race condition with add_action get_option set_option
-
I am editing to make a wordpress plugin made for ticket selling. It keeps track of the limit of a ticket with the help of options.
If you buy a ticket and a payment is received add_action(processing_function) is called and the processing_function first retrieves the limit data with get_option, adds 1 to the desired itemID in the option and it will update it with update_option. The problem is however, if two people buy tickets at the same time, sometimes the limit is only updated with +1.
I think this is because they retrieve the get_option at the same time and then we have a race condition since they both add +1 to the old value.
How can you deal with this in WordPress? Is using options the correct way or is there another better way in which this can’t happen?
Down here you can see a simplified version of the code (everything necessary except retrieving the POST data is in there)
<?php function data_processing() { if ( (!$paid_not_necessary)) { return; } $current_item_limits = //POST DATA WITH THE AMOUNT THE CUSTOMER BOUGHT if (get_option('item_limits')) { $item_limits = unserialize(get_option('item_limits')); foreach ($current_item_limits as $key => $value) { $item_limits[$key] = isset($item_limits[$key]) ? $item_limits[$key] + $value['quantity'] : $value['quantity']; } update_option('item_limits', serialize($item_limits)); } else { foreach ($current_item_limits as $key => $value) { $current_item_limits[$key] = $value['quantity']; } update_option('item_limits', serialize($current_item_limits)); } } add_action('cust_after_submit', 'data_processing', 10, 0);
- The topic ‘Race condition with add_action get_option set_option’ is closed to new replies.