Skip to content

fix: Add reaper to coder agent - #2441

Merged
sreya merged 12 commits into
mainfrom
jon/reap
Jun 17, 2022
Merged

fix: Add reaper to coder agent#2441
sreya merged 12 commits into
mainfrom
jon/reap

Conversation

@sreya

@sreya sreya commented Jun 16, 2022

Copy link
Copy Markdown
Collaborator
  • The coder agent runs as PID 1 in some of our Docker workspaces.
    In such cases it is the responsibility of the init process to
    reap dead processes. Failing to do so can result in an inability
    to create new processes by running out of PIDs.

    This PR adds a reaper to our agent that is only spawned if it
    detects that it is PID1.

resolves #2398

sreya added 3 commits June 16, 2022 20:27
- The coder agent runs as PID 1 in some of our Docker workspaces.
  In such cases it is the responsibility of the init process to
  reap dead processes. Failing to do so can result in an inability
  to create new processes by running out of PIDs.

  This PR adds a reaper to our agent that is only spawned if it
  detects that it is PID1.
@sreya
sreya requested review from deansheather and kylecarbs June 16, 2022 21:20
@sreya

sreya commented Jun 16, 2022

Copy link
Copy Markdown
Collaborator Author

@kylecarbs what do you think about skipping the test if it's in CI, I'm a little concerned about having a forkexec in our tests...

@sreya
sreya requested a review from mafredri June 16, 2022 21:59
Comment thread agent/reaper/reaper.go Outdated
Comment thread agent/reaper/reaper.go Outdated

@mafredri mafredri left a comment

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.

Really nice solution!

Comment thread agent/reaper/reaper_unix.go
pattrs := &syscall.ProcAttr{
Dir: pwd,
// Add our marker for identifying the child process.
Env: append(os.Environ(), fmt.Sprintf("%s=true", agentEnvMark)),

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.

Should we filter this out from sub-process envs (i.e. when we launch shells)? It probably doesn't matter for correctness, more of a cleanliness thing.

We could also add CODER_AGENT_PID as part of the sub-process envs for programmatic use (e.g. to enable pprof, kill -USR1 $CODER_AGENT_PID). We'd need to do that always though, not only when forking.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I agree that it's certainly cleaner to filter it out. The only reason I didn't is because I did not want implementation details of the reaper package to leak outside. In other words, if you're developing the agent I don't want people to have to think about using a special function that filters out arcane environment variables that you don't understand.

I think if it becomes an issue we devote some time to figuring out something unobtrusive.

@sreya sreya Jun 17, 2022

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I already merged this but I think a cleaner solution is to have a --noreap flag that we append to the forkexec cmd. Then we don't pollute subprocess envs or leak to the world whats happening under the hood. What do you think @mafredri

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 makes sense and I agree that's a clean solution @sreya. 👍🏻

Comment thread cli/agent.go
defer logWriter.Close()
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()), sloghuman.Sink(logWriter)).Leveled(slog.LevelDebug)

isLinux := runtime.GOOS == "linux"

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.

Any reason to limit this to linux only? I suppose this should work on Darwin and other BSD flavors too?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It was causing things to fail when it ran on macos. I don't think limiting it to linux is going to be a big deal because the reaper is primarily going to be used for docker containers and on mac that's going to be run in a linux vm anyway.

@sreya
sreya merged commit 18973a6 into main Jun 17, 2022
@sreya
sreya deleted the jon/reap branch June 17, 2022 16:51
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.

Workspace processes are not reaped

3 participants