Skip to content

Conversation

@thaJeztah
Copy link
Member

cli/command/container: RunStats: simplify, and fix context-cancellation

cli/command/container: move debug logs to call-site

cli/command/container: RunStats: simplify stats loop

Use a single select for the ticker and the closeChan; use early returns.

cli/command/container: RunStats: handle context-cancellation

cli/command/container: RunStats: gracefully handle io.EOF

cli/command/container: RunStats: small tweaks on closeChan

Some suggestions from ChatGPT to prevent deadlocks.

cli/command/container: RunStats: early return for non-streaming

We should consider splitting this out to a separate function, but
start with just an early return before we hit the timer-loop.

- Human readable description for the release notes

Fix `docker stats <container>` not exiting gracefully.

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

Use a single select for the ticker and the closeChan; use early returns.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@codecov-commenter
Copy link

codecov-commenter commented Oct 28, 2025

Codecov Report

❌ Patch coverage is 0% with 71 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cli/command/container/stats.go 0.00% 61 Missing ⚠️
cli/command/container/stats_helpers.go 0.00% 10 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a context-cancellation issue in the docker stats command where it would not exit gracefully when interrupted. The changes simplify the stats collection loop and add proper handling for context cancellation and EOF errors.

Key changes:

  • Refactored the stats loop from a for range ticker.C pattern to a for { select { ... } } pattern with explicit context cancellation handling
  • Added early return for non-streaming mode to avoid entering the ticker loop unnecessarily
  • Improved error handling to gracefully handle io.EOF and io.ErrUnexpectedEOF

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
cli/command/container/stats_helpers.go Removed debug log statement from collect function (moved to call-site)
cli/command/container/stats.go Main implementation changes: refactored stats loop, added context cancellation handling, moved debug logs to call-sites, and improved error handling

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Some suggestions from ChatGPT to prevent deadlocks.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
We should consider splitting this out to a separate function, but
start with just an early return before we hit the timer-loop.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah force-pushed the fix_stats_cancellation branch from 0c6d17b to 292001a Compare October 28, 2025 11:36
construct the decoder inside the go-routine, including closing the body,
and add handling for context-cancellation.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah force-pushed the fix_stats_cancellation branch from 051ccb2 to e01ce69 Compare October 28, 2025 12:29
@thaJeztah
Copy link
Member Author

Thanks @robmry !

@thaJeztah thaJeztah merged commit e809e65 into docker:master Oct 28, 2025
87 checks passed
@thaJeztah thaJeztah deleted the fix_stats_cancellation branch October 28, 2025 13:12
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.

docker stats <container> doesn't exit gracefully

3 participants