shadow1ng/fscan · error

failed to sync realtime backup: %w

Error message

failed to sync realtime backup: %w

What it means

Returned when fsync on the realtime backup file fails after a successful WriteString. The library calls Sync() on every write to guarantee crash-safe persistence of scan results, so any flush-to-disk failure is reported. It wraps the underlying OS error.

Source

Thrown at common/output/writers.go:140

	if w.closed {
		return fmt.Errorf("writer is closed")
	}
	if result == nil {
		return fmt.Errorf("result cannot be nil")
	}

	// 1. 加入内存分类缓冲(用于最终有序输出)
	w.buffer.Add(result)

	// 2. 实时写入备份文件(防崩溃丢数据)
	if w.realtimeFile != nil {
		line := w.formatLine(result)
		if _, err := w.realtimeFile.WriteString(line + "\n"); err != nil {
			return fmt.Errorf("failed to write realtime backup: %w", err)
		}
		if err := w.realtimeFile.Sync(); err != nil {
			return fmt.Errorf("failed to sync realtime backup: %w", err)
		}
	}

	return nil
}

// getSeparator 获取分隔线文本
func (w *TXTWriter) getSeparator(newType ResultType) string {
	switch newType {
	case TypeHost:
		return i18n.GetText("output_section_hosts")
	case TypePort:
		return i18n.GetText("output_section_ports")
	case TypeService:
		return i18n.GetText("output_section_services")
	case TypeVuln:
		return i18n.GetText("output_section_vulns")
	default:

View on GitHub (pinned to 95cc12e753)

Solutions

  1. Check dmesg/system logs for underlying disk I/O errors
  2. Verify available disk space and volume health
  3. If writing to a network filesystem, move output to local disk or accept weaker durability
  4. Inspect the wrapped OS error via errors.Is/Unwrap to confirm the cause
  5. Retry the write or restart the writer after the storage issue is fixed

Example fix

// before
if err := w.realtimeFile.Sync(); err != nil {
	return fmt.Errorf("failed to sync realtime backup: %w", err)
}
// after
if err := w.realtimeFile.Sync(); err != nil {
	if isNetworkFS(w.realtimePath) { // tolerate fsync on NFS-like mounts
		return nil
	}
	return fmt.Errorf("failed to sync realtime backup: %w", err)
}
Defensive patterns

Strategy: try-catch

Validate before calling

info, err := os.Stat(outDir)
if err != nil || !info.IsDir() { return fmt.Errorf("output dir missing") }
if unix.Access(outDir, unix.W_OK) != nil { return fmt.Errorf("output dir not writable") }

Try / catch

if err := writer.Write(result); err != nil {
	if strings.Contains(err.Error(), "failed to sync realtime backup") {
		log.Printf("fsync failed; durability compromised: %v", err)
	}
	return err
}

Prevention

When it happens

Trigger: Calling Write() on a writer with realtimeFile != nil where w.realtimeFile.Sync() returns an error: fsync failing on disk full, I/O error, or on certain filesystems/network mounts that do not support fsync.

Common situations: Output written to NFS or FUSE mounts with unreliable fsync; disk failure or ENOSPC surfacing only at sync time; container with restricted I/O on the volume.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of shadow1ng/fscan@95cc12e753 (2026-09-06). Data as JSON: /api/errors/63c5dd1b71168469. Report an issue: GitHub.