Skip to content

Commit a8d41c0

Browse files
authored
Use a specific lock file to avoid temp directory contention (#13371)
Previously in debug mode we could try to avoid temp file contension by taking and holding the cache lock for the duration of the process. This change uses a specific lock file within the temp directory which avoid needing a writable cache directory. Its also more logical. Partial fix for #13369
1 parent 4928f85 commit a8d41c0

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

emcc.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"""
2525

2626

27-
import atexit
2827
import json
2928
import logging
3029
import os
@@ -1927,14 +1926,6 @@ def get_full_import_name(name):
19271926
# exit block 'parse arguments and setup'
19281927
log_time('parse arguments and setup')
19291928

1930-
if DEBUG:
1931-
# we are about to start using temp dirs. serialize access to the temp dir
1932-
# when using EMCC_DEBUG, since we don't want multiple processes would to
1933-
# use it at once, they might collide if they happen to use the same
1934-
# tempfile names
1935-
shared.Cache.acquire_cache_lock()
1936-
atexit.register(shared.Cache.release_cache_lock)
1937-
19381929
if options.post_link:
19391930
process_libraries(libs, lib_dirs, temp_files)
19401931
if len(input_files) != 1:

tools/shared.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from . import cache, tempfiles, colored_logger
3030
from . import diagnostics
3131
from . import config
32+
from . import filelock
3233

3334

3435
DEBUG = int(os.environ.get('EMCC_DEBUG', '0'))
@@ -396,6 +397,20 @@ def __init__(self):
396397
except Exception as e:
397398
exit_with_error(str(e) + 'Could not create canonical temp dir. Check definition of TEMP_DIR in ' + config.config_file_location())
398399

400+
# Since the cannonical temp directory is, by definition, the same
401+
# between all processes that run in DEBUG mode we need to use a multi
402+
# process lock to prevent more than one process from writing to it.
403+
# This is because emcc assumes that it can use non-unique names inside
404+
# the temp directory.
405+
# In the case where we run emcc recurively to populate the cache we
406+
# do (sadly) still need to ignore this lock, in the same way we do for
407+
# the cache lock.
408+
if 'EM_EXCLUSIVE_CACHE_ACCESS' not in os.environ:
409+
filelock_name = os.path.join(self.EMSCRIPTEN_TEMP_DIR, 'emscripten.lock')
410+
lock = filelock.FileLock(filelock_name)
411+
lock.acquire()
412+
atexit.register(lock.release)
413+
399414
def get_temp_files(self):
400415
return tempfiles.TempFiles(
401416
tmp=self.TEMP_DIR if not DEBUG else get_emscripten_temp_dir(),

0 commit comments

Comments
 (0)