mirror of
https://github.com/gomods/athens
synced 2026-02-03 12:10:32 +00:00
feat: add handling for SIGCHLD to cleanup child processes (#2043)
This commit is contained in:
+25
-25
@@ -63,22 +63,6 @@ func main() {
|
|||||||
Handler: handler,
|
Handler: handler,
|
||||||
ReadHeaderTimeout: 2 * time.Second,
|
ReadHeaderTimeout: 2 * time.Second,
|
||||||
}
|
}
|
||||||
idleConnsClosed := make(chan struct{})
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
sigint := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(sigint, shutdown.GetSignals()...)
|
|
||||||
s := <-sigint
|
|
||||||
logger.WithField("signal", s).Infof("Received signal, shutting down server")
|
|
||||||
|
|
||||||
// We received an interrupt signal, shut down.
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(conf.ShutdownTimeout))
|
|
||||||
defer cancel()
|
|
||||||
if err := srv.Shutdown(ctx); err != nil {
|
|
||||||
logger.WithError(err).Fatal("Could not shut down server")
|
|
||||||
}
|
|
||||||
close(idleConnsClosed)
|
|
||||||
}()
|
|
||||||
|
|
||||||
if conf.EnablePprof {
|
if conf.EnablePprof {
|
||||||
go func() {
|
go func() {
|
||||||
@@ -111,15 +95,31 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if conf.TLSCertFile != "" && conf.TLSKeyFile != "" {
|
signalCtx, signalStop := signal.NotifyContext(context.Background(), shutdown.GetSignals()...)
|
||||||
err = srv.ServeTLS(ln, conf.TLSCertFile, conf.TLSKeyFile)
|
reaper := shutdown.ChildProcReaper(signalCtx, logger.Logger)
|
||||||
} else {
|
|
||||||
err = srv.Serve(ln)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !errors.Is(err, http.ErrServerClosed) {
|
go func() {
|
||||||
logger.WithError(err).Fatal("Could not start server")
|
defer signalStop()
|
||||||
}
|
if conf.TLSCertFile != "" && conf.TLSKeyFile != "" {
|
||||||
|
err = srv.ServeTLS(ln, conf.TLSCertFile, conf.TLSKeyFile)
|
||||||
|
} else {
|
||||||
|
err = srv.Serve(ln)
|
||||||
|
}
|
||||||
|
|
||||||
<-idleConnsClosed
|
if !errors.Is(err, http.ErrServerClosed) {
|
||||||
|
logger.WithError(err).Fatal("Could not start server")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Wait for shutdown signal, then cleanup before exit.
|
||||||
|
<-signalCtx.Done()
|
||||||
|
logger.Infof("Shutting down server")
|
||||||
|
|
||||||
|
// We received an interrupt signal, shut down.
|
||||||
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(conf.ShutdownTimeout))
|
||||||
|
defer cancel()
|
||||||
|
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||||
|
logger.WithError(err).Fatal("Could not shut down server")
|
||||||
|
}
|
||||||
|
<-reaper.Done()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,13 @@
|
|||||||
package shutdown
|
package shutdown
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetSignals returns the appropriate signals to catch for a clean shutdown, dependent on the OS.
|
// GetSignals returns the appropriate signals to catch for a clean shutdown, dependent on the OS.
|
||||||
@@ -13,3 +18,42 @@ import (
|
|||||||
func GetSignals() []os.Signal {
|
func GetSignals() []os.Signal {
|
||||||
return []os.Signal{os.Interrupt, syscall.SIGTERM}
|
return []os.Signal{os.Interrupt, syscall.SIGTERM}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ChildProcReaper spawns a goroutine to listen for SIGCHLD signals to cleanup
|
||||||
|
// zombie child processes. The returned context will be canceled once all child
|
||||||
|
// processes have been cleaned up, and should be waited on before exiting.
|
||||||
|
//
|
||||||
|
// This only applies to Unix platforms, and returns an already canceled context
|
||||||
|
// on Windows.
|
||||||
|
func ChildProcReaper(ctx context.Context, logger logrus.FieldLogger) context.Context {
|
||||||
|
sigChld := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigChld, syscall.SIGCHLD)
|
||||||
|
done, cancel := context.WithCancel(context.WithoutCancel(ctx))
|
||||||
|
go func() {
|
||||||
|
defer cancel()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
reap(logger)
|
||||||
|
return
|
||||||
|
case <-sigChld:
|
||||||
|
reap(logger)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return done
|
||||||
|
}
|
||||||
|
|
||||||
|
func reap(logger logrus.FieldLogger) {
|
||||||
|
for {
|
||||||
|
var wstatus syscall.WaitStatus
|
||||||
|
pid, err := syscall.Wait4(-1, &wstatus, syscall.WNOHANG, nil)
|
||||||
|
if err != nil && !errors.Is(err, syscall.ECHILD) {
|
||||||
|
logger.Errorf("failed to reap child process: %v", err)
|
||||||
|
continue
|
||||||
|
} else if pid <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
logger.Infof("reaped child process %v, exit status: %v", pid, wstatus.ExitStatus())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,9 +2,26 @@
|
|||||||
|
|
||||||
package shutdown
|
package shutdown
|
||||||
|
|
||||||
import "os"
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
// GetSignals returns the appropriate signals to catch for a clean shutdown, dependent on the OS.
|
// GetSignals returns the appropriate signals to catch for a clean shutdown, dependent on the OS.
|
||||||
func GetSignals() []os.Signal {
|
func GetSignals() []os.Signal {
|
||||||
return []os.Signal{os.Interrupt}
|
return []os.Signal{os.Interrupt}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ChildProcReaper spawns a goroutine to listen for SIGCHLD signals to cleanup
|
||||||
|
// zombie child processes. The returned context will be canceled once all child
|
||||||
|
// processes have been cleaned up, and should be waited on before exiting.
|
||||||
|
//
|
||||||
|
// This only applies to Unix platforms, and returns an already canceled context
|
||||||
|
// on Windows.
|
||||||
|
func ChildProcReaper(ctx context.Context, logger logrus.FieldLogger) context.Context {
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
defer cancel()
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user