Repository navigation
[ports] Clear port build under cache lock to avoid deletion race - #27647
Merged
Merged
Conversation
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
brendandahl
approved these changes
Sep 2, 2026
Collaborator
Author
|
CC @ypujante who make this change originally. Hopefully the explanation of who the change to the |
Contributor
|
@sbc100 it has been a while but I think that does make sense. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In #24303,
Ports.fetch_projectwas refactored intoPorts.fetch_port_artifactandPorts.clear_project_build. In thatrefactoring,
Ports.clear_project_buildwas moved outside thecache.lock('unpack port')block.When parallel processes both require the same port on a cold cache:
cache.lock('unpack port').build the port library, and acquires
cache.lock.Ports.clear_project_build, which blocks on thecache lock while Process B compiles the library.
acquires the lock and deletes the newly compiled library.
Fix this by reverting the split between
fetch_projectandfetch_port_artifactfrom #24303, and performingPorts.clear_project_buildinsidefetch_projectwhile still holdingthe unpack cache lock.
Also revert
get_port_by_nameback toports_by_nameinPorts.clear_project_buildso it does not trigger recursion duringexternal port loading, and mock
clearininit_external_port.Note that external ports no longer have their loaded module's
.clear()method called when updated, but this is safe because:
Ports.clear_project_buildstill deletes the intermediate builddirectory (
build/<name>) under the unpack lock.clear()to delete static library filenames(like
libz.aorlibSDL2.a) from the cache, but external ports(
contrib.glfw3andemdawnwebgpu) incorporate either the releasetag or a content hash of source inputs into their library filenames
(e.g.
lib_emscripten-glfw3_<TAG>...andlibemdawnwebgpu-<hash>...). When updated, the library filenamechanges, ensuring the new library is built on first use.
.clear()on the newly unpacked port module wouldonly attempt to erase the new library name (which does not exist yet)
and would never have deleted the previous version's library anyway.
embuilder clear <port>orclear_port()continues to resolve and invoke
local_port.clear()as before.Fixes: #27160