{"record":{"id":"3cf84f051b4f2cfc","repo":"gofr-dev/gofr","slug":"file-is-closed","errorCode":null,"errorMessage":"File is closed","messagePattern":"File is closed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/gofr/datasource/interface.go","lineNumber":59,"sourceCode":"\tOpen(name string) (File, error)\n\n\t// OpenFile opens a file using the given flags and the given mode.\n\tOpenFile(name string, flag int, perm os.FileMode) (File, error)\n\n\t// Remove removes a file identified by name, returning an error, if any\n\t// happens.\n\tRemove(name string) error\n\n\t// RemoveAll removes a directory path and any children it contains. It\n\t// does not fail if the path does not exist (return nil).\n\tRemoveAll(path string) error\n\n\t// Rename renames a file.\n\tRename(oldname, newname string) error\n}\n\nvar (\n\tErrFileClosed        = errors.New(\"File is closed\")\n\tErrOutOfRange        = errors.New(\"out of range\")\n\tErrTooLarge          = errors.New(\"too large\")\n\tErrFileNotFound      = os.ErrNotExist\n\tErrFileExists        = os.ErrExist\n\tErrDestinationExists = os.ErrExist\n)\n\ntype FileSystemProvider interface {\n\tFileSystem\n\n\t// UseLogger sets the logger for the FileSystem client.\n\tUseLogger(logger any)\n\n\t// UseMetrics sets the metrics for the FileSystem client.\n\tUseMetrics(metrics any)\n\n\t// Connect establishes a connection to FileSystem and registers metrics using the provided configuration when the client was Created.\n\tConnect()","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/datasource/interface.go#L41-L77","documentation":"ErrFileClosed is a public sentinel error in the gofr datasource interface package indicating an operation was attempted on a datasource file that has already been closed. It mirrors os.ErrClosed semantics for provider-agnostic file abstraction. Any read/write/seek after Close returns this.","triggerScenarios":"Reading, writing, seeking, or stat-ing a CommonFile after calling Close() on it.","commonSituations":"Double-close followed by use; deferred Close() early in a function so subsequent code uses a closed file; goroutines sharing a file where one closes it while another reads.","solutions":["Remove the extra Close() or restructure so Close is the last operation (defer at function end)","Reopen the file if continued use is needed","Check errors.Is(err, ErrFileClosed) to handle closed-file cases explicitly"],"exampleFix":"// before\nf.Close()\ndata, _ := f.Read(...) // File is closed\n// after\ndata, _ := f.Read(...)\nf.Close()","handlingStrategy":"try-catch","validationCode":"// track close state yourself\ntype safeFile struct{ f datasource.File; closed bool }\nfunc (s *safeFile) ensureOpen() error {\n    if s.closed { return datasource.ErrFileClosed }\n    return nil\n}","typeGuard":"func isFileClosed(err error) bool {\n    return errors.Is(err, datasource.ErrFileClosed)\n}","tryCatchPattern":"n, err := f.Read(buf)\nif errors.Is(err, datasource.ErrFileClosed) {\n    // reopen or abort operation\n}","preventionTips":["Use defer f.Close() at function end, never before final reads","Avoid sharing files across goroutines without synchronization","Never use a file after Close; reopen instead"],"tags":["file-io","lifecycle","gofr","datasource"],"backgroundTag":"use-after-close","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}