siyuan-note/siyuan · critical

map file [ ] failed

Error message

map file [%s] failed: %s

What it means

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.

Solutions

  1. Check the embedded OS error: 'operation not permitted'/'invalid argument' -> move the workspace to a local filesystem that supports mmap
  2. Raise vm.max_map_count (Linux) if the container/host exhausted map count
  3. Stop sync/backup clients that may interfere with the file and retry the save
  4. Fall back to plain file I/O if the environment cannot support mmap (patch or relocate workspace)
Defensive patterns

Strategy: fallback

Validate before calling

func mmapSupported(dir string) bool {
    if runtime.GOOS == "linux" {
        if b, err := os.ReadFile("/proc/sys/vm/max_map_count"); err == nil {
            n, _ := strconv.Atoi(strings.TrimSpace(string(b)))
            return n > 65536
        }
    }
    return runtime.GOOS != "js"
}

Try / catch

if err := util.WriteFileByMmap(path, data); err != nil {
    if strings.Contains(err.Error(), "map file") {
        log.Warnf("mmap unsupported here, falling back: %v", err)
        return os.WriteFile(path, data, 0644) // plain I/O fallback
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/94fbe248f8ca9d31. Report an issue: GitHub.

Appendix: source

Thrown at kernel/util/mmap.go:55

func WriteFileByMmap(filePath string, data []byte) (err error) {
	f, err := filelock.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0644)
	if err != nil {
		return
	}
	defer filelock.CloseFile(f)

	if err = f.Truncate(int64(len(data))); err != nil {
		msg := fmt.Sprintf("truncate file [%s] failed: %s", filePath, err)
		logging.LogError(msg)
		err = errors.New(msg)
		return
	}

	m, err := mmap.Map(f, mmap.RDWR, 0)
	if err != nil {
		msg := fmt.Sprintf("map file [%s] failed: %s", filePath, err)
		logging.LogError(msg)
		err = errors.New(msg)
		return
	}
	defer m.Unmap()

	copy(m, data)
	if err = m.Flush(); err != nil {
		msg := fmt.Sprintf("flush data [%s] failed: %s", filePath, err)
		logging.LogError(msg)
		err = errors.New(msg)
		return
	}
	return
}

View on GitHub (pinned to 9f775e8a12)