Skip to content

fix: return tty size in container_inspect.go#28579

Open
priyanshsao wants to merge 1 commit into
containers:mainfrom
priyanshsao:inspect-tty-size
Open

fix: return tty size in container_inspect.go#28579
priyanshsao wants to merge 1 commit into
containers:mainfrom
priyanshsao:inspect-tty-size

Conversation

@priyanshsao
Copy link
Copy Markdown

@priyanshsao priyanshsao commented Apr 24, 2026

In the output of podman inspect, there is a field for the container's TTY size (ConsoleSize), which Podman presently only returns [0,0].
This PR updates the tty size for containers which have terminal enabled and are in running state.
For cases where there is an issue with getting the size or the container is not running, suitable warning is given, no update happens and default [0,0] is returned.

Fixes: #28368

Checklist

Ensure you have completed the following checklist for your pull request to be reviewed:

  • Certify you wrote the patch or otherwise have the right to pass it on as an open-source patch by signing all
    commits. (git commit -s). (If needed, use git commit -s --amend). The author email must match
    the sign-off email address. See CONTRIBUTING.md
    for more information.
  • Referenced issues using Fixes: #00000 in commit message (if applicable)
  • Tests have been added/updated (or no tests are needed)
  • Documentation has been updated (or no documentation changes are needed)
  • All commits pass make validatepr (format/lint checks)
  • Release note entered in the section below (or None if no user-facing changes)

Does this PR introduce a user-facing change?

Fix podman inspect to return console size of a running container.

@priyanshsao
Copy link
Copy Markdown
Author

A simple video to see the change quickly

podman_tty.mp4

@packit-as-a-service
Copy link
Copy Markdown

tmt tests failed for commit 8f5a4a2. @lsm5, @psss, @thrix please check.

Copy link
Copy Markdown
Member

@danishprakash danishprakash left a comment

Choose a reason for hiding this comment

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

Can you please also add a test case for this?

Comment thread libpod/container_inspect.go Outdated
} else {
defer f.Close()

height, width, err := xterm.GetSize(int(f.Fd()))
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.

GetSize returns (width, height), so the size is flipped.

Comment thread libpod/container_inspect.go Outdated
Comment on lines +641 to +643
procPath := fmt.Sprintf("/proc/%d/fd/0", c.state.PID)

f, err := os.Open(procPath)
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.

That path doesn't exist on FreeBSD, so this would always fail. We could either guard this for Linux or even split this into *_freebsd.go. Bottom line, the behavior on FreeBSD won't change (podman will still return [0,0]), but there's going to be an additional warning, which is superfluous considering we know this won't work.

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.

Probably safest to have _linux and _freebsd files for this

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

So, for *_freebsd.go the console size should be updated with another method or just simply make it [0,0] ?

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.

0,0 for now, though add a TODO comment saying it should be fixed later

Comment thread libpod/container_inspect.go Outdated
f, err := os.Open(procPath)
if err != nil {
if c.state.State != define.ContainerStateRunning {
logrus.Warnf("container %q is not running, unable to get console size", c.ID())
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.

To me, this seems to be noise.

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.

Maybe debug or exit with an error?

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.

Debug logs safest

Comment thread libpod/container_inspect.go Outdated
"go.podman.io/podman/v6/libpod/driver"
"go.podman.io/podman/v6/pkg/signal"
"go.podman.io/podman/v6/pkg/util"
xterm "golang.org/x/term"
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.

Suggested change
xterm "golang.org/x/term"
"golang.org/x/term"

Comment thread libpod/container_inspect.go Outdated
if c.state.State != define.ContainerStateRunning {
logrus.Warnf("container %q is not running, unable to get console size", c.ID())
} else {
logrus.Warnf("unable to open %s", procPath)
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.

The lack of context here might confuse more than it would help, consider:

Suggested change
logrus.Warnf("unable to open %s", procPath)
logrus.Debugf("unable to get console size for container %s: %v", c.ID(), err)

Comment thread libpod/container_inspect.go Outdated
if c.Terminal() {
procPath := fmt.Sprintf("/proc/%d/fd/0", c.state.PID)

f, err := os.Open(procPath)
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.

What if the container doesn't run, and some other process gets that PID?

Copy link
Copy Markdown
Author

@priyanshsao priyanshsao May 1, 2026

Choose a reason for hiding this comment

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

According to my knowledege,
PID's are reused only when the prev. process with same PID has already been exited.
Even if another process gets same PID,
the prev. container c.state.pid returns 0 because it is stopped, because without the prev. process stopped same PID cannot be assigned to another.
Also, /proc/0/fd/ access is not allowed, so os.Open("/proc/0/fd/") gives error.
Resulting in debug message and [0,0] consoleSize.

Comment thread libpod/container_inspect.go Outdated
f, err := os.Open(procPath)
if err != nil {
if c.state.State != define.ContainerStateRunning {
logrus.Warnf("container %q is not running, unable to get console size", c.ID())
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.

Maybe debug or exit with an error?

@priyanshsao
Copy link
Copy Markdown
Author

priyanshsao commented May 4, 2026

Can you please also add a test case for this?

@danishprakash @mheon,
I was looking at the test files for container_inspect.go, I have found this integration test container_inspect_test.go, do I need to add test case in this file?
can you please provide more details.

@mheon
Copy link
Copy Markdown
Member

mheon commented May 4, 2026

Probably not necessary. You could add an E2E test instead, maybe in test/e2e/inspect_test.go, and just verify the numbers are non-zero when a container is started with a TTY and 0 when it is not?

@priyanshsao
Copy link
Copy Markdown
Author

Probably not necessary. You could add an E2E test instead, maybe in test/e2e/inspect_test.go, and just verify the numbers are non-zero when a container is started with a TTY and 0 when it is not?

I think this test would fail in FreeBSD, because we return zero in both case. What do you think?

@danishprakash
Copy link
Copy Markdown
Member

You could skip the test on freebsd, or check that it returns [0,0]:

if runtime.GOOS == "freebsd" {
    Skip("ConsoleSize not implemented on FreeBSD")
}

In the output of podman inspect, there is a field for the container's TTY size (ConsoleSize), which Podman presently only returns [0,0].
This PR updates the console size field for containers with terminal enabled.
For cases where there is an issue with getting the size, suitable debug information is given, no update happens and default [0,0] is returned.
This implementation is only for Linux systems.
For FreeBSD, the console size is not implemented yet so, adds a todo for that.

Fixes: containers#28368

Signed-off-by: Priyansh Sao <saopriyansh06@gmail.com>
@priyanshsao
Copy link
Copy Markdown
Author

priyanshsao commented May 7, 2026

Implemented all the changes related to implementation asked by reviewers.
For tests I will make another commit.

Just a small change from my side, I replaced the Warnf with Debugf for this
logrus.Debugf("could not get terminal size: %v", err)
I thought, the users shouldn't be bothered, if for some reason we were unable to get size.

If that's not a good change, I would fix that when I would make commit for tests.

@priyanshsao
Copy link
Copy Markdown
Author

priyanshsao commented May 7, 2026

For tests, the problem I'm facing is that, to get the consoleSize field updated

container's terminal should be enabled + a real terminal should be attached.

But this doesn't work in podman test:
session := podmanTest.Podman([]string{"run", "-it", "--name", ctrName, FEDORA_MINIMAL, "sleep", "+Inf"})

So I tried this:
session := podmanTest.Podman([]string{"run", "-dt", "--name", ctrName, FEDORA_MINIMAL, "sleep", "+Inf"})

But the thing is even with terminal enabled the console size is 0 because no terminal is attached.

@priyanshsao priyanshsao closed this May 7, 2026
@priyanshsao priyanshsao reopened this May 7, 2026
@priyanshsao
Copy link
Copy Markdown
Author

Very Sorry for this, my keys got wrongly pressed and this pr got closed.

@Honny1
Copy link
Copy Markdown
Member

Honny1 commented May 7, 2026

@priyanshsao I think you forgot to push the new test. Can you check your changes once more? Thanks.

@priyanshsao
Copy link
Copy Markdown
Author

priyanshsao commented May 7, 2026

@priyanshsao I think you forgot to push the new test. Can you check your changes once more? Thanks.

There are some issues I'm facing related to tests, once those are resolved I will add another commit with changes.

@priyanshsao
Copy link
Copy Markdown
Author

For tests, the problem I'm facing is that, to get the consoleSize field updated

container's terminal should be enabled + a real terminal should be attached.

But this doesn't work in podman test: session := podmanTest.Podman([]string{"run", "-it", "--name", ctrName, FEDORA_MINIMAL, "sleep", "+Inf"})

So I tried this: session := podmanTest.Podman([]string{"run", "-dt", "--name", ctrName, FEDORA_MINIMAL, "sleep", "+Inf"})

But the thing is even with terminal enabled the console size is 0 because no terminal is attached.

@mheon @danishprakash, Do I need to use some external lib for creating a real terminal or podman tests has some functions for this, can you please provide your thoughts on this.

@mheon
Copy link
Copy Markdown
Member

mheon commented May 11, 2026

Hm. I don't know if we've encountered this one before. It looks like the system tests can create terminals.

@mheon
Copy link
Copy Markdown
Member

mheon commented May 11, 2026

Yeah, system tests might be safer - see tests like https://github.com/containers/podman/blob/main/test/system/450-interactive.bats#L51-L81

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

podman inspect does not return size of container terminal

4 participants