fix: race condition in os.makedirs causing worker crashes
Replace check-then-create pattern with exist_ok=True to prevent FileExistsError when multiple workers initialize simultaneously. Affects: - subscriptions.py: open_database() - watch.py: save_decrypt_cache() - local_playlist.py: add_to_playlist() - util.py: fetch_url(), get_visitor_data() - settings.py: initialization Fixes Gunicorn worker startup failures in multi-worker deployments.
This commit is contained in:
@@ -453,8 +453,7 @@ else:
|
|||||||
print("Running in non-portable mode")
|
print("Running in non-portable mode")
|
||||||
settings_dir = os.path.expanduser(os.path.normpath("~/.yt-local"))
|
settings_dir = os.path.expanduser(os.path.normpath("~/.yt-local"))
|
||||||
data_dir = os.path.expanduser(os.path.normpath("~/.yt-local/data"))
|
data_dir = os.path.expanduser(os.path.normpath("~/.yt-local/data"))
|
||||||
if not os.path.exists(settings_dir):
|
os.makedirs(settings_dir, exist_ok=True)
|
||||||
os.makedirs(settings_dir)
|
|
||||||
|
|
||||||
settings_file_path = os.path.join(settings_dir, 'settings.txt')
|
settings_file_path = os.path.join(settings_dir, 'settings.txt')
|
||||||
|
|
||||||
|
|||||||
@@ -26,8 +26,7 @@ def video_ids_in_playlist(name):
|
|||||||
|
|
||||||
|
|
||||||
def add_to_playlist(name, video_info_list):
|
def add_to_playlist(name, video_info_list):
|
||||||
if not os.path.exists(playlists_directory):
|
os.makedirs(playlists_directory, exist_ok=True)
|
||||||
os.makedirs(playlists_directory)
|
|
||||||
ids = video_ids_in_playlist(name)
|
ids = video_ids_in_playlist(name)
|
||||||
missing_thumbnails = []
|
missing_thumbnails = []
|
||||||
with open(os.path.join(playlists_directory, name + ".txt"), "a", encoding='utf-8') as file:
|
with open(os.path.join(playlists_directory, name + ".txt"), "a", encoding='utf-8') as file:
|
||||||
|
|||||||
@@ -30,8 +30,7 @@ database_path = os.path.join(settings.data_dir, "subscriptions.sqlite")
|
|||||||
|
|
||||||
|
|
||||||
def open_database():
|
def open_database():
|
||||||
if not os.path.exists(settings.data_dir):
|
os.makedirs(settings.data_dir, exist_ok=True)
|
||||||
os.makedirs(settings.data_dir)
|
|
||||||
connection = sqlite3.connect(database_path, check_same_thread=False)
|
connection = sqlite3.connect(database_path, check_same_thread=False)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -343,8 +343,7 @@ def fetch_url(url, headers=(), timeout=15, report_text=None, data=None,
|
|||||||
and debug_name is not None
|
and debug_name is not None
|
||||||
and content):
|
and content):
|
||||||
save_dir = os.path.join(settings.data_dir, 'debug')
|
save_dir = os.path.join(settings.data_dir, 'debug')
|
||||||
if not os.path.exists(save_dir):
|
os.makedirs(save_dir, exist_ok=True)
|
||||||
os.makedirs(save_dir)
|
|
||||||
|
|
||||||
with open(os.path.join(save_dir, debug_name), 'wb') as f:
|
with open(os.path.join(save_dir, debug_name), 'wb') as f:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
@@ -902,8 +901,7 @@ INNERTUBE_CLIENTS = {
|
|||||||
def get_visitor_data():
|
def get_visitor_data():
|
||||||
visitor_data = None
|
visitor_data = None
|
||||||
visitor_data_cache = os.path.join(settings.data_dir, 'visitorData.txt')
|
visitor_data_cache = os.path.join(settings.data_dir, 'visitorData.txt')
|
||||||
if not os.path.exists(settings.data_dir):
|
os.makedirs(settings.data_dir, exist_ok=True)
|
||||||
os.makedirs(settings.data_dir)
|
|
||||||
if os.path.isfile(visitor_data_cache):
|
if os.path.isfile(visitor_data_cache):
|
||||||
with open(visitor_data_cache, 'r') as file:
|
with open(visitor_data_cache, 'r') as file:
|
||||||
print('Getting visitor_data from cache')
|
print('Getting visitor_data from cache')
|
||||||
|
|||||||
@@ -329,11 +329,8 @@ def get_ordered_music_list_attributes(music_list):
|
|||||||
|
|
||||||
|
|
||||||
def save_decrypt_cache():
|
def save_decrypt_cache():
|
||||||
try:
|
os.makedirs(settings.data_dir, exist_ok=True)
|
||||||
f = open(os.path.join(settings.data_dir, 'decrypt_function_cache.json'), 'w')
|
f = open(os.path.join(settings.data_dir, 'decrypt_function_cache.json'), 'w')
|
||||||
except FileNotFoundError:
|
|
||||||
os.makedirs(settings.data_dir)
|
|
||||||
f = open(os.path.join(settings.data_dir, 'decrypt_function_cache.json'), 'w')
|
|
||||||
|
|
||||||
f.write(json.dumps({'version': 1, 'decrypt_cache':decrypt_cache}, indent=4, sort_keys=True))
|
f.write(json.dumps({'version': 1, 'decrypt_cache':decrypt_cache}, indent=4, sort_keys=True))
|
||||||
f.close()
|
f.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user