Ensure that only one thread accesses settings.xml

Avoids corruption of settings.xml
This commit is contained in:
croneter 2018-11-26 07:35:19 +01:00
parent 7d2f785a8d
commit 030c381f65

View file

@ -8,6 +8,7 @@ from logging import getLogger
from sqlite3 import connect, OperationalError from sqlite3 import connect, OperationalError
from datetime import datetime from datetime import datetime
from unicodedata import normalize from unicodedata import normalize
from threading import Lock
try: try:
import xml.etree.cElementTree as etree import xml.etree.cElementTree as etree
import defusedxml.cElementTree as defused_etree # etree parse unsafe import defusedxml.cElementTree as defused_etree # etree parse unsafe
@ -36,6 +37,10 @@ LOG = getLogger('PLEX.utils')
WINDOW = xbmcgui.Window(10000) WINDOW = xbmcgui.Window(10000)
ADDON = xbmcaddon.Addon(id='plugin.video.plexkodiconnect') ADDON = xbmcaddon.Addon(id='plugin.video.plexkodiconnect')
# If several threads access the settings.xml file concurrently, it gets
# corrupted
SETTINGS_LOCK = Lock()
# Grab Plex id from '...plex_id=XXXX....' # Grab Plex id from '...plex_id=XXXX....'
REGEX_PLEX_ID = re.compile(r'''plex_id=(\d+)''') REGEX_PLEX_ID = re.compile(r'''plex_id=(\d+)''')
# Return the numbers at the end of an url like '.../.../XXXX' # Return the numbers at the end of an url like '.../.../XXXX'
@ -122,13 +127,14 @@ def settings(setting, value=None):
setting and value can either be unicode or string setting and value can either be unicode or string
""" """
# We need to instantiate every single time to read changed variables! # We need to instantiate every single time to read changed variables!
addon = xbmcaddon.Addon(id='plugin.video.plexkodiconnect') with SETTINGS_LOCK:
if value is not None: addon = xbmcaddon.Addon(id='plugin.video.plexkodiconnect')
# Takes string or unicode by default! if value is not None:
addon.setSetting(try_encode(setting), try_encode(value)) # Takes string or unicode by default!
else: addon.setSetting(try_encode(setting), try_encode(value))
# Should return unicode by default, but just in case else:
return try_decode(addon.getSetting(setting)) # Should return unicode by default, but just in case
return try_decode(addon.getSetting(setting))
def lang(stringid): def lang(stringid):