Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/runtime/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ 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.
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;
Expand Down Expand Up @@ -181,14 +197,8 @@ WasmMemoryAllocator::~WasmMemoryAllocator() {
}
delete MmapAddresses;
}
if (MmapMemoryInitFd > 0) {
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));
}
}
Comment on lines 199 to 202
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MmapMemoryInitFd uses 0 as the “invalid” sentinel, and the destructor (and checkWasmMemoryCanUseMmap()) only close/accept the fd when > 0. Since POSIX open() can legally return fd 0 (e.g., if stdin is closed), this can leak the fd and—now that the file is unlinked—prevent tmpfs storage from being reclaimed until process exit. Use -1 as the sentinel and treat >= 0 as valid in both the close and the checkWasmMemoryCanUseMmap() logic.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update in new commit 9799750

if (MmapMemoryFilepath) {
::free(MmapMemoryFilepath);
Expand All @@ -206,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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading