Page MenuHomePhabricator

Problems with logging in
Open, HighPublicBUG REPORT

Description

For a few weeks or months, I have constantly been getting errors such as:

Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/data/project/matsubot/pwbvenv/lib/python3.9/site-packages/pywikibot/__init__.py", line 1485, in async_manager
    request(*args, **kwargs)
  File "/data/project/matsubot/pwbvenv/lib/python3.9/site-packages/pywikibot/page/_decorators.py", line 34, in handle
    func(self, *args, **kwargs)
  File "/data/project/matsubot/pwbvenv/lib/python3.9/site-packages/pywikibot/page/_wikibase.py", line 680, in editEntity
    super().editEntity(data, **kwargs)
  File "/data/project/matsubot/pwbvenv/lib/python3.9/site-packages/pywikibot/page/_wikibase.py", line 315, in editEntity
    updates = self.repo.editEntity(
  File "/data/project/matsubot/pwbvenv/lib/python3.9/site-packages/pywikibot/site/_decorators.py", line 92, in callee
    return fn(self, *args, **kwargs)
  File "/data/project/matsubot/pwbvenv/lib/python3.9/site-packages/pywikibot/site/_datasite.py", line 310, in editEntity
    params['token'] = self.tokens['csrf']
  File "/data/project/matsubot/pwbvenv/lib/python3.9/site-packages/pywikibot/site/_tokenwallet.py", line 59, in __getitem__
    raise KeyError(
KeyError: "Invalid token 'csrf' for user 'MatSuBot' on wikidata:wikidata wiki."

or

WARNING: API error badtoken: Invalid CSRF token.

or

ERROR: User assertion failed. Forcing re-login.
Logging in to wikidata:wikidata as MatSuBot@pywikibot
ERROR: Received incorrect login token. Forcing re-login.

for my Toolforge-based bot running on Wikidata. It's happening with and without BotPasswords.

Related Objects

StatusSubtypeAssignedTask
OpenBUG REPORTNone
OpenBUG REPORTNone
OpenBUG REPORTNone

Event Timeline

It would be better to receive the token instead raising a KeyError

Xqt triaged this task as High priority.Aug 1 2023, 10:14 AM
Xqt moved this task from Backlog to Login on the Pywikibot board.
Xqt added a project: Pywikibot-login.py.

The problem persists. It always makes the same script fail when it starts. The script updates Wikidata labels. Other Wikidata scripts which edit data do not fail.

Found a new exception in the logs:

WARNING: /usr/lib/python3.9/http/cookiejar.py:1971: UserWarning: http.cookiejar bug!
Traceback (most recent call last):
  File "/usr/lib/python3.9/http/cookiejar.py", line 1953, in _really_load
    c = Cookie(h("version"), name, value,
  File "/usr/lib/python3.9/http/cookiejar.py", line 767, in __init__
    if version is not None: version = int(version)
ValueError: invalid literal for int() with base 10: 'None'

  _warn_unhandled_exception()
This comment was removed by Xqt.

Same problem with pwb.py replace (some older version, then also 8.5.1, 8.6.0, master):

WARNING: Http response status 404
WARNING: Http response status 404
Exception in thread Put-Thread:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/m/src/pywikibot/pywikibot/__init__.py", line 399, in async_manager
    request(*args, **kwargs)
  File "/home/m/src/pywikibot/pywikibot/page/_decorators.py", line 35, in handle
    func(self, *args, **kwargs)
  File "/home/m/src/pywikibot/pywikibot/page/_basepage.py", line 1281, in _save
    done = self.site.editpage(self, summary=summary, minor=minor,
  File "/home/m/src/pywikibot/pywikibot/site/_decorators.py", line 86, in callee
    return fn(self, *args, **kwargs)
  File "/home/m/src/pywikibot/pywikibot/site/_apisite.py", line 2057, in editpage
    token = self.tokens['csrf']
  File "/home/m/src/pywikibot/pywikibot/site/_tokenwallet.py", line 60, in __getitem__
    raise KeyError(
KeyError: "Invalid token 'csrf' for user 'MykhalBot' on wikipedia:cs wiki."

on some cswiki page, but did not have problem on some other page.

This comment was removed by mykhal.

In the log I see queries to doi.org.. ??

2024-01-24 16:12:55       _requests.py,  744 in        _json_loads: DEBUG    Non-JSON response received from server doi:doi for url
https://www.doi.org/w/api.php?action=query&meta=userinfo&uiprop=blockinfo%7Cgroups%7Chasmsg%7Cratelimits%7Crights&formatversion=2&maxlag=5&format=json
The server may be down.
Status code: 404

So in my case, the problem is {{doi:…}} in the page (probably regardless if the prefix handling exists or not, in my case the syntax was not functional in either cswiki and testwiki).
(I've isolated the problem in https://test.wikipedia.org/wiki/Test_bot_doi_problems (ignore the NBSP typo which the script is designed to fix))

.. the problem disappears on the second try, but on cleanup apicache-py3/ cleanup it's there again. (but I could not reproduce it on another tries later on)

I was suspicious there could be a race condition since my bot uses the asynchronous saving feature. So, I forced it to save synchronously. When I have it log out before running the script, I get messages like

Change summary: add missing labels
Logging in to wikidata:wikidata as MatSuBot@[redacted bot password key]
WARNING: API warning (main): Subscribe to the mediawiki-api-announce mailing list at <https://lists.wikimedia.org/postorius/lists/mediawiki-api-announce.lists.wikimedia.org/> for notice of API deprecations and breaking changes. Use [[Special:ApiFeatureUsage]] to see usage of deprecated features by your application.
WARNING: API warning (login): Fetching a token via "action=login" is deprecated. Use "action=query&meta=tokens&type=login" instead.
ERROR: Received incorrect login token. Forcing re-login.
Change summary: add missing labels
ERROR: User assertion failed. Forcing re-login.
Change summary: add missing labels
ERROR: User assertion failed. Forcing re-login.
Change summary: add missing labels

and so on. The ERROR: lines later occur less often. But the script keeps running, and edits are saved.

So there are two problems:

  • the bot does not handle and retry in asynchronous mode (but it does in synchronous mode)
  • there are still some login (edit token) problems

The doi links problem could be related, but I get errors by running the bot on Wikidata.