Skip to content

client: addFile closes the source file before io.Copy — multipart file uploads always fail #12

Description

@Tyagiquamar

In client.go (current master, v1.22.66), addFile closes the opened file before calling io.Copy(part, file):

file, err := os.Open(filepath.Clean(path))
if err != nil {
    return err
}
err = file.Close()   // closes here...
...
_, err = io.Copy(part, file)  // ...then reads from a closed file

Any multipart form-file upload prepared through this helper fails with file already closed.

Repro (Go 1.26):

// addFile(w, "file", tmpfile) always returns:
// read <tmpfile>: file already closed

Fix: close the file only after the copy (defer). I have a fix + regression test ready and will open a PR linked to this issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions