Repository navigation
Description
Currently there is trouble with multiprocess compilation from make -j4 and similar multiprocess compilation drivers, and independently compiling multiple projects. If there's something that needs to be written to ~/.emscripten_cache, then things fail horribly. STR on pretty much any project:
"emcmake cmake someProject", or, "emconfigure ./configure"
emcc --clear-cache
make -j4
This will cause all emccs to start populating the cache on their own, and then most of the time, randomly failing somewhere e.g. on trying to write-open a file that is already write-open elsewhere, when each thread races to populate the cache, and build optimizer on demand, and so on. A workaround is to know when the cache is dirty, and do a manual singlethreaded build once, and only after that start doing multiprocess builds when one knows the cache is up-to-date, but it's not very nice.
I think we should implement a file-based locking scheme for accesses to the cache, where we'd have each writer to the cache acquire a write lock, and each reader to the cache acquire a read lock. There could be arbitrary number of read locks allowed at any time, but acquiring a write lock would prohibit any reads for the duration of the write lock (and write lock should wait for all read locks to be released).
Started googling a bit for this kind of utility for python, but can't really find anything. There are a lot of resources for simple single-access locking to a file, which would not be good here, since it would mean only one thread can access the whole cache directory at once. E.g. http://amix.dk/blog/post/19531 and http://tilde.town/~cristo/file-locking-in-python.html describe single file locking.
If nothing else, we could do a custom solution:
- use a single file to represent the write lock, and a directory which can contain an arbitrary number of files, which each would be a read lock. When obtaining the write lock, we would have to check that there are no files in the read lock directory, and when obtaining a new read lock, we would have to check that the write lock file doesn't exist.
- the contents of each file should be a process ID, so that we can detect killed and stale processes and clean up lock files that may have left over from random reasons (and final failsafe "could not acquire read/write -lock" error out)
- and lot of figuring out how to properly do atomic operations on the filesystem to be race-free.
If there was a readymade solution for python single-write, multiple-read lock for multiprocessing, that would be cool so we wouldn't have to start working through the potential quirks on our own.
Activity
kripken commentedon Oct 13, 2015
The problem is that this is very hard to do properly and especially in a cross-platform manner. It might be easier to catch errors like failing to read or write to the cache, and showing an error about too many processes accessing it at once, and to suggest removing
-j.Implement a file-based lock mechanism to access the Emscripten cache. C…
Implement a file-based lock mechanism to access the Emscripten cache. C…
juj commentedon Jun 19, 2016
Realized that if we assume that during a compilation run the cache will never be cleared once it is initially populated, we don't actually need a multiple reader - single writer cache, but a simple file-based mutex lock is enough (as long as the lock is extended to cover the decision making whether the item in cache exists or should be built). That seems like a reasonable assumption, since the only way to violate it would be to edit ~/.emscripten while a build is running, or do
emcc --clear-cacheoremsdk install xxxwhile a build is running, which each are cold setup time operations.I do have an experimental multiple reader - single writer cache implementation based on the above PR, in case that will one day be necessary, but left it out from the PR since it's not needed here.
juj commentedon Jun 29, 2016
Fixed by above PR.