@@ -357,12 +357,12 @@ struct llama_mmap::impl {
357357 LLAMA_LOG_INFO (" mmap(%s) desire=%p size=%llu result=%p is_new_mem[%d]=%s\n " ,
358358 path, (void *)address, GGML_MMAP_HUGEPAGESZ, mm, node, is_new_mem[node] ? " yes" : " no" );
359359
360- // Store mapping info for cleanup BEFORE checking for errors
361- if (mm != MAP_FAILED) {
362- numa_mappings.push_back ({mm, GGML_MMAP_HUGEPAGESZ, std::string (path)});
363- }
364-
365360 if (((uintptr_t )mm) != address) {
361+ // If mmap failed completely, delete the file we just created
362+ if (mm == MAP_FAILED) {
363+ unlink (path);
364+ }
365+
366366 // Clean up any mappings we've already created before throwing
367367 for (const auto & mapping : numa_mappings) {
368368 munmap (mapping.addr , mapping.size );
@@ -371,6 +371,10 @@ struct llama_mmap::impl {
371371 LLAMA_LOG_WARN (" unable to mmap memory: %d %s\n " , errno, strerror (errno));
372372 throw std::runtime_error (format (" mmap failed: %s" , strerror (errno)));
373373 }
374+
375+ // Only store valid mappings
376+ numa_mappings.push_back ({mm, GGML_MMAP_HUGEPAGESZ, std::string (path)});
377+
374378 if (is_new_mem[node]) {
375379 memset (mm, 0 , GGML_MMAP_HUGEPAGESZ);
376380 }
0 commit comments