I have an intermittent bug in my WP/PHP code related to updating a stored integer counter variable using update_user_meta. Basically, the code is triggered from an external HTTP request which among other things, updates a counter variable (++) using update_user_meta and then exits until the next pass, i.e., until the next HTTP ping comes in.

What happens on occasion is that the counter gets "stuck" at a particular number, and for that particular sequence of HTTP calls it stays at that number. I added some debug code to the code that does the updating of this counter variable, and the increment itself looks to be working, but when this bug occurs the value that's read back immediately after storing is not the incremented value, but the old value. Once this happens then subsequent calls (HTTP pings) result in the same stuck value. Here is the code in question:

//Increment loop index
$userID = 3;
$temp = (int) get_user_meta ($userID, 'og-miner-loopIndex', true); // true means get as single value, vs as array
error_log ("temp (num files counter) before incr= $temp");
error_log ("temp and userID after incr, before store= $temp, $userID");
update_user_meta ($userID, 'og-miner-loopIndex', $temp); // try writing this val twice due to the loop cnt bug not updating
update_user_meta ($userID, 'og-miner-loopIndex', $temp);

$temp = (int) get_user_meta ($userID, 'og-miner-loopIndex', true);
error_log ("FOR THE COUNTER BUG: Reread temp after incr and store= $temp");


[18-Oct-2017 01:24:10 UTC] POST op=report received! Entire array contents should be shown above<br />
[18-Oct-2017 01:24:10 UTC] temp (num files counter) before incr= 95<br />
[18-Oct-2017 01:24:10 UTC] temp and userID after incr, before store= 96, 3<br />
[18-Oct-2017 01:24:10 UTC] FOR THE COUNTER BUG: Reread temp after incr and store= 95<br />

The "POST" entry means that the HTTP request came in and the resulting code was invoked ok. Then you see that the counter was incremented ok from 95 to 96 in this case, but the immediate read back has it still at 95.

As you can see I also tried writing the value twice in case it didn't take the first time for whatever reason (yes, not exactly confidence inspiring), but that doesn't solve the issue. As I mentioned this is intermittent and doesn't happen often, but enough to where it must be fixed. Thank you for any insight or suggestions

  • 1
    Can you try outputting the result of update_user_meta when saving the value? Something like $update = update_user_meta... and then show the value of $update in your error log. Maybe update_user_meta is failing? Also try switching your last variable from $temp to $updated_temp and then show that. May be something odd going on with the variables. A long shot but doesn't hurt to check. – Tom Oct 18 '17 at 14:10
  • @Tom thanks for the suggestions. I'll add that extra trace and post any new information back here when/if it fails again. – Sambo Oct 19 '17 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.