Skip to content

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

Merged
thaJeztah merged 1 commit intomoby:masterfrom
vvoland:pkg-pools-close-noop
Jan 25, 2024
Merged

pkg/ioutils: Make subsequent Close calls a no-op#46419
thaJeztah merged 1 commit intomoby:masterfrom
vvoland:pkg-pools-close-noop

Conversation

@vvoland
Copy link
Copy Markdown
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)

Comment thread pkg/pools/pools.go Outdated
Comment thread pkg/pools/pools.go Outdated
@vvoland vvoland force-pushed the pkg-pools-close-noop branch from d5bc8dc to 415a910 Compare September 7, 2023 11:37
Comment thread pkg/pools/pools.go Outdated
Comment thread pkg/pools/pools.go Outdated
Comment thread pkg/pools/pools.go Outdated
return ioutils.NewReadCloserWrapper(r, func() error {
if !atomic.CompareAndSwapInt32(&closed, 0, 1) {
subsequentCloseWarn("ReadCloserWrapper")
return nil
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread pkg/pools/pools.go Outdated
Comment thread pkg/pools/pools.go Outdated
@thaJeztah
Copy link
Copy Markdown
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 <[email protected]>
@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
Copy Markdown
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

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