golangci/golangci-lint · error

can't write heap profile: %w

Error message

can't write heap profile: %w

What it means

golangci-lint wraps the error returned by pprof.WriteHeapProfile when dumping the heap profile to the --mem-profile-path file created moments earlier. Since the file was already created successfully, this is usually an I/O failure during the write itself (disk full, I/O error, closed fd).

Source

Thrown at pkg/commands/run.go:310

}

func (c *runCommand) stopTracing() error {
	if c.opts.CPUProfilePath != "" {
		pprof.StopCPUProfile()
	}

	if c.opts.MemProfilePath != "" {
		f, err := os.Create(c.opts.MemProfilePath)
		if err != nil {
			return fmt.Errorf("can't create file %s: %w", c.opts.MemProfilePath, err)
		}

		var ms runtime.MemStats
		runtime.ReadMemStats(&ms)
		printMemStats(&ms, c.log)

		if err := pprof.WriteHeapProfile(f); err != nil {
			return fmt.Errorf("can't write heap profile: %w", err)
		}
		_ = f.Close()
	}

	if c.opts.TracePath != "" {
		trace.Stop()
	}

	return nil
}

func (c *runCommand) runAndPrint(ctx context.Context) error {
	if err := c.goenv.Discover(ctx); err != nil {
		c.log.Warnf("Failed to discover go env: %s", err)
	}

	if !logutils.HaveDebugTag(logutils.DebugKeyLintersOutput) {
		// Don't allow linters and loader to print anything

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Free disk space on the volume holding --mem-profile-path and re-run.
  2. Write the profile to a different, reliable local disk (e.g. /tmp) instead of a network mount.
  3. Check dmesg/storage health if I/O errors persist.
  4. Re-run the command; transient write failures often clear on retry.

Example fix

// before
--mem-profile-path /mnt/nfs-share/mem.out   # nfs write failed: no space left
// after
--mem-profile-path /tmp/mem.out
Defensive patterns

Strategy: try-catch

Validate before calling

if st, err := os.Stat(filepath.Dir(memPath)); err != nil || !st.IsDir() {
	return fmt.Errorf("profile dir missing")
}
// roughly ensure free space
if usage := diskFreeBytes("/tmp"); usage < 64<<20 {
	return fmt.Errorf("not enough free space for heap profile")
}

Try / catch

if err := cmd.Run(); err != nil && strings.Contains(err.Error(), "can't write heap profile") {
	log.Printf("heap profile write failed (disk full or I/O error); rerun after freeing space: %v", err)
}

Prevention

When it happens

Trigger: Disk full or underlying storage error while pprof serializes the heap profile; file descriptor closed/invalidated between os.Create and WriteHeapProfile; rare pprof-internal serialization failures.

Common situations: CI machines with nearly full disks; ephemeral filesystems with tiny quotas; NFS/network volumes dropping connections mid-write.

Related errors


AI-assisted analysis of golangci/golangci-lint@ed7a235d2d (2026-09-02). Data as JSON: /api/errors/6bac4767d3483639. Report an issue: GitHub.