From a2cd7aa52fad288e677ea1a06ebc82d90e1f2f74 Mon Sep 17 00:00:00 2001 From: Sam Haves Date: Wed, 29 Apr 2026 07:18:34 -0700 Subject: [PATCH] Make mjz_decoder threadsafe. PiperOrigin-RevId: 907574530 Change-Id: Ie292ff10dfa515e4ddc9dad1459e44c3ba9eeabe --- src/experimental/mjz/mjz_decoder.cc | 5 +++++ 1 file changed, 5 insertions(+) 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,