diff --git a/src/experimental/mjz/mjz_decoder.cc b/src/experimental/mjz/mjz_decoder.cc index ee29a80f..12fc0ba5 100644 --- a/src/experimental/mjz/mjz_decoder.cc +++ b/src/experimental/mjz/mjz_decoder.cc @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -141,6 +142,9 @@ class ZipArchiveProvider : public mjpResourceProvider { FileInfo& info = it->second; // Lazily read and store the file contents from the archive. + // The mutex is needed because mz_zip_archive is not thread-safe, and + // multiple threads may call Read concurrently during parallel compilation. + std::lock_guard lock(mutex_); if (info.contents.empty()) { info.contents.resize(info.size); if (!mz_zip_reader_extract_to_mem(&archive_, info.index, @@ -168,6 +172,7 @@ class ZipArchiveProvider : public mjpResourceProvider { mz_zip_archive archive_; std::vector buffer_; std::unordered_map files_; + mutable std::mutex mutex_; }; static mjSpec* ParseZipBuffer(const void* buffer, int nbuffer, const char* name,