jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1154444?usp=email )

Change subject: IMPR: use a Lock object in PywikibotCookieJar for thread-safe 
saving
......................................................................

IMPR: use a Lock object in PywikibotCookieJar for thread-safe saving

Bug: T396309
Change-Id: Idcfdc9e4f3a53c69c1f22f1b829090cab0e9761e
---
M pywikibot/comms/http.py
1 file changed, 12 insertions(+), 4 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified




diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py
index 8fab9f0..ef9802a 100644
--- a/pywikibot/comms/http.py
+++ b/pywikibot/comms/http.py
@@ -35,6 +35,7 @@
 import codecs
 import re
 import sys
+import threading
 import traceback
 from contextlib import suppress
 from http import HTTPStatus, cookiejar
@@ -67,8 +68,13 @@
     """CookieJar which create the filename and checks file permissions.

     .. versionadded:: 8.0
+    .. versionchanged:: 10.2
+       use `threading.Lock` in :meth:`load` and :meth`save` to be thread
+       safe.
     """

+    _lock = threading.Lock()  # Class-level lock shared across all instances
+
     def load(self, user: str = '', *args, **kwargs) -> None:
         """Loads cookies from a file.

@@ -81,7 +87,8 @@
         self.filename = config.datafilepath(f'pywikibot{_user}.lwp')

         try:
-            super().load(*args, **kwargs)
+            with self._lock:
+                super().load(*args, **kwargs)
         except (cookiejar.LoadError, FileNotFoundError):
             debug(f'Loading cookies for user {user} failed.')
         else:
@@ -96,9 +103,10 @@
         :raises ValueError: a filename was not supplied; :meth:`load`
             must be called first.
         """
-        if self.filename:
-            file_mode_checker(self.filename, create=True)
-        super().save(*args, **kwargs)
+        with self._lock:
+            if self.filename:
+                file_mode_checker(self.filename, create=True)
+            super().save(*args, **kwargs)
 

 #: global :class:`PywikibotCookieJar` instance.

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1154444?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Idcfdc9e4f3a53c69c1f22f1b829090cab0e9761e
Gerrit-Change-Number: 1154444
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to