From b58defbe8bdb2dcdffd982b5d23750f94c91451a Mon Sep 17 00:00:00 2001 From: cmgCr Date: Fri, 20 Mar 2026 09:19:36 +0000 Subject: [PATCH 1/3] fix(runtime): remove /dev/shm mmap file immediately after open --- src/runtime/memory.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/runtime/memory.cpp b/src/runtime/memory.cpp index 8ab830d37..637eaedf2 100644 --- a/src/runtime/memory.cpp +++ b/src/runtime/memory.cpp @@ -101,6 +101,13 @@ WasmMemoryAllocator::WasmMemoryAllocator( Stats.revertRecord(Timer); goto try_use_mmap_init; } + // Remove the directory entry while keeping the fd open. On POSIX, + // remove() on a regular file is equivalent to unlink(): it deletes the + // name from the filesystem but the file data remains accessible through + // any open fd or mmap. The kernel reclaims the storage automatically + // when the last fd/mmap reference is closed — even on abnormal exit + // (SIGKILL, crash). This prevents /dev/shm file accumulation. + ::remove(Path); MmapMemoryFilepath = ::strdup(Path); DefaultMemoryType = WM_MEMORY_DATA_TYPE_BUCKET_MMAP; MmapMemoryBucketGrowMaxSize = MmapMemoryFileMaxSize; @@ -183,12 +190,6 @@ WasmMemoryAllocator::~WasmMemoryAllocator() { } if (MmapMemoryInitFd > 0) { ::close(MmapMemoryInitFd); - // delete the memory file forcely - int Status = ::remove(MmapMemoryFilepath); - if (Status != 0) { - ZEN_LOG_WARN("failed to remove mmap tmp memory file %s due to '%s'", - MmapMemoryFilepath, std::strerror(errno)); - } } if (MmapMemoryFilepath) { ::free(MmapMemoryFilepath); From ad7ae0a5169c444f41f788c06ddb819b79be0ac9 Mon Sep 17 00:00:00 2001 From: cmgCr Date: Fri, 20 Mar 2026 19:16:12 +0800 Subject: [PATCH 2/3] fix(compiler): disable mmap-bucket on early-unlink failure --- src/runtime/memory.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/runtime/memory.cpp b/src/runtime/memory.cpp index 637eaedf2..aa4d8746a 100644 --- a/src/runtime/memory.cpp +++ b/src/runtime/memory.cpp @@ -107,7 +107,16 @@ WasmMemoryAllocator::WasmMemoryAllocator( // any open fd or mmap. The kernel reclaims the storage automatically // when the last fd/mmap reference is closed — even on abnormal exit // (SIGKILL, crash). This prevents /dev/shm file accumulation. - ::remove(Path); + if (::remove(Path) != 0) { + ZEN_LOG_WARN("failed to early-unlink mmap file %s due to '%s', " + "disabling mmap-bucket for this module", + Path, std::strerror(errno)); + ::close(MmapFileFd); + MmapFileFd = -1; + UseMmapBucket = false; + Stats.revertRecord(Timer); + goto try_use_mmap_init; + } MmapMemoryFilepath = ::strdup(Path); DefaultMemoryType = WM_MEMORY_DATA_TYPE_BUCKET_MMAP; MmapMemoryBucketGrowMaxSize = MmapMemoryFileMaxSize; From 979975073c621a7b7d13cdc608dd05e2bdadb6f0 Mon Sep 17 00:00:00 2001 From: cmgCr Date: Fri, 27 Mar 2026 15:56:33 +0800 Subject: [PATCH 3/3] fix(runtime): use -1 as invalid fd sentinel for MmapMemoryInitFd --- src/runtime/memory.cpp | 4 ++-- src/runtime/memory.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/runtime/memory.cpp b/src/runtime/memory.cpp index aa4d8746a..e80718a42 100644 --- a/src/runtime/memory.cpp +++ b/src/runtime/memory.cpp @@ -197,7 +197,7 @@ WasmMemoryAllocator::~WasmMemoryAllocator() { } delete MmapAddresses; } - if (MmapMemoryInitFd > 0) { + if (MmapMemoryInitFd >= 0) { ::close(MmapMemoryInitFd); } if (MmapMemoryFilepath) { @@ -216,7 +216,7 @@ bool WasmMemoryAllocator::checkWasmMemoryCanUseMmap() { bool CanUseMmap = false; if (UseMmap) { if (DefaultMemoryType == WM_MEMORY_DATA_TYPE_BUCKET_MMAP && - MmapMemoryInitFd > 0) { + MmapMemoryInitFd >= 0) { CanUseMmap = true; } } diff --git a/src/runtime/memory.h b/src/runtime/memory.h index 100999711..e3f546067 100644 --- a/src/runtime/memory.h +++ b/src/runtime/memory.h @@ -125,7 +125,7 @@ class WasmMemoryAllocator { // when grow to not larger then it, just inc the size. // otherwise mmap another copy and copy data size_t MmapMemoryBucketGrowMaxSize = 0; - int MmapMemoryInitFd = 0; // when <=0, means not create memory file + int MmapMemoryInitFd = -1; // when <0, means not create memory file char *MmapMemoryFilepath = nullptr; // use pointers so when MMAP_BUCKET enabled but the module has no memory, // then no need to create the objects