grafana/k6 · error

failed to close file

Error message

failed to close file %s: %w

What it means

Raised in the fs cache's deferred close in open: the file opened from the filesystem could not be closed after reading. The original close error is wrapped; the read may already have succeeded, so this surfaces resource-cleanup failure (e.g. file descriptor issues).

Solutions

  1. Inspect the wrapped error for the underlying OS cause
  2. Check file descriptor limits and file permissions on the target file
  3. Treat as mostly benign if the content was read; retry the open operation if it recurs
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at internal/js/modules/k6/experimental/fs/cache.go:98 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at internal/js/modules/k6/experimental/fs/cache.go:98

	if f, ok := fr.openedFiles.Load(filename); ok {
		data, ok = f.([]byte)
		if !ok {
			panic(fmt.Errorf("cache's file %s is not stored as a byte slice", filename))
		}

		return data, nil
	}

	// TODO: re-evaluate opening from the FS this once #1079 is resolved.
	f, err := fromFs.Open(filename)
	if err != nil {
		return nil, err
	}
	defer func() {
		cerr := f.Close()
		if cerr != nil {
			err = fmt.Errorf("failed to close file %s: %w", filename, cerr)
		}
	}()

	data, err = io.ReadAll(f)
	if err != nil {
		return nil, fmt.Errorf("failed to read the content of file %s: %w", filename, err)
	}

	fr.openedFiles.Store(filename, data)

	return data, nil
}

View on GitHub (pinned to 01ffac6f24)