gofr-dev/gofr · error

File is closed

Error message

File is closed

What it means

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.

Source

Thrown at pkg/gofr/datasource/interface.go:59

	Open(name string) (File, error)

	// OpenFile opens a file using the given flags and the given mode.
	OpenFile(name string, flag int, perm os.FileMode) (File, error)

	// Remove removes a file identified by name, returning an error, if any
	// happens.
	Remove(name string) error

	// RemoveAll removes a directory path and any children it contains. It
	// does not fail if the path does not exist (return nil).
	RemoveAll(path string) error

	// Rename renames a file.
	Rename(oldname, newname string) error
}

var (
	ErrFileClosed        = errors.New("File is closed")
	ErrOutOfRange        = errors.New("out of range")
	ErrTooLarge          = errors.New("too large")
	ErrFileNotFound      = os.ErrNotExist
	ErrFileExists        = os.ErrExist
	ErrDestinationExists = os.ErrExist
)

type FileSystemProvider interface {
	FileSystem

	// UseLogger sets the logger for the FileSystem client.
	UseLogger(logger any)

	// UseMetrics sets the metrics for the FileSystem client.
	UseMetrics(metrics any)

	// Connect establishes a connection to FileSystem and registers metrics using the provided configuration when the client was Created.
	Connect()

View on GitHub (pinned to 187eb24962)

Solutions

  1. Remove the extra Close() or restructure so Close is the last operation (defer at function end)
  2. Reopen the file if continued use is needed
  3. Check errors.Is(err, ErrFileClosed) to handle closed-file cases explicitly

Example fix

// before
f.Close()
data, _ := f.Read(...) // File is closed
// after
data, _ := f.Read(...)
f.Close()
Defensive patterns

Strategy: try-catch

Validate before calling

// track close state yourself
type safeFile struct{ f datasource.File; closed bool }
func (s *safeFile) ensureOpen() error {
    if s.closed { return datasource.ErrFileClosed }
    return nil
}

Type guard

func isFileClosed(err error) bool {
    return errors.Is(err, datasource.ErrFileClosed)
}

Try / catch

n, err := f.Read(buf)
if errors.Is(err, datasource.ErrFileClosed) {
    // reopen or abort operation
}

Prevention

When it happens

Trigger: Reading, writing, seeking, or stat-ing a CommonFile after calling Close() on it.

Common situations: 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.

Related errors


AI-assisted analysis of gofr-dev/gofr@187eb24962 (2026-09-01). Data as JSON: /api/errors/3cf84f051b4f2cfc. Report an issue: GitHub.