Skip to content

Intermittent bot failures due EM_CACHE_IS_LOCKED building ports with libsdl2-ttf #27160

Closed

Description

@sbc100

These failures seem to occur when building libsdl2-ttf, and have been seen on both mac and windows builders:

For example:

https://ci.chromium.org/ui/p/emscripten-releases/builders/try/win/b8678860304918941313/overview

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.

Activity

  1. sbc100 commented on Jun 22, 2026

    @sbc100
    CollaboratorAuthor

    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.

  2. sbc100 commented on Jun 23, 2026

    @sbc100
    CollaboratorAuthor

    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:

    1. Process A starts building sdl2_ttf . It successfully unpacks sdl2 (writing the .emscripten_url marker) and builds libSDL2.a .
    2. 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.
    3. Process B (running in parallel) acquires the lock after Process A releases it momentarily. It checks if sdl2 is up-to-date.
    4. 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.
    5. The Destruction: Process B calls clear_project_build('sdl2') , which deletes libSDL2.a from the cache.
    6. The Collision: Process A's child emcc (compiling SDL_ttf.c ) needs sdl2 . It sees libSDL2.a is missing and tries to rebuild it.
    7. 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.

  3. sbc100 commented on Jun 23, 2026

    @sbc100
    CollaboratorAuthor

    This just showed up on the linux CI https://logs.chromium.org/logs/emscripten-releases/buildbucket/cr-buildbucket/8678253928542712369/+/u/Emscripten_testsuite__other_/stdout

    cache:INFO: deleting cached file: /b/s/w/ir/x/w/install/emscripten/cache/sysroot/lib/wasm32-emscripten/libharfbuzz.a
    cache:INFO: generating port: sysroot/lib/wasm32-emscripten/libharfbuzz.a... (this will be cached in "/b/s/w/ir/x/w/install/emscripten/cache/sysroot/lib/wasm32-emscripten/libharfbuzz.a" for subsequent builds)
    wasm-ld: error: cannot open /b/s/w/ir/x/w/install/emscripten/cache/sysroot/lib/wasm32-emscripten/libharfbuzz.a: No such file or directory
    emcc: error: '/b/s/w/ir/x/w/install/bin/wasm-ld -o a.out.wasm /b/s/w/ir/x/t/emtest_t0uxqmot/tmpcdridmqqlibemscripten_js_symbols.so -Bstatic --strip-debug --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_get_current --export=emscripten_stack_init --export=_emscripten_stack_alloc --export=__wasm_call_ctors --export=_emscripten_stack_restore --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=main --export-if-defined=__main_argc_argv --export-if-defined=fflush --export-table -z stack-size=65536 --no-growable-memory --initial-heap=16777216 --no-entry --stack-first --table-base=1 /b/s/w/ir/x/t/emtest_t0uxqmot/emscripten_temp_k3hokhil/test_sdl2_ttf.o -L/b/s/w/ir/x/w/install/emscripten/cache/sysroot/lib/wasm32-emscripten -L/b/s/w/ir/x/w/install/emscripten/src/lib /b/s/w/ir/x/w/install/emscripten/cache/sysroot/lib/wasm32-emscripten/libSDL2_ttf.a /b/s/w/ir/x/w/install/emscripten/cache/sysroot/lib/wasm32-emscripten/libharfbuzz.a /b/s/w/ir/x/w/install/emscripten/cache/sysroot/lib/wasm32-emscripten/libSDL2.a /b/s/w/ir/x/w/install/emscripten/cache/sysroot/lib/wasm32-emscripten/libfreetype.a /b/s/w/ir/x/w/install/emscripten/cache/sysroot/lib/wasm32-emscripten/libz.a -lGL-getprocaddr -lal -lhtml5 -lstubs-debug -lnoexit -lc-debug -ldlmalloc-debug -lclang_rt.builtins -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr' failed (returned 1)
    None
    None
    [533/1647] test_sdl2_ttf (test_other.other.test_sdl2_ttf) ... FAIL
    

    I think anther test is doing deleting cached file while test_sdl2_ttf` is running.

    So we know it is reproducible on linux

  4. sbc100 commented on Sep 1, 2026

    @sbc100
    CollaboratorAuthor

    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 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:

      @staticmethod
      def fetch_port_artifact(name, url, sha512hash=None):
        ...
        with cache.lock('unpack port'):
          if os.path.exists(fullpath):
            if up_to_date():
              return True
            ...
          retrieve()
          unpack()
    
          return False  # Exits `with cache.lock('unpack port')` -> releases lock!
    
      @staticmethod
      def fetch_project(name, url, sha512hash=None):
        if not Ports.fetch_port_artifact(name, url, sha512hash):
          # we unpacked a new version, clear the build in the cache
          Ports.clear_project_build(name)  # CALLED OUTSIDE THE LOCK!

    Inside Ports.clear_project_build(name):

    1. port.clear() is called, which calls cache.erase_lib(...).
    2. erase_lib calls erase_file(...), which acquires with cache.lock('erase: ' + shortname):.
    3. 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

    1. Process A runs fetch_project('sdl2'). It acquires cache.lock('unpack port'), downloads, and unpackages the port.
    2. Process A exits with cache.lock('unpack port') and returns False. The global cache lock is released.
    3. 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.
    4. 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).
    5. 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.
    6. Process B finishes compilation, writes the .a library to disk, logs cache:INFO: - ok, and releases cache.lock.
    7. 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.
    8. 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.

    Why PR #27161 Did Not Fix It

    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) inside fetch_port_artifact under with cache.lock('unpack port'):, immediately after unpack():

        with cache.lock('unpack port'):
          if os.path.exists(fullpath):
            if up_to_date():
              return True
            logger.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 it
          Ports.clear_project_build(name)
    
          return False

    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.

  5. added a commit that references this issue on Sep 2, 2026
    d01f0ed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions