[27.x backport] Buffer 'docker stats' text to avoid terminal flickering #5588
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This change reduces the flickering of the terminal when running
docker stats
by buffering the formatted stats text and printing it in one write.Should also consume less CPU as we now only have to issue a single syscall to write the stats text to the terminal.
I suspect it fixes #4348, but I can not confirm since that issue was reported on WSL.
- What I did
On all of my Linux machines (Fedora Linux + GNOME) I can see terminal flickering when running 'docker stats'.
I've tested this with gnome-terminal/xfce4-terminal/xterm and it flickers on all. The only term that does not flicker is kitty.
I've also tested this on GNOME running Wayland and X11. I can see no difference in behaviour between Wayland/X11.
I've tested this on two machines: Thinkpad T14s gen1 running AMD zen, and a desktop PC running AMD zen chip with Radeon GPU. Both behave identically.
recording of the flickering:
https://github.com/user-attachments/assets/05f8278e-05c3-411e-bb32-dad262136937
'docker stats' used to issue multiple write calls to stdout. Starting with the "clear screen" command and following multiple small write requests.
I'm now bundling all of those into a single string that is then written out to the stdout as a single request.
This fixes the flickering (as well as reduces syscalls required for each update) on both of my machines.
I've tested this on Fedora 41 GNOME with the following terminal emulators: gnome-terminal/xfce4-terminal/xterm/kitty.
I've tried things like resizing the window as 'docker stats' was running. It all works flawlessly now :)
- Description for the changelog
- A picture of a cute animal (not mandatory but encouraged)
A cat that sometimes roams outside my flat. Me and my flatmates call him Phantom 🐱