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

Graceful fixes #8645

Merged
merged 6 commits into from
Oct 23, 2019
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
2 changes: 2 additions & 0 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"strings"

"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/routers"
Expand Down Expand Up @@ -226,6 +227,7 @@ func runWeb(ctx *cli.Context) error {
log.Critical("Failed to start server: %v", err)
}
log.Info("HTTP Listener: %s Closed", listenAddr)
graceful.WaitForServers()
log.Close()
return nil
}
5 changes: 5 additions & 0 deletions modules/graceful/graceful_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ package graceful

// This file contains shims for windows builds
const IsChild = false

// WaitForServers waits for all running servers to finish
func WaitForServers() {

}
16 changes: 16 additions & 0 deletions modules/graceful/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,24 @@ import (
"os"
"os/exec"
"strings"
"sync"
"syscall"
)

var killParent sync.Once

// KillParent sends the kill signal to the parent process if we are a child
func KillParent() {
killParent.Do(func() {
if IsChild {
ppid := syscall.Getppid()
if ppid > 1 {
_ = syscall.Kill(ppid, syscall.SIGTERM)
}
}
})
}

// RestartProcess starts a new process passing it the active listeners. It
// doesn't fork, but starts a new process using the same environment and
// arguments as when it was originally started. This allows for a newly
Expand Down
17 changes: 11 additions & 6 deletions modules/graceful/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
var (
// RWMutex for when adding servers or shutting down
runningServerReg sync.RWMutex
runningServerWG sync.WaitGroup
// ensure we only fork once
runningServersForked bool

Expand All @@ -47,6 +48,7 @@ var (

func init() {
runningServerReg = sync.RWMutex{}
runningServerWG = sync.WaitGroup{}

DefaultMaxHeaderBytes = 0 // use http.DefaultMaxHeaderBytes - which currently is 1 << 20 (1MB)
}
Expand All @@ -69,6 +71,11 @@ type Server struct {
OnShutdown func()
}

// WaitForServers waits for all running servers to finish
func WaitForServers() {
runningServerWG.Wait()
}

// NewServer creates a server on network at provided address
func NewServer(network, address string) *Server {
runningServerReg.Lock()
Expand Down Expand Up @@ -110,9 +117,7 @@ func (srv *Server) ListenAndServe(serve ServeFunction) error {

srv.listener = newWrappedListener(l, srv)

if IsChild {
_ = syscall.Kill(syscall.Getppid(), syscall.SIGTERM)
}
KillParent()

srv.BeforeBegin(srv.network, srv.address)

Expand Down Expand Up @@ -156,9 +161,7 @@ func (srv *Server) ListenAndServeTLSConfig(tlsConfig *tls.Config, serve ServeFun
wl := newWrappedListener(l, srv)
srv.listener = tls.NewListener(wl, tlsConfig)

if IsChild {
_ = syscall.Kill(syscall.Getppid(), syscall.SIGTERM)
}
KillParent()
srv.BeforeBegin(srv.network, srv.address)

return srv.Serve(serve)
Expand All @@ -175,10 +178,12 @@ func (srv *Server) ListenAndServeTLSConfig(tlsConfig *tls.Config, serve ServeFun
func (srv *Server) Serve(serve ServeFunction) error {
defer log.Debug("Serve() returning... (PID: %d)", syscall.Getpid())
srv.setState(stateRunning)
runningServerWG.Add(1)
err := serve(srv.listener)
log.Debug("Waiting for connections to finish... (PID: %d)", syscall.Getpid())
srv.wg.Wait()
srv.setState(stateTerminate)
runningServerWG.Done()
// use of closed means that the listeners are closed - i.e. we should be shutting down - return nil
if err != nil && strings.Contains(err.Error(), "use of closed") {
return nil
Expand Down
2 changes: 1 addition & 1 deletion modules/graceful/server_signals.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (srv *Server) handleSignals() {
if setting.GracefulRestartable {
log.Info("PID: %d. Received SIGHUP. Forking...", pid)
err := srv.fork()
if err != nil {
if err != nil && err.Error() != "another process already forked. Ignoring this one" {
log.Error("Error whilst forking from PID: %d : %v", pid, err)
}
} else {
Expand Down