Skip to content

libartifact: add CloseEventChannel to cleanly stop event streams#831

Open
nimdrak wants to merge 1 commit into
containers:mainfrom
nimdrak:add_shutdown_eventchannel_in_libartifact
Open

libartifact: add CloseEventChannel to cleanly stop event streams#831
nimdrak wants to merge 1 commit into
containers:mainfrom
nimdrak:add_shutdown_eventchannel_in_libartifact

Conversation

@nimdrak
Copy link
Copy Markdown
Contributor

@nimdrak nimdrak commented May 8, 2026

Fixes #465

We need a way to cleanly close the event channel from the sender side to prevent resource leaks.
This requirement was identified while implementing artifact event handling in Podman (containers/podman#28662).

@github-actions github-actions Bot added the common Related to "common" package label May 8, 2026
Comment thread common/pkg/libartifact/store_test.go Outdated
ch := as.EventChannel()
defer func() {
as.CloseEventChannel()
}()
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.

if you want to add an actual test because this was already passing you need to add extra reads from the channel to ensure this is not blocking after you close it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review. I've addressed your feedback, squashed the changes into the original commit, and force-pushed. Please check it here 03c5b33

Comment on lines +99 to +103
func (as *ArtifactStore) CloseEventChannel() {
if as.eventChannel != nil {
close(as.eventChannel)
}
}
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.

This and well even EventChannel() are not safe for concurrent use. Another goroutine might as well try to write to it after this is closed.

Generally design wise nothing prevents users from keep using the store after this is closed which will cause panics. At the very least as.eventChannel would need to be set to nil but even that will not prevent any race conditions.

Overall the more I look at this and libimage event handling the more I see how totally broken (racy) this all is, I really need to stress that we need something like #465 (comment) instead which would avoid basically all race conditions. OF course out of scope for this I guess...

At the minimum I would expect big comments here warning of any concurrent use.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see your point. Since you mentioned fixing the race condition is out of scope for this PR, I will add the warning comment as you suggested. I agree that it would be better if the architecture becomes robust towards concurrent access in the future.

Out of curiosity, I initially thought the artifact store would only be accessed by a single goroutine from a single libpod runtime at a time. Are there actual use cases where it is handled concurrently? I would highly appreciate it if you could share your insights into the broader architecture so I can understand it better.

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.

@Luap99 should we make an issue for your proposal long-term?

@nimdrak nimdrak force-pushed the add_shutdown_eventchannel_in_libartifact branch from c4ba785 to 03c5b33 Compare May 8, 2026 13:33
Copy link
Copy Markdown
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

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

LGTM

@baude @mheon PTAL

Copy link
Copy Markdown
Member

@baude baude left a comment

Choose a reason for hiding this comment

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

LGTM

@baude baude enabled auto-merge May 11, 2026 13:27
@Luap99 Luap99 disabled auto-merge May 11, 2026 13:30
@Luap99
Copy link
Copy Markdown
Member

Luap99 commented May 11, 2026

@nimdrak please force push, cirrus CI was not triggered right here

- Add CloseEventChannel with a warning comment to `CloseEventChannel` regarding unsafe concurrent usage
- Refactor `TestArtifactStore_EventChannel` to isolate test setups per `t.Run`.
- Add a dedicated test case to verify that `CloseEventChannel` properly closes the channel and ensures non-blocking reads.

Signed-off-by: Byounguk Lee <nimdrak@gmail.com>
@nimdrak nimdrak force-pushed the add_shutdown_eventchannel_in_libartifact branch from 03c5b33 to ecc9631 Compare May 11, 2026 23:17
@nimdrak
Copy link
Copy Markdown
Contributor Author

nimdrak commented May 11, 2026

@Luap99 I did. Thanks for your review!

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

Labels

common Related to "common" package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[libartifact] Introduce an event channel in ArtifactStore

3 participants