{"record":{"id":"7913e8c71d5ff074","repo":"siyuan-note/siyuan","slug":"flush-data-s-failed-s","errorCode":null,"errorMessage":"flush data [%s] failed: %s","messagePattern":"flush data \\[(.+?)\\] failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"kernel/util/mmap.go","lineNumber":64,"sourceCode":"\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":46,"sourceCodeEnd":69,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/mmap.go#L46-L69","documentation":"WriteFileByMmap memory-maps a file, copies data into the mapping, then calls m.Flush() (msync/msync-equivalent) to force the mapping back to disk. When the OS-level flush of the mapped pages fails (msync/FlushViewOfFile returns an error), the error is wrapped as \"flush data [%s] failed: %s\" and returned. This usually signals disk full, an I/O error, or the underlying file being truncated/removed externally between Map and Flush.","triggerScenarios":"Calling util.WriteFileByMmap(path, data) (used by saveAttributeView and treenode.WriteTree) when msync fails: disk full on the volume, underlying storage I/O error, file shrunk or deleted by another process after mapping, or mmap flush unsupported on the filesystem (e.g. some network filesystems).","commonSituations":"Disk quota exhausted while SiYuan writes attribute-view (.av) or .sy files; running the workspace on a NAS/network mount that fails msync; antivirus or sync tools (OneDrive/Dropbox) touching the file mid-write; failing disk sectors.","solutions":["Free disk space / resolve quota on the volume hosting the workspace and retry the write","Check the embedded OS error in the message (e.g. errno 28 ENOSPC, 5 EIO) and address that underlying cause","Move the workspace off unreliable network/overlay filesystems to a local disk","Let the caller fall back to filelock.WriteFile (the documented fallback path in the code comment) to complete the write","Check dmesg/Event Viewer for storage device errors and repair or replace failing hardware"],"exampleFix":"// before\ncopied := make([]byte, len(data))\nutil.WriteFileByMmap(path, copied) // fails on full disk, error dropped\n// after\nif err := util.WriteFileByMmap(path, data); err != nil {\n    logging.LogErrorf(\"mmap write failed, falling back: %s\", err)\n    if err2 := filelock.WriteFile(path, data); err2 != nil {\n        return fmt.Errorf(\"write %s failed: %w\", path, err2)\n    }\n}","handlingStrategy":"try-catch","validationCode":"// Go: check writable space and file health before mmap write\nif st, err := os.Stat(filepath.VolumeName(filePath) + string(os.PathSeparator)); err == nil {\n    _ = st // monitor free space via syscall.Statfs on POSIX\n}\nif fi, err := os.Stat(filePath); err == nil && fi.Size() != int64(len(data)) {\n    // WriteFileByMmap truncates to len(data); oversized shrink may indicate external tampering\n    logging.LogWarnf(\"file size %d != data size %d\", fi.Size(), len(data))\n}","typeGuard":null,"tryCatchPattern":"if err := util.WriteFileByMmap(path, data); err != nil {\n    if strings.Contains(err.Error(), \"flush data\") {\n        logging.LogErrorf(\"mmap flush failed for %s, retrying via filelock.WriteFile: %s\", path, err)\n        if fbErr := filelock.WriteFile(path, data); fbErr != nil {\n            return fmt.Errorf(\"persistent write failure for %s: %w\", path, fbErr)\n        }\n        return nil\n    }\n    return err\n}","preventionTips":["Monitor free disk space on the workspace volume and alert before it fills","Keep the workspace on a local, reliable filesystem rather than NAS/network mounts","Exclude the workspace from antivirus/sync tools that rewrite files during writes","Wrap calls with a non-mmap fallback (filelock.WriteFile) so a flush failure never loses data","Check the embedded OS error string to distinguish ENOSPC (disk full) from EIO (failing disk)"],"tags":["go","filesystem","mmap","disk-io"],"backgroundTag":"file-write-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"}