Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The process GetSelf method can produce incorrect results when used with an alternate hostfs #147

Closed
fearful-symmetry opened this issue Apr 24, 2024 · 0 comments · Fixed by #149
Assignees
Labels
bug Something isn't working Team:Elastic-Agent Label for the Agent team

Comments

@fearful-symmetry
Copy link
Contributor

Right now, process.GetSelf() fetches the pid of the process with os.Getpid() and then uses the alternate hostfs to fetch the metrics from /proc/GETPID/ There's a problem with this, which you can demonstrate with a small test:

func TestContainerPids(t *testing.T) {
	t.Logf("os.getpid: %d", os.Getpid())

	raw, err := os.ReadFile("/proc/self/stat")
	require.NoError(t, err)
	pid := strings.Split(string(raw), " ")[0]
	t.Logf("statfile: %s", pid)

	raw, err = os.ReadFile("/hostfs/proc/self/stat")
	require.NoError(t, err)
	pid = strings.Split(string(raw), " ")[0]
	t.Logf("statfile hostfs: %s", pid)
}

This returns:

    process_container_test.go:42: os.getpid: 16
    process_container_test.go:47: statfile: 16
    process_container_test.go:52: statfile hostfs: 3929281

The namespacing is actually smart enough to handle something like /hostfs/proc/self/, but if you do os.Getpid() and then pass that to /hostfs/proc, you'll get an invalid result.

On linux, when we're using hostfs, GetSelf() should use /hostfs/proc/self instead.

@fearful-symmetry fearful-symmetry added the bug Something isn't working label Apr 24, 2024
@fearful-symmetry fearful-symmetry self-assigned this Apr 24, 2024
@pierrehilbert pierrehilbert added the Team:Elastic-Agent Label for the Agent team label May 2, 2024
pierrehilbert pushed a commit that referenced this issue May 3, 2024
Closes
#147

## What does this PR do?

This fixes an issue where `Stats.GetSelf()` would fail when run inside a
container while using a hostfs path, as the PID returned by
`os.Getpid()` would not be valid outside the container runtime.

This changes the logic, so if we have a `hostfs` set, `GetSelf` will
fetch the pid from `/hostf/proc/self`.

## Why is it important?

This is a bug.

## Checklist


- [x] My code follows the style guidelines of this project
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have added an entry in `CHANGELOG.md`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Team:Elastic-Agent Label for the Agent team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants