grafana/k6 · error

closing the local file

Error message

closing the local file %q: %w

What it means

Raised by the deferred close in LocalFilePersister.Persist when the created file ('%q') fails to close AND no earlier error occurred — the guard `cerr != nil && err == nil` promotes the close failure to the returned error. This fires on resource-level failures at close time (e.g. flush-to-disk errors on some filesystems, NFS quirks, or fd exhaustion), meaning write data may or may not be fully durable even though the copy itself reported success.

Solutions

  1. Check for file descriptor exhaustion in long-running test processes
  2. Verify the storage backend (especially network filesystems) is healthy
  3. Retry the persist operation; the file is recreated with O_TRUNC
  4. Check disk space and filesystem error logs for close-time failures
Defensive patterns

Strategy: retry

When it happens

Trigger: Thrown at internal/js/modules/k6/browser/storage/file_persister.go:38 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@8d06114777 (2026-08-18). Data as JSON: /api/errors/ecec52f56e865553. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/storage/file_persister.go:38

type LocalFilePersister struct{}

// Persist will write the contents of data to the local disk on the specified path.
// TODO: we should not write to disk here but put it on some queue for async disk writes.
func (l *LocalFilePersister) Persist(_ context.Context, path string, data io.Reader) (err error) {
	cp := filepath.Clean(path)

	dir := filepath.Dir(cp)
	if err = os.MkdirAll(dir, 0o755); err != nil { //nolint:forbidigo,gosec
		return fmt.Errorf("creating a local directory %q: %w", dir, err)
	}

	f, err := os.OpenFile(cp, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600) //nolint:forbidigo
	if err != nil {
		return fmt.Errorf("creating a local file %q: %w", cp, err)
	}
	defer func() {
		if cerr := f.Close(); cerr != nil && err == nil {
			err = fmt.Errorf("closing the local file %q: %w", cp, cerr)
		}
	}()

	bf := bufio.NewWriter(f)

	if _, err := io.Copy(bf, data); err != nil {
		return fmt.Errorf("copying data to file: %w", err)
	}

	if err := bf.Flush(); err != nil {
		return fmt.Errorf("flushing data to disk: %w", err)
	}

	return nil
}

// RemoteFilePersister is to be used when files created by the browser module need
// to be uploaded to a remote location. This uses a presignedURLRequestURL to

View on GitHub (pinned to 8d06114777)