-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Do not double close reader #29354
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
Conversation
modules/git/blob_nogogit.go
Outdated
if b.rd == nil { | ||
return fs.ErrClosed | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
, callingClose
multiple times must not fail and return anil
error.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer to return "nil". But could LGTM ahead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return nil
is better.
Fixes go-gitea#29346 --------- Co-authored-by: wxiaoguang <[email protected]>
Backport #29354 by @KN4CK3R Fixes #29346 Co-authored-by: KN4CK3R <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
Fixes #29346