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

pkg/ioutils: Make subsequent Close calls a no-op #46419

Merged
merged 1 commit into from
Jan 25, 2024

Conversation

vvoland
Copy link
Contributor

@vvoland vvoland commented Sep 7, 2023

Turn subsequent Close calls into a no-op and produce a warning with an optional stack trace (if debug mode is enabled).

- What I did
Turned subsequent Close calls on on (Read/Write/cancel)CloserWrappers into a no-op.

- How I did it

- How to verify it

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

pkg/pools/pools.go Outdated Show resolved Hide resolved
pkg/pools/pools.go Outdated Show resolved Hide resolved
@vvoland vvoland force-pushed the pkg-pools-close-noop branch from d5bc8dc to 415a910 Compare September 7, 2023 11:37
pkg/pools/pools.go Outdated Show resolved Hide resolved
pkg/pools/pools.go Outdated Show resolved Hide resolved
return ioutils.NewReadCloserWrapper(r, func() error {
if !atomic.CompareAndSwapInt32(&closed, 0, 1) {
subsequentCloseWarn("ReadCloserWrapper")
return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

The behavior of Close after the first call is undefined.

Code which calls Close more than once on an arbitrary io.Closer is buggy by definition. Logging a warning is a great first step, especially for code which may discard the error return, but it really should also return an error to maximize fail-loudly-ness without actually panicking.

Copy link
Member

Choose a reason for hiding this comment

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

In this case, I would tend to agree, at least with not logging, nobody will ever see this log message message because there generally wouldn't be a reason to correlate that with anything.
If someone does see it it will have very little meaning to them.

If it is an error maybe we'll see it show up in CI?
In most cases if there is a bug I would imagine it would be something like passing a closer to another function and both the outer and the inner have close in a defer.
Not technically correct since the outer function should be delegating the close to the inner function.
That can also get hairy, what if the inner function returns an error? It would be difficult to tell if the closer was closed or not.

Anyway, I see your point that this would be a bug that we should catch, but also possibly it would just as easily be a non-issue.
Not to mention in most places we don't check error on close.

pkg/pools/pools.go Outdated Show resolved Hide resolved
pkg/pools/pools.go Outdated Show resolved Hide resolved
@thaJeztah
Copy link
Member

@vvoland ptal

@vvoland vvoland force-pushed the pkg-pools-close-noop branch from 415a910 to 75b40fd Compare December 22, 2023 12:44
@vvoland vvoland changed the title pkg/pools: Make subsequent Close calls a no-op pkg/ioutils: Make subsequent Close calls a no-op Dec 22, 2023
@vvoland vvoland self-assigned this Dec 22, 2023
@vvoland vvoland force-pushed the pkg-pools-close-noop branch from 75b40fd to d2cbdc2 Compare January 3, 2024 16:01
Turn subsequent `Close` calls into a no-op and produce a warning with an
optional stack trace (if debug mode is enabled).

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
@vvoland vvoland force-pushed the pkg-pools-close-noop branch from d2cbdc2 to 585d74b Compare January 4, 2024 10:21
@vvoland vvoland requested a review from cpuguy83 January 16, 2024 13:47
@thaJeztah thaJeztah modified the milestones: 25.0.0, 26.0.0 Jan 19, 2024
@vvoland vvoland requested a review from corhere January 24, 2024 15:06
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

LGTM

@thaJeztah thaJeztah dismissed cpuguy83’s stale review January 25, 2024 13:24

change request was addressed #46419 (review)

@thaJeztah thaJeztah merged commit 69d2923 into moby:master Jan 25, 2024
105 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants