grafana/k6 · warning

failed to close file %s: %w

Error message

failed to close file %s: %w

What it means

Raised by the experimental fs module's internal file cache: after a file registered via open() has been fully read with io.ReadAll, the deferred f.Close() on the underlying fsext file handle fails; because cache.open uses named return values, the close error replaces the nil error and is returned from open() (internal/js/modules/k6/experimental/fs/cache.go:98). The content was read successfully by the time it fires, so it indicates a broken handle or filesystem backend, not missing data. It is extremely rare.

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 93accf6570)

Solutions

  1. Re-run the script - transient handle failures are the most common cause
  2. Verify the file is intact (size, checksum) and on stable local storage
  3. If it reproduces, minimize the script and report it as a k6 bug with your filesystem setup
Defensive patterns

Strategy: retry

Try / catch

function openWithRetry(path, attempts = 2) {
  for (let i = 0; i < attempts; i++) {
    try { return open(path); }
    catch (e) {
      if (!/failed to close file/.test(e.message) || i === attempts - 1) throw e;
    }
  }
}

Prevention

When it happens

Trigger: fs.open(path) where the backing file handle fails on close - a corrupted archive-backed filesystem entry, the file deleted/truncated externally between open and close, or a flaky network/FUSE mount behind fsext.

Common situations: Running scripts from a hand-modified or corrupted k6 archive; unusual container storage setups; almost never occurs with plain local files.

Related errors


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