Skip to content

[ports] Clear port build under cache lock to avoid deletion race - #27647

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:fix_port_clear_race
Sep 2, 2026
Merged

sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:fix_port_clear_race

Conversation

@sbc100

@sbc100 sbc100 commented Sep 2, 2026 •

Copy link
Copy Markdown
Collaborator

In #24303, Ports.fetch_project was refactored into
Ports.fetch_port_artifact and Ports.clear_project_build. In that
refactoring, Ports.clear_project_build was moved outside the
cache.lock('unpack port') block.

When parallel processes both require the same port on a cold cache:

  1. Process A unpacks the port and releases cache.lock('unpack port').
  2. Process B sees the port is now unpacked and up-to-date, proceeds to
    build the port library, and acquires cache.lock.
  3. Process A calls Ports.clear_project_build, which blocks on the
    cache lock while Process B compiles the library.
  4. When Process B finishes compiling and releases the lock, Process A
    acquires the lock and deletes the newly compiled library.

Fix this by reverting the split between fetch_project and
fetch_port_artifact from #24303, and performing
Ports.clear_project_build inside fetch_project while still holding
the unpack cache lock.

Also revert get_port_by_name back to ports_by_name in
Ports.clear_project_build so it does not trigger recursion during
external port loading, and mock clear in init_external_port.

Note that external ports no longer have their loaded module's .clear()
method called when updated, but this is safe because:

  1. Ports.clear_project_build still deletes the intermediate build
    directory (build/<name>) under the unpack lock.
  2. In-tree ports rely on clear() to delete static library filenames
    (like libz.a or libSDL2.a) from the cache, but external ports
    (contrib.glfw3 and emdawnwebgpu) incorporate either the release
    tag or a content hash of source inputs into their library filenames
    (e.g. lib_emscripten-glfw3_<TAG>... and
    libemdawnwebgpu-<hash>...). When updated, the library filename
    changes, ensuring the new library is built on first use.
  3. In fact, calling .clear() on the newly unpacked port module would
    only attempt to erase the new library name (which does not exist yet)
    and would never have deleted the previous version's library anyway.
  4. Explicit clearing via embuilder clear <port> or clear_port()
    continues to resolve and invoke local_port.clear() as before.

Fixes: #27160

In emscripten-core#24303, `Ports.fetch_project` was refactored into
`Ports.fetch_port_artifact` and `Ports.clear_project_build`. In that
refactoring, `Ports.clear_project_build` was moved outside the
`cache.lock('unpack port')` block.

When parallel processes both require the same port on a cold cache:
1. Process A unpacks the port and releases `cache.lock('unpack port')`.
2. Process B sees the port is now unpacked and up-to-date, proceeds to
   build the port library, and acquires `cache.lock`.
3. Process A calls `Ports.clear_project_build`, which blocks on the
   cache lock while Process B compiles the library.
4. When Process B finishes compiling and releases the lock, Process A
   acquires the lock and deletes the newly compiled library.

Fix this by reverting the split between `fetch_project` and
`fetch_port_artifact` from emscripten-core#24303, and performing
`Ports.clear_project_build` inside `fetch_project` while still holding
the unpack cache lock.

Also revert `get_port_by_name` back to `ports_by_name` in
`Ports.clear_project_build` so it does not trigger recursion during
external port loading, and mock `clear` in `init_external_port`.

Fixes: emscripten-core#27160
@sbc100

sbc100 commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator Author

CC @ypujante who make this change originally. Hopefully the explanation of who the change to the .clear() behaviour is safe makes sense?

@sbc100
sbc100 merged commit d01f0ed into emscripten-core:main Sep 2, 2026
7 checks passed
@sbc100
sbc100 deleted the fix_port_clear_race branch September 2, 2026 22:32
@ypujante

ypujante commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

@sbc100 it has been a while but I think that does make sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Intermittent bot failures due EM_CACHE_IS_LOCKED building ports with libsdl2-ttf

3 participants