fix: vscode not being killed (#284)

This commit is contained in:
Carlos Florêncio
2026-02-25 21:10:28 +00:00
committed by GitHub
parent 5132144db7
commit 9f24b9664c
3 changed files with 18 additions and 11 deletions
@@ -19,7 +19,13 @@ func (l *Launcher) gracefulStop(pid int) error {
return nil
}
// forceKill sends SIGKILL to the process.
// forceKill sends SIGKILL to the entire process group.
// Since agentctl starts with Setpgid=true, its PID == PGID, so killing -pid
// terminates agentctl and all children that haven't created their own groups
// (including code-server and its Node.js worker processes).
func (l *Launcher) forceKill(pid int) {
_ = syscall.Kill(pid, syscall.SIGKILL)
if err := syscall.Kill(-pid, syscall.SIGKILL); err != nil {
// Fallback: kill the single process if group kill fails
_ = syscall.Kill(pid, syscall.SIGKILL)
}
}
@@ -19,7 +19,7 @@ func Provide(ctx context.Context, cfg Config, log *logger.Logger) (*Launcher, fu
cleanup := func() error {
var stopErr error
stopOnce.Do(func() {
stopCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
stopCtx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
stopErr = launcher.Stop(stopCtx)
})
@@ -173,7 +173,9 @@ func (v *VscodeManager) startProcess(ctx context.Context, binaryPath string) err
v.cmd = exec.Command(binaryPath, args...)
v.cmd.Dir = workDir
v.workDir = workDir
setProcGroup(v.cmd)
// Do NOT set a new process group for code-server.
// It inherits agentctl's process group so that a force-kill of agentctl's
// process group also terminates code-server and its Node.js workers.
stderr, err := v.cmd.StderrPipe()
if err != nil {
@@ -324,13 +326,12 @@ func (v *VscodeManager) Stop(ctx context.Context) error {
v.mu.Unlock()
if cmd != nil && cmd.Process != nil {
// Try graceful shutdown via process group kill
pid := cmd.Process.Pid
if err := killProcessGroup(pid); err != nil {
v.logger.Debug("failed to kill code-server process group, trying single process", zap.Error(err))
if err := cmd.Process.Kill(); err != nil {
v.logger.Warn("failed to kill code-server process", zap.Error(err))
}
// Kill code-server directly. code-server shares agentctl's process group,
// so we must not use killProcessGroup here (that would kill agentctl itself).
// Worker processes inheriting the same pgid are cleaned up when agentctl's
// process group is force-killed on shutdown.
if err := cmd.Process.Kill(); err != nil {
v.logger.Warn("failed to kill code-server process", zap.Error(err))
}
}