{"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":"critical","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/9f775e8a12daef8255556097396f9b2739078892/kernel/util/mmap.go#L37-L69","documentation":"After truncation, WriteFileByMmap maps the file descriptor into memory with mmap.Map(f, mmap.RDWR, 0). If mapping fails, it returns 'map file [...] failed: <os error>'. Mapping failures usually indicate OS-level limits or filesystem incompatibility rather than bad data.","triggerScenarios":"Calling WriteFileByMmap (via WriteTree or saveAttributeView) on a filesystem where mmap is unsupported or fails: some network filesystems, containers with restricted vm.max_map_count, files on FUSE mounts, or size zero / invalid file states.","commonSituations":"Workspace stored on cloud-synced or network drives that disallow mmap, containerized deployments hitting vm.max_map_count limits, unusual sandboxed environments (some mobile/VM setups).","solutions":["Check the embedded OS error: 'operation not permitted'/'invalid argument' -> move the workspace to a local filesystem that supports mmap","Raise vm.max_map_count (Linux) if the container/host exhausted map count","Stop sync/backup clients that may interfere with the file and retry the save","Fall back to plain file I/O if the environment cannot support mmap (patch or relocate workspace)"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"func mmapSupported(dir string) bool {\n    if runtime.GOOS == \"linux\" {\n        if b, err := os.ReadFile(\"/proc/sys/vm/max_map_count\"); err == nil {\n            n, _ := strconv.Atoi(strings.TrimSpace(string(b)))\n            return n > 65536\n        }\n    }\n    return runtime.GOOS != \"js\"\n}","typeGuard":null,"tryCatchPattern":"if err := util.WriteFileByMmap(path, data); err != nil {\n    if strings.Contains(err.Error(), \"map file\") {\n        log.Warnf(\"mmap unsupported here, falling back: %v\", err)\n        return os.WriteFile(path, data, 0644) // plain I/O fallback\n    }\n    return err\n}","preventionTips":["Host the workspace on a local filesystem that supports mmap","Raise vm.max_map_count on containerized Linux deployments","Avoid FUSE/network mounts for the SiYuan data directory","Implement a plain os.WriteFile fallback for restricted environments"],"tags":["filesystem","mmap","io","persistence"],"backgroundTag":"mmap-failed","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}