{"record":{"id":"f7990711adcc97bc","repo":"grafana/k6","slug":"failed-to-close-file-s-w","errorCode":null,"errorMessage":"failed to close file %s: %w","messagePattern":"failed to close file (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/js/modules/k6/experimental/fs/cache.go","lineNumber":98,"sourceCode":"\n\tif f, ok := fr.openedFiles.Load(filename); ok {\n\t\tdata, ok = f.([]byte)\n\t\tif !ok {\n\t\t\tpanic(fmt.Errorf(\"cache's file %s is not stored as a byte slice\", filename))\n\t\t}\n\n\t\treturn data, nil\n\t}\n\n\t// TODO: re-evaluate opening from the FS this once #1079 is resolved.\n\tf, err := fromFs.Open(filename)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tdefer func() {\n\t\tcerr := f.Close()\n\t\tif cerr != nil {\n\t\t\terr = fmt.Errorf(\"failed to close file %s: %w\", filename, cerr)\n\t\t}\n\t}()\n\n\tdata, err = io.ReadAll(f)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to read the content of file %s: %w\", filename, err)\n\t}\n\n\tfr.openedFiles.Store(filename, data)\n\n\treturn data, nil\n}\n","sourceCodeStart":80,"sourceCodeEnd":111,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/experimental/fs/cache.go#L80-L111","documentation":"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.","triggerScenarios":"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.","commonSituations":"Running scripts from a hand-modified or corrupted k6 archive; unusual container storage setups; almost never occurs with plain local files.","solutions":["Re-run the script - transient handle failures are the most common cause","Verify the file is intact (size, checksum) and on stable local storage","If it reproduces, minimize the script and report it as a k6 bug with your filesystem setup"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"function openWithRetry(path, attempts = 2) {\n  for (let i = 0; i < attempts; i++) {\n    try { return open(path); }\n    catch (e) {\n      if (!/failed to close file/.test(e.message) || i === attempts - 1) throw e;\n    }\n  }\n}","preventionTips":["Keep data files on local, intact storage","Do not hand-edit k6 archives","If it recurs, capture the filesystem type and report a k6 issue"],"tags":["fs","k6","io","experimental","file-handle"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}