Skip to content

Commit 53722ba

Browse files
iQQBotcsweichel
andcommitted
[supervisor] fixes not recycle zombie processes
Co-authored-by: Christian Weichel <chris@gitpod.io>
1 parent eee9847 commit 53722ba

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

components/supervisor/pkg/supervisor/supervisor.go

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -500,39 +500,37 @@ func reaper(terminatingReaper <-chan bool) {
500500
case terminating = <-terminatingReaper:
501501
continue
502502
}
503-
504-
// wait on the process, hence remove it from the process table
505-
pid, err := unix.Wait4(-1, nil, 0, nil)
506-
// if we've been interrupted, try again until we're done
507-
for err == syscall.EINTR {
508-
pid, err = unix.Wait4(-1, nil, 0, nil)
509-
}
510-
if err == unix.ECHILD {
511-
// The calling process does not have any unwaited-for children.
512-
// Not really an error for us
513-
err = nil
514-
}
515-
if err != nil {
516-
log.WithField("pid", pid).WithError(err).Debug("cannot call waitpid() for re-parented child")
517-
}
518-
519-
if !terminating {
520-
continue
521-
}
522-
proc, err := os.FindProcess(pid)
523-
if err != nil {
524-
log.WithField("pid", pid).WithError(err).Debug("cannot find re-parented process")
525-
continue
526-
}
527-
err = proc.Signal(syscall.SIGTERM)
528-
if err != nil {
529-
if !strings.Contains(err.Error(), "os: process already finished") {
530-
log.WithField("pid", pid).WithError(err).Debug("cannot send SIGTERM to re-parented process")
503+
for {
504+
// wait on the process, hence remove it from the process table
505+
pid, err := unix.Wait4(-1, nil, 0, nil)
506+
// if we've been interrupted, try again until we're done
507+
for err == syscall.EINTR {
508+
pid, err = unix.Wait4(-1, nil, 0, nil)
531509
}
532-
533-
continue
510+
// The calling process does not have any unwaited-for children. Let's wait for a SIGCHLD notification.
511+
if err == unix.ECHILD {
512+
break
513+
}
514+
if err != nil {
515+
log.WithField("pid", pid).WithError(err).Debug("cannot call waitpid() for re-parented child")
516+
}
517+
if !terminating {
518+
continue
519+
}
520+
proc, err := os.FindProcess(pid)
521+
if err != nil {
522+
log.WithField("pid", pid).WithError(err).Debug("cannot find re-parented process")
523+
continue
524+
}
525+
err = proc.Signal(syscall.SIGTERM)
526+
if err != nil {
527+
if !strings.Contains(err.Error(), "os: process already finished") {
528+
log.WithField("pid", pid).WithError(err).Debug("cannot send SIGTERM to re-parented process")
529+
}
530+
continue
531+
}
532+
log.WithField("pid", pid).Debug("SIGTERM'ed reparented child process")
534533
}
535-
log.WithField("pid", pid).Debug("SIGTERM'ed reparented child process")
536534
}
537535
}
538536

0 commit comments

Comments
 (0)