{"record":{"id":"6ac8b1b263f83822","repo":"larksuite/cli","slug":"cannot-stat-opened-file-w-6ac8b1","errorCode":null,"errorMessage":"cannot stat opened file: %w","messagePattern":"cannot stat opened file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/vfs/localfileio/openvalidated_windows.go","lineNumber":44,"sourceCode":"\tif err != nil {\n\t\treturn nil, err\n\t}\n\tif err := inspectOpenedFile(f, pre); err != nil {\n\t\tf.Close()\n\t\t// An unusable target is a bad argument, not an internal fault: callers\n\t\t// map ErrPathValidation to a typed validation error, and the fd checks\n\t\t// are the same verdict the path checks make, one layer later.\n\t\treturn nil, &fileio.PathValidationError{Err: err}\n\t}\n\treturn f, nil\n}\n\n// inspectOpenedFile validates the opened handle. pre is nil when there is no\n// prior Stat to compare against.\nfunc inspectOpenedFile(f *os.File, pre os.FileInfo) error {\n\tpost, err := f.Stat()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"cannot stat opened file: %w\", err)\n\t}\n\tif pre != nil && !os.SameFile(pre, post) {\n\t\treturn fmt.Errorf(\"file changed between validation and open\")\n\t}\n\tif !post.Mode().IsRegular() {\n\t\treturn fmt.Errorf(\"not a regular file (directories, devices, FIFOs, and sockets are refused)\")\n\t}\n\tvar handleInfo syscall.ByHandleFileInformation\n\tif err := syscall.GetFileInformationByHandle(syscall.Handle(f.Fd()), &handleInfo); err != nil {\n\t\treturn fmt.Errorf(\"cannot inspect opened file links: %w\", err)\n\t}\n\tif handleInfo.NumberOfLinks > 1 {\n\t\treturn fmt.Errorf(\"file has multiple hard links, so the other names it can be reached by \" +\n\t\t\t\"cannot be checked (hint: copy the file and use the copy instead)\")\n\t}\n\treturn nil\n}\n","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/larksuite/cli/blob/7fd6ef3c07182257ce776cdc5a614e122d5bd4b3/internal/vfs/localfileio/openvalidated_windows.go#L26-L62","documentation":"This error means the OS Stat call on an already-opened file handle failed inside inspectOpenedFile. The library opens the path, then re-stats via the handle to verify nothing changed between path validation and open; if the handle cannot be stat'd, the file's post-open identity is unknown and opening is refused rather than proceeding on an unverifiable handle. It wraps the underlying OS error, so the cause (e.g. deleted file, invalid handle) is in %w.","triggerScenarios":"Calling openValidated on Windows when the file is deleted or the volume becomes unavailable between opening the handle and calling f.Stat(), or when the returned handle is somehow invalid.","commonSituations":"Antivirus/quarantine removing the file mid-open, files on network shares or removable drives that drop during access, race with another process deleting the file, or filesystem errors on failing disks.","solutions":["Retry the open; the failure is often a transient race and a fresh openValidated call will either succeed or give a clearer error","Check the wrapped cause (%w) to identify the OS failure (e.g. ERROR_FILE_NOT_FOUND means a concurrent delete)","Verify the file is on a healthy local NTFS volume, not a flaky network share or removable drive","Exclude the path from antivirus real-time scanning/quarantine if AV is deleting files mid-open"],"exampleFix":"// before: no retry, single call\nf, err := openValidated(path, preStat)\n// after: tolerate transient mid-open failures with bounded retry\nvar f *os.File\nfor i := 0; i < 3; i++ {\n    f, err = openValidated(path, preStat)\n    if err == nil { break }\n    time.Sleep(50 * time.Millisecond)\n}","handlingStrategy":"retry","validationCode":"info, err := os.Stat(path)\nif err != nil { return fmt.Errorf(\"path not statable before open: %w\", err) }\n_ = info","typeGuard":null,"tryCatchPattern":"var f *os.File\nvar err error\nfor i := 0; i < 3; i++ {\n    f, err = openValidated(path, pre)\n    if err == nil || !strings.Contains(err.Error(), \"cannot stat opened file\") { break }\n    time.Sleep(50 * time.Millisecond)\n}\nif err != nil { return err }","preventionTips":["Keep target files on healthy local volumes; avoid flaky network shares and removable drives","Exclude CLI workspace paths from aggressive antivirus quarantine","Do not delete or move files concurrently with opening them"],"tags":["windows","filesystem","toctou","file-open"],"backgroundTag":"file-stat-failed","analyzedSha":"7fd6ef3c07182257ce776cdc5a614e122d5bd4b3","analyzedAt":"2026-09-04T21:17:44.649Z","contentChangedAt":"2026-09-04T21:17:44.649Z","schemaVersion":2},"datasetVersion":"2026-09-12T02:17:10.037Z"}