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

Comments

Projects
None yet
6 participants
@dsnet
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.

@dsnet dsnet added the Documentation label May 14, 2018

@gopherbot gopherbot added this to the Proposal milestone May 14, 2018

@gopherbot gopherbot added the Proposal label May 14, 2018

@davecheney

This comment has been minimized.

Contributor

davecheney commented May 14, 2018

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

This comment has been minimized.

Contributor

davecheney commented May 14, 2018

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

This comment has been minimized.

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

This comment has been minimized.

Member

bradfitz commented May 21, 2018

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

This comment has been minimized.

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

This comment has been minimized.

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 Jun 4, 2018

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