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

Close proc file when done discovering PID #649

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http
### Fixed

- Don't call `manager.Close()` when `Analyze()` fails. ([#638](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/638))
- Close `proc` file when done discovering PID. ([#649](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/649))

## [v0.10.1-alpha] - 2024-01-10

Expand Down
16 changes: 9 additions & 7 deletions internal/pkg/process/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,20 @@ func (a *Analyzer) DiscoverProcessID(target *TargetArgs) (int, error) {
if target.Pid != 0 {
return target.Pid, nil
}

proc, err := os.Open("/proc")
if err != nil {
return 0, err
}
defer proc.Close()

for {
select {
case <-a.done:
a.logger.Info("stopping process id discovery due to kill signal")
return 0, ErrInterrupted
case <-t.C:
pid, err := a.findProcessID(target)
pid, err := a.findProcessID(target, proc)
if err == nil {
a.logger.Info("found process", "pid", pid)
return pid, nil
Expand All @@ -79,12 +86,7 @@ func (a *Analyzer) DiscoverProcessID(target *TargetArgs) (int, error) {
}
}

func (a *Analyzer) findProcessID(target *TargetArgs) (int, error) {
proc, err := os.Open("/proc")
if err != nil {
return 0, err
}

func (a *Analyzer) findProcessID(target *TargetArgs, proc *os.File) (int, error) {
for {
dirs, err := proc.Readdir(15)
if err == io.EOF {
Expand Down
Loading