You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Intermittent bot failures due EM_CACHE_IS_LOCKED building ports with libsdl2-ttf #27160
The common pattern seems to be the occurrence of cache:INFO: deleting cached file: ... libSDL2.a in the log file, which logically should never happen during test_other.py.
The cause looks to me some kind of race condition that occurs when multiple tests all try to build libSDL2. In this case it seems like we end up calling Post.clear_project_build .. which causes the library built by another process to be deleted.
Our locking mechanism for the cache is not designed to be robust gainst library being deleted by an out side process, and this can break the assertion in the ports build that all deps have been built before we build a given port.
I think the fix we need to make here is to add / wider our locking such as that so matter many many concurrent processes try to build a given library only one will end up building it and none of them will call Post.clear_project_build.
The bug is indeed caused by Ports.fetch_port_artifact returning False in one of the parallel processes, which triggers clear_project_build
and deletes the library ( libSDL2.a ) that another process has just built.
Here is the high-level sequence that leads to the failure:
Process A starts building sdl2_ttf . It successfully unpacks sdl2 (writing the .emscripten_url marker) and builds libSDL2.a .
Process A proceeds to build sdl2_ttf and holds the cache lock. It spawns a child emcc to compile SDL_ttf.c . This child inherits the
EM_CACHE_IS_LOCKED=1 environment variable.
Process B (running in parallel) acquires the lock after Process A releases it momentarily. It checks if sdl2 is up-to-date.
The Race: If up_to_date('sdl2') in Process B returns False (which can happen due to filesystem latency in reflecting the marker file written
by Process A, especially on macOS/Windows), Process B thinks the port is invalid.
The Destruction: Process B calls clear_project_build('sdl2') , which deletes libSDL2.a from the cache.
The Collision: Process A's child emcc (compiling SDL_ttf.c ) needs sdl2 . It sees libSDL2.a is missing and tries to rebuild it.
The Crash: The child emcc calls cache.get , which tries to lock the cache. Since it has EM_CACHE_IS_LOCKED=1 in its environment, it fails
the assertion and exits with the EM_CACHE_IS_LOCKED error.
This explains why the bug is intermittent and why it has primarily been seen on macOS and Windows, where filesystem metadata updates can have more
latency than on Linux.
The intermittent failures accompanied by cache:INFO: deleting cached file: are caused by a race condition between parallel worker processes in tools/ports/__init__.py, introduced in PR #24303 (26b4bcef02).
In that PR, fetch_project was split into fetch_port_artifact and Ports.clear_project_build. As a result of that refactoring, clear_project_build was moved outside the cache lock:
@staticmethoddeffetch_port_artifact(name, url, sha512hash=None):
...
withcache.lock('unpack port'):
ifos.path.exists(fullpath):
ifup_to_date():
returnTrue
...
retrieve()
unpack()
returnFalse# Exits `with cache.lock('unpack port')` -> releases lock!@staticmethoddeffetch_project(name, url, sha512hash=None):
ifnotPorts.fetch_port_artifact(name, url, sha512hash):
# we unpacked a new version, clear the build in the cachePorts.clear_project_build(name) # CALLED OUTSIDE THE LOCK!
Inside Ports.clear_project_build(name):
port.clear() is called, which calls cache.erase_lib(...).
erase_lib calls erase_file(...), which acquires with cache.lock('erase: ' + shortname):.
If the file exists, it logs cache:INFO: deleting cached file: ... and removes the file.
Sequence of Events Across Concurrent Workers
Suppose two tests running concurrently on different workers in parallel_testsuite.py both depend on the same port (e.g. test_sdl2_ttf and test_sdl2_mixer_wav for sdl2, or test_sdl2_ttf and test_sdl3_ttf for harfbuzz):
sequenceDiagram
autonumber
actor A as Process A (Worker 1)
participant Lock as Global cache.lock
participant Disk as Cache Disk Storage
actor B as Process B (Worker 2)
Note over A,B: Clean cache: both tests need port (e.g. sdl2)
A->>Lock: Acquire lock ("unpack port")
A->>Disk: Retrieve & unpack sdl2 (writes .emscripten_url)
A->>Lock: Release lock ("unpack port")
Note over A: fetch_port_artifact returns False
B->>Disk: Check up_to_date() outside lock
Disk-->>B: .emscripten_url exists and matches (True)
Note over B: fetch_port_artifact returns True immediately
B->>Lock: Acquire lock ("libSDL2.a")
Note over B: Begins compiling 119 inputs for libSDL2.a
A->>Lock: clear_project_build() -> erase_file() tries to acquire lock ("erase")
Note over A: BLOCKED! Sits waiting for cache.lock held by B
Note over B: Compiling libSDL2.a (takes ~16s)...
B->>Disk: Writes built libSDL2.a to disk
B->>Lock: Release lock ("libSDL2.a") -> logs "cache:INFO: - ok"
Note over A: Wakes up and acquires cache.lock ("erase")!
A->>Disk: Checks if libSDL2.a exists (True, just built by B!)
A->>Disk: Deletes libSDL2.a -> logs "deleting cached file: ... libSDL2.a"
A->>Lock: Release lock ("erase")
Note over B: Attempts to link / compile dependent port
B-->>Disk: Reads libSDL2.a
Note over B: FAILS! libSDL2.a was deleted by A!
Loading
Step-by-Step Breakdown
Process A runs fetch_project('sdl2'). It acquires cache.lock('unpack port'), downloads, and unpackages the port.
Process A exits with cache.lock('unpack port') and returns False. The global cache lock is released.
In that exact window, Process B runs fetch_project('sdl2'). It checks up_to_date() outside the lock. Since Process A already completed the unpack and wrote .emscripten_url, up_to_date() returns True. Process B returns True immediately without calling clear_project_build.
Process B calls cache.get_lib('libSDL2.a'). The library does not exist yet, so Process B acquires cache.lock('libSDL2.a') and begins compiling its inputs (e.g. 119 inputs for SDL2 taking ~16s, or 55 inputs for HarfBuzz taking ~36s).
Meanwhile, Process A proceeds to Ports.clear_project_build('sdl2'), which calls erase_file('.../libSDL2.a'). It attempts to acquire cache.lock('erase: ...'). Because Process B holds the cache lock, Process A blocks.
Process B finishes compilation, writes the .a library to disk, logs cache:INFO: - ok, and releases cache.lock.
The instant the lock is released, Process A unblocks and acquires cache.lock. It checks if name.exists():. Because Process B just created it, the file exists! Process A logs cache:INFO: deleting cached file: ... and deletes the newly built library.
Process B now proceeds to the link step or compiles dependent inputs using child emcc processes:
A child emcc (e.g. compiling SDL_ttf.c) expects libSDL2.a to exist. Since it was just deleted, the child tries to acquire the cache lock. Because it inherited EM_CACHE_IS_LOCKED=1 from Process B, it hits: AssertionError: attempt to lock the cache while a parent process is holding the lock.
Or wasm-ld runs and fails with: wasm-ld: error: cannot open ...: No such file or directory.
PR #27161 unpacked into .tmp and used os.replace to prevent other processes from seeing a partially unpacked directory. However, this race occurs after unpacking is 100% complete: the race window is between fetch_port_artifact releasing cache.lock and fetch_project calling clear_project_build.
This also explains why:
cache:INFO: deleting cached file: always appears immediately after cache:INFO: - ok across all CI logs (Linux, macOS, Windows).
Neither retrieving port: nor unpacking port: was logged when deleting cached file: happened (the unpack happened earlier, before compilation started).
Proposed Fix
Move Ports.clear_project_build(name)insidefetch_port_artifact under with cache.lock('unpack port'):, immediately after unpack():
withcache.lock('unpack port'):
ifos.path.exists(fullpath):
ifup_to_date():
returnTruelogger.warning('local copy of port is not correct, retrieving from remote server')
utils.delete_dir(fullname)
utils.delete_file(fullpath)
retrieve()
unpack()
# Clear any previous build under the lock before releasing itPorts.clear_project_build(name)
returnFalse
Because cache.lock is re-entrant within the same process (tracked via acquired_count), erase_file executes safely under the already-held lock without releasing it. No other process can observe the port as up-to-date or acquire the cache lock until clear_project_build has finished.
Activity
sbc100 commentedon Jun 22, 2026
I think the fix we need to make here is to add / wider our locking such as that so matter many many concurrent processes try to build a given library only one will end up building it and none of them will call
Post.clear_project_build.sbc100 commentedon Jun 23, 2026
AI generated root analysis:
Summary of the Root Cause
The bug is indeed caused by Ports.fetch_port_artifact returning False in one of the parallel processes, which triggers clear_project_build
and deletes the library ( libSDL2.a ) that another process has just built.
Here is the high-level sequence that leads to the failure:
EM_CACHE_IS_LOCKED=1 environment variable.
by Process A, especially on macOS/Windows), Process B thinks the port is invalid.
the assertion and exits with the EM_CACHE_IS_LOCKED error.
This explains why the bug is intermittent and why it has primarily been seen on macOS and Windows, where filesystem metadata updates can have more
latency than on Linux.
sbc100 commentedon Jun 23, 2026
This just showed up on the linux CI https://logs.chromium.org/logs/emscripten-releases/buildbucket/cr-buildbucket/8678253928542712369/+/u/Emscripten_testsuite__other_/stdout
I think anther test is doing
deleting cached file whiletest_sdl2_ttf` is running.So we know it is reproducible on linux
[ports] Unpack ports atomically to improve concurrency robustness (#2…
sbc100 commentedon Jun 24, 2026
We just this happen on macOS again: https://logs.chromium.org/logs/emscripten-releases/buildbucket/cr-buildbucket/8678099676295138017/+/u/Emscripten_testsuite__other_/stdout
So I guess the fix in #27161 did not work :(
sbc100 commentedon Sep 1, 2026
AI-generated Root Cause Analysis
The intermittent failures accompanied by
cache:INFO: deleting cached file:are caused by a race condition between parallel worker processes intools/ports/__init__.py, introduced in PR #24303 (26b4bcef02).In that PR,
fetch_projectwas split intofetch_port_artifactandPorts.clear_project_build. As a result of that refactoring,clear_project_buildwas moved outside the cache lock:Inside
Ports.clear_project_build(name):port.clear()is called, which callscache.erase_lib(...).erase_libcallserase_file(...), which acquireswith cache.lock('erase: ' + shortname):.cache:INFO: deleting cached file: ...and removes the file.Sequence of Events Across Concurrent Workers
Suppose two tests running concurrently on different workers in
parallel_testsuite.pyboth depend on the same port (e.g.test_sdl2_ttfandtest_sdl2_mixer_wavforsdl2, ortest_sdl2_ttfandtest_sdl3_ttfforharfbuzz):sequenceDiagram autonumber actor A as Process A (Worker 1) participant Lock as Global cache.lock participant Disk as Cache Disk Storage actor B as Process B (Worker 2) Note over A,B: Clean cache: both tests need port (e.g. sdl2) A->>Lock: Acquire lock ("unpack port") A->>Disk: Retrieve & unpack sdl2 (writes .emscripten_url) A->>Lock: Release lock ("unpack port") Note over A: fetch_port_artifact returns False B->>Disk: Check up_to_date() outside lock Disk-->>B: .emscripten_url exists and matches (True) Note over B: fetch_port_artifact returns True immediately B->>Lock: Acquire lock ("libSDL2.a") Note over B: Begins compiling 119 inputs for libSDL2.a A->>Lock: clear_project_build() -> erase_file() tries to acquire lock ("erase") Note over A: BLOCKED! Sits waiting for cache.lock held by B Note over B: Compiling libSDL2.a (takes ~16s)... B->>Disk: Writes built libSDL2.a to disk B->>Lock: Release lock ("libSDL2.a") -> logs "cache:INFO: - ok" Note over A: Wakes up and acquires cache.lock ("erase")! A->>Disk: Checks if libSDL2.a exists (True, just built by B!) A->>Disk: Deletes libSDL2.a -> logs "deleting cached file: ... libSDL2.a" A->>Lock: Release lock ("erase") Note over B: Attempts to link / compile dependent port B-->>Disk: Reads libSDL2.a Note over B: FAILS! libSDL2.a was deleted by A!Step-by-Step Breakdown
fetch_project('sdl2'). It acquirescache.lock('unpack port'), downloads, and unpackages the port.with cache.lock('unpack port')and returnsFalse. The global cache lock is released.fetch_project('sdl2'). It checksup_to_date()outside the lock. Since Process A already completed the unpack and wrote.emscripten_url,up_to_date()returnsTrue. Process B returnsTrueimmediately without callingclear_project_build.cache.get_lib('libSDL2.a'). The library does not exist yet, so Process B acquirescache.lock('libSDL2.a')and begins compiling its inputs (e.g. 119 inputs for SDL2 taking ~16s, or 55 inputs for HarfBuzz taking ~36s).Ports.clear_project_build('sdl2'), which callserase_file('.../libSDL2.a'). It attempts to acquirecache.lock('erase: ...'). Because Process B holds the cache lock, Process A blocks..alibrary to disk, logscache:INFO: - ok, and releasescache.lock.cache.lock. It checksif name.exists():. Because Process B just created it, the file exists! Process A logscache:INFO: deleting cached file: ...and deletes the newly built library.emccprocesses:emcc(e.g. compilingSDL_ttf.c) expectslibSDL2.ato exist. Since it was just deleted, the child tries to acquire the cache lock. Because it inheritedEM_CACHE_IS_LOCKED=1from Process B, it hits:AssertionError: attempt to lock the cache while a parent process is holding the lock.wasm-ldruns and fails with:wasm-ld: error: cannot open ...: No such file or directory.Why PR #27161 Did Not Fix It
PR #27161 unpacked into
.tmpand usedos.replaceto prevent other processes from seeing a partially unpacked directory. However, this race occurs after unpacking is 100% complete: the race window is betweenfetch_port_artifactreleasingcache.lockandfetch_projectcallingclear_project_build.This also explains why:
cache:INFO: deleting cached file:always appears immediately aftercache:INFO: - okacross all CI logs (Linux, macOS, Windows).retrieving port:norunpacking port:was logged whendeleting cached file:happened (the unpack happened earlier, before compilation started).Proposed Fix
Move
Ports.clear_project_build(name)insidefetch_port_artifactunderwith cache.lock('unpack port'):, immediately afterunpack():Because
cache.lockis re-entrant within the same process (tracked viaacquired_count),erase_fileexecutes safely under the already-held lock without releasing it. No other process can observe the port as up-to-date or acquire the cache lock untilclear_project_buildhas finished.[ports] Clear port build under cache lock to avoid deletion race (ems…