1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-12-27 02:16:12 +01:00

Fix race condition in using .old directory in Serviceinfo.execute()

This commit is contained in:
Daniel Mach 2022-09-05 15:33:00 +02:00
parent 6a1e8053ac
commit ca6a352acb

View File

@ -20,6 +20,7 @@ import shlex
import shutil import shutil
import subprocess import subprocess
import sys import sys
import time
from functools import cmp_to_key from functools import cmp_to_key
from http.client import IncompleteRead from http.client import IncompleteRead
from io import StringIO from io import StringIO
@ -375,16 +376,23 @@ class Serviceinfo:
def execute(self, dir, callmode = None, singleservice = None, verbose = None): def execute(self, dir, callmode = None, singleservice = None, verbose = None):
old_dir = os.path.join(dir, '.old') old_dir = os.path.join(dir, '.old')
if os.path.exists(old_dir) or os.path.islink(old_dir):
msg = '"%s" exists, please remove it' % old_dir # if 2 osc instances are executed at a time one, of them fails on .old file existence
raise oscerr.OscIOError(None, msg) # sleep up to 10 seconds until we can create the directory
for i in reversed(range(10)):
try: try:
os.mkdir(old_dir) os.mkdir(old_dir)
return self._execute(dir, old_dir, callmode, singleservice, break
verbose) except FileExistsError:
finally: time.sleep(1)
if os.path.exists(old_dir):
if i == 0:
msg = f'"{old_dir}" exists, please remove it'
raise oscerr.OscIOError(None, msg)
result = self._execute(dir, old_dir, callmode, singleservice, verbose)
shutil.rmtree(old_dir) shutil.rmtree(old_dir)
return result
def _execute(self, dir, old_dir, callmode=None, singleservice=None, def _execute(self, dir, old_dir, callmode=None, singleservice=None,
verbose=None): verbose=None):