@@ -141,16 +141,19 @@ Error XNNWeightsCache::initialize_for_runtime(
141141 return Error::Ok;
142142 }
143143
144- // Already loaded earlier this session; just reopen the write fd that
145- // save_packed_index() closed. Subsequent reserve_space can extend the
146- // file for any entries not in the saved trailer.
147- if (cache_loaded_) {
144+ // Entries already in memory (from a prior load_packed_cache or a prior
145+ // fresh-write session). Just reopen the write fd that save_packed_index
146+ // closed; subsequent reserve_space can extend the file. Using metadata
147+ // emptiness (not a separate flag) as the gate avoids a latent bug
148+ // where fresh-write→save→re-init re-enters load_packed_cache and
149+ // double-mmaps the same file.
150+ if (!name_to_packed_data_metadata_.empty ()) {
148151 packed_file_fd_ = open_locked (packed_cache_path_, O_RDWR );
149152 return Error::Ok;
150153 }
151154
152- // First init for this path : try to load the saved trailer; on success
153- // open a write fd for any new entries. If load fails, fall through to
155+ // No in-memory entries : try to load the saved trailer; on success open
156+ // a write fd for any new entries. If load fails, fall through to
154157 // fresh-write below.
155158 if (load_packed_cache ()) {
156159 ET_LOG (
@@ -280,7 +283,6 @@ void XNNWeightsCache::full_unload() {
280283 packed_data_ptrs_.clear ();
281284 ptr_to_file_offset_.clear ();
282285 file_ptr_to_region_index_.clear ();
283- cache_loaded_ = false ;
284286 if (packed_file_fd_ >= 0 ) {
285287 close (packed_file_fd_);
286288 packed_file_fd_ = -1 ;
@@ -721,7 +723,6 @@ bool XNNWeightsCache::load_packed_cache() {
721723 name_to_packed_data_metadata_[name] = meta;
722724 }
723725
724- cache_loaded_ = true ;
725726 packed_file_used_ = index_start;
726727 // In-memory state matches the on-disk trailer; the next save would be
727728 // a no-op. Initialize watermark so save_packed_index short-circuits.
0 commit comments