{"record":{"id":"94fbe248f8ca9d31","repo":"siyuan-note/siyuan","slug":"map-file-s-failed-s","errorCode":null,"errorMessage":"map file [%s] failed: %s","messagePattern":"map file \\[(.+?)\\] failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/mmap.go","lineNumber":55,"sourceCode":"func WriteFileByMmap(filePath string, data []byte) (err error) {\n\tf, err := filelock.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0644)\n\tif err != nil {\n\t\treturn\n\t}\n\tdefer filelock.CloseFile(f)\n\n\tif err = f.Truncate(int64(len(data))); err != nil {\n\t\tmsg := fmt.Sprintf(\"truncate file [%s] failed: %s\", filePath, err)\n\t\tlogging.LogError(msg)\n\t\terr = errors.New(msg)\n\t\treturn\n\t}\n\n\tm, err := mmap.Map(f, mmap.RDWR, 0)\n\tif err != nil {\n\t\tmsg := fmt.Sprintf(\"map file [%s] failed: %s\", filePath, err)\n\t\tlogging.LogError(msg)\n\t\terr = errors.New(msg)\n\t\treturn\n\t}\n\tdefer m.Unmap()\n\n\tcopy(m, data)\n\tif err = m.Flush(); err != nil {\n\t\tmsg := fmt.Sprintf(\"flush data [%s] failed: %s\", filePath, err)\n\t\tlogging.LogError(msg)\n\t\terr = errors.New(msg)\n\t\treturn\n\t}\n\treturn\n}\n","sourceCodeStart":37,"sourceCodeEnd":69,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/util/mmap.go#L37-L69","documentation":"WriteFileByMmap guard: mmap.Map of the truncated file failed (memory exhaustion, address-space limits, or filesystem lacking mmap support). The mmap fast-path write of attribute-view or tree data cannot proceed; the raw OS error is included.","triggerScenarios":"Called via SaveAttributeView or WriteTree when the file descriptor is invalid, the file was truncated to zero then mapping fails, the OS hits a mapping limit (vm.max_map_count exhausted, out of address space on 32-bit), or the file is on a filesystem that does not support mmap.","commonSituations":"Long-running kernel exhausting mmap regions after many saves (Linux vm.max_map_count); network/SMB/NFS mounts where mmap is unreliable; 32-bit builds on mobile platforms mapping large .av files.","solutions":["Read the wrapped OS error for the concrete mmap failure reason","On Linux, raise vm.max_map_count (sysctl -w vm.max_map_count=262144) or restart the kernel to release stale mappings","Avoid placing the workspace on network shares that do not support memory-mapped files","If the error persists, check free virtual address space / use a 64-bit kernel build"],"exampleFix":"// before\nsudo sysctl vm.max_map_count // 65530, exhausted\n// after\nsudo sysctl -w vm.max_map_count=262144\n# restart the SiYuan kernel and retry the save","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := util.WriteFileByMmap(path, data); err != nil {\n    if strings.Contains(err.Error(), \"map file\") {\n        // fall back to a plain os.WriteFile path or alert the user\n    }\n}","preventionTips":["Run a 64-bit kernel build for large attribute views","Avoid SMB/NFS mounts for the workspace","Raise vm.max_map_count on Linux servers with heavy save traffic","Restart long-running kernels periodically to release mappings"],"tags":["go","file-io","mmap","filesystem"],"backgroundTag":"file-write-failed","analyzedSha":"8641553a1f07374001902d3ce773285db1292b2d","analyzedAt":"2026-09-11T16:08:28.414Z","contentChangedAt":"2026-09-11T16:08:28.414Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}