Error trying to change preferences through API on a wiki the user hasn't visited
Server Environment: Production
URL: https://hydra.gamepedia.com/api.php
Detailed Reproduction Steps:
First, determine a wiki that you haven't visited yet while logged in (for the purposes of this ticket I will use thislandismyland.gamepedia.com as it is the newest wiki at time of writing)
Then, retrieve an edit token (api.php?action=query&meta=tokens&type=csrf) for the unvisited wiki.
Next, perform a POST request (with the token just received) to api.php, params: {action:"options",change:"language=en",token:{token}
}
It will error with the following:
{
"error": {
"code": "internal_api_error_MWException",
"info": "[1a332e41742692b8f7faabab] Exception caught: CAS update failed on user_touched for user ID '10' (read from master); the version of the user to be saved is older than the current version.",
"*": "MWException at /home/hydra/public_html/includes/user/User.php(4160)\n#0 [internal function]: User->{closure}(Wikimedia\\Rdbms\\DatabaseAurora, string)\n#1 /home/hydra/public_html/includes/libs/rdbms/database/Database.php(3666): call_user_func_array(Closure, array)\n#2 /home/hydra/public_html/includes/user/User.php(4174): Wikimedia\\Rdbms\\Database->doAtomicSection(string, Closure)\n#3 /home/hydra/public_html/includes/api/ApiOptions.php(129): User->saveSettings()\n#4 /home/hydra/public_html/includes/api/ApiMain.php(1579): ApiOptions->execute()\n#5 /home/hydra/public_html/includes/api/ApiMain.php(535): ApiMain->executeAction()\n#6 /home/hydra/public_html/includes/api/ApiMain.php(506): ApiMain->executeActionWithErrorHandling()\n#7 /home/hydra/public_html/api.php(94): ApiMain->execute()\n#8 {main}"
}
}
If you visit the wiki in a different browser window and retry the same requests it will succeed.
This procedure may be performed using the MW JS API from any GP wiki page:
fa = new mw.ForeignApi("//thislandismyland.gamepedia.com/api.php");
fa.postWithToken('csrf',{action:"options",change:"language=en"}).done(function(data){console.log("success")}).fail(function(code,error){console.log(error);});
//retry after visiting the wiki
fa.postWithToken('csrf',{action:"options",change:"language=en"}).done(function(data){console.log("success")}).fail(function(code,error){console.log(error);});
I suspect the issue may be linked to either HydraAuth or CurseProfile and trying to twice-save user settings somewhere.
Possibly related WMF issues: