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

proposal: io: document Closer as an idempotent operation #25390

Closed
dsnet opened this issue May 14, 2018 · 6 comments
Closed

proposal: io: document Closer as an idempotent operation #25390

dsnet opened this issue May 14, 2018 · 6 comments

Comments

@dsnet
Copy link
Member

dsnet commented May 14, 2018

The documented behavior of io.Closer is:

The behavior of Close after the first call is undefined. Specific implementations may document their own behavior.

The fact that the behavior of a second call to Close is undefined means that an implementation may actually panic. Unfortunately, too many callers of Close make the assumption that the call is safe to make twice. For example:

f, ... := foo.Open(...)
defer f.Close() // panic!
...
if err := f.Close(); err != nil {
    ...
}

The documentation regarding undefined behavior was made 5 years ago by @bradfitz because he observed people making this assumption. However, time has demonstrated that people still widely assume Close to be idempotent.

Thus, I propose changing the documentation to match what people expect:

Calling Close is an idempotent operation that returns the same error value for all subsequent calls as the first call.

This may need to be a Go2 change, but it may be worth considering today given how pervasive the assumption already is.

@gopherbot gopherbot added this to the Proposal milestone May 14, 2018
@davecheney
Copy link
Contributor

If the first a Close returned an error, are subsequent calls required to return the same error?

Conversely, if the first close returned no error, are subsequent calls required to return no error?

@davecheney
Copy link
Contributor

Would it be more correct to update the documented for io.Closer to say

implementations must not panic, rather use the provided error value to communicate with the caller.

That would appear to address this issue without having to overly specify the outcome of calling Close more than once.

@slrz
Copy link

slrz commented May 14, 2018

Why is a panic considered a bad outcome given today's contract? It allows you to quickly pinpoint the issue and go on. I'm more worried about the cases where duplicate Close calls result in silent bogus actions (think os.File without its safeguards).

Implementations of io.Closer are often wrappers around other systems where the close operation is not defined as idempotent or where duplicate close calls are plain illegal/undefined. This change would require all those io.Closers to keep a state flag (and possibly synchronization around it).

I don't think this can possibly be a Go1 change.

@bradfitz
Copy link
Contributor

Thus, I propose changing the documentation to match what people expect:

Calling Close is an idempotent operation that returns the same error value for all subsequent calls as the first call.

That's not wording we can really use for an interface. We can only document the actual behavior for concrete types (you used the word "is"). For an interface, we can only say "should" or "must not", etc.

@rsc
Copy link
Contributor

rsc commented May 21, 2018

When the io.Closer docs say "The behavior of Close after the first call is undefined.", that means callers may not assume anything and that implementations may do anything they like. We can't retroactively change that, not without causing many (most?) existing implementations to become invalid. We should probably leave well enough alone for now. At most we could add a suggestion that implementations be idempotent, but users of a general io.Closer still must not assume that.

@rsc
Copy link
Contributor

rsc commented Jun 4, 2018

Per previous comment, we're pretty well locked in at this point. We can't redefine the interface. There are a few APIs where Close means "commit" and maybe we should look at this separately. But io.Closer is what it is.

@rsc rsc closed this as completed Jun 4, 2018
@golang golang locked and limited conversation to collaborators Jun 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants