Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not double close reader #29354

Merged
merged 3 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion modules/git/blob_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bufio"
"bytes"
"io"
"io/fs"

"code.gitea.io/gitea/modules/log"
)
Expand Down Expand Up @@ -102,7 +103,17 @@ func (b *blobReader) Read(p []byte) (n int, err error) {

// Close implements io.Closer
func (b *blobReader) Close() error {
if b.rd == nil {
return fs.ErrClosed
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it panic here instead? Calling Close multiple times is a coding error. I checked all calls to DataAsync and could not find another double close. A panic could prevent double closes in future while returning a (most of time) ignored error could hide it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. None of Golang's other "Close" methods panics.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it simply return "nil"? Most (all) errors for a Close method are meanless .... no caller cares about it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think double Close should be supported. i.e. It's safe to invoke file.Close twice. So I think we should also allow invoking Close twice. It's convenient for some situations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we then add this rule to our coding guidelines?

If a type implements io.Closer, calling Close multiple times must not fail and return a nil error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it depends, for most cases: yes, nil is good enough.

But I guess there could be some special cases that the caller might be interested in the result.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then maybe relax the nil but specify that calling Close multiple times must not be undefined behaviour and must not panic?


defer b.cancel()

return DiscardFull(b.rd, b.n+1)
if err := DiscardFull(b.rd, b.n+1); err != nil {
return err
}

b.rd = nil

return nil
}
3 changes: 0 additions & 3 deletions routers/web/repo/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,6 @@ func editFile(ctx *context.Context, isNewFile bool) {
}

d, _ := io.ReadAll(dataRc)
if err := dataRc.Close(); err != nil {
log.Error("Error whilst closing blob data: %v", err)
}

buf = append(buf, d...)
if content, err := charset.ToUTF8(buf, charset.ConvertOpts{KeepBOM: true}); err != nil {
Expand Down
Loading