@@ -338,6 +338,11 @@ struct llama_mmap::impl {
338338 }
339339 int hugefd = open (path, O_CREAT | O_RDWR, 0600 );
340340 if (hugefd < 0 ) {
341+ // Clean up any mappings we've already created before throwing
342+ for (const auto & mapping : numa_mappings) {
343+ munmap (mapping.addr , mapping.size );
344+ unlink (mapping.path .c_str ());
345+ }
341346 LLAMA_LOG_WARN (" failed to open hugepage fd %s: %d %s\n " ,
342347 path, errno, strerror (errno));
343348 throw std::runtime_error (format (" failed to open hugepage fd: %s" , strerror (errno)));
@@ -351,6 +356,12 @@ struct llama_mmap::impl {
351356 close (hugefd);
352357 LLAMA_LOG_INFO (" mmap(%s) desire=%p size=%llu result=%p is_new_mem[%d]=%s\n " ,
353358 path, (void *)address, GGML_MMAP_HUGEPAGESZ, mm, node, is_new_mem[node] ? " yes" : " no" );
359+
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+
354365 if (((uintptr_t )mm) != address) {
355366 // Clean up any mappings we've already created before throwing
356367 for (const auto & mapping : numa_mappings) {
@@ -363,9 +374,6 @@ struct llama_mmap::impl {
363374 if (is_new_mem[node]) {
364375 memset (mm, 0 , GGML_MMAP_HUGEPAGESZ);
365376 }
366-
367- // Store mapping info for cleanup
368- numa_mappings.push_back ({mm, GGML_MMAP_HUGEPAGESZ, std::string (path)});
369377 }
370378 }
371379 base_address_offset += i * GGML_MMAP_HUGEPAGESZ;
0 commit comments