2019-12-01 00:43:18 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2019-12-01 01:08:11 +00:00
|
|
|
"fmt"
|
2020-12-06 20:56:17 +00:00
|
|
|
"strconv"
|
2020-07-19 23:27:55 +00:00
|
|
|
"sync"
|
2019-12-01 00:43:18 +00:00
|
|
|
"time"
|
2021-01-10 01:22:39 +00:00
|
|
|
|
2023-01-17 18:47:27 +00:00
|
|
|
"emperror.dev/errors"
|
|
|
|
|
2021-01-10 01:22:39 +00:00
|
|
|
"github.com/pterodactyl/wings/config"
|
|
|
|
"github.com/pterodactyl/wings/environment"
|
2019-12-01 00:43:18 +00:00
|
|
|
)
|
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
type CrashHandler struct {
|
|
|
|
mu sync.RWMutex
|
2019-12-01 00:43:18 +00:00
|
|
|
|
|
|
|
// Tracks the time of the last server crash event.
|
|
|
|
lastCrash time.Time
|
|
|
|
}
|
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
// Returns the time of the last crash for this server instance.
|
|
|
|
func (cd *CrashHandler) LastCrashTime() time.Time {
|
|
|
|
cd.mu.RLock()
|
|
|
|
defer cd.mu.RUnlock()
|
|
|
|
|
|
|
|
return cd.lastCrash
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the last crash time for a server.
|
|
|
|
func (cd *CrashHandler) SetLastCrash(t time.Time) {
|
|
|
|
cd.mu.Lock()
|
|
|
|
cd.lastCrash = t
|
|
|
|
cd.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-12-01 00:43:18 +00:00
|
|
|
// Looks at the environment exit state to determine if the process exited cleanly or
|
|
|
|
// if it was the result of an event that we should try to recover from.
|
|
|
|
//
|
|
|
|
// This function assumes it is called under circumstances where a crash is suspected
|
2020-09-05 19:08:40 +00:00
|
|
|
// of occurring. It will not do anything to determine if it was actually a crash, just
|
2019-12-01 00:43:18 +00:00
|
|
|
// look at the exit state and check if it meets the criteria of being called a crash
|
|
|
|
// by Wings.
|
|
|
|
//
|
|
|
|
// If the server is determined to have crashed, the process will be restarted and the
|
|
|
|
// counter for the server will be incremented.
|
|
|
|
func (s *Server) handleServerCrash() error {
|
|
|
|
// No point in doing anything here if the server isn't currently offline, there
|
|
|
|
// is no reason to do a crash detection event. If the server crash detection is
|
|
|
|
// disabled we want to skip anything after this as well.
|
2020-11-20 21:35:29 +00:00
|
|
|
if s.Environment.State() != environment.ProcessOfflineState || !s.Config().CrashDetectionEnabled {
|
2020-07-19 23:27:55 +00:00
|
|
|
if !s.Config().CrashDetectionEnabled {
|
2020-06-13 17:26:35 +00:00
|
|
|
s.Log().Debug("server triggered crash detection but handler is disabled for server process")
|
2020-12-06 20:56:17 +00:00
|
|
|
s.PublishConsoleOutputFromDaemon("Aborting automatic restart, crash detection is disabled for this instance.")
|
2019-12-01 00:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
exitCode, oomKilled, err := s.Environment.ExitState()
|
|
|
|
if err != nil {
|
2023-01-17 18:47:27 +00:00
|
|
|
return errors.Wrap(err, "failed to get exit state for server process")
|
2019-12-01 00:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the system is not configured to detect a clean exit code as a crash, and the
|
|
|
|
// crash is not the result of the program running out of memory, do nothing.
|
2020-12-06 20:56:17 +00:00
|
|
|
if exitCode == 0 && !oomKilled && !config.Get().System.CrashDetection.DetectCleanExitAsCrash {
|
2020-06-13 17:26:35 +00:00
|
|
|
s.Log().Debug("server exited with successful exit code; system is configured to not detect this as a crash")
|
2019-12-01 00:43:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-18 22:04:26 +00:00
|
|
|
s.PublishConsoleOutputFromDaemon("---------- Detected server process in a crashed state! ----------")
|
|
|
|
s.PublishConsoleOutputFromDaemon(fmt.Sprintf("Exit code: %d", exitCode))
|
|
|
|
s.PublishConsoleOutputFromDaemon(fmt.Sprintf("Out of memory: %t", oomKilled))
|
2019-12-01 01:08:11 +00:00
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
c := s.crasher.LastCrashTime()
|
2020-12-06 20:56:17 +00:00
|
|
|
timeout := config.Get().System.CrashDetection.Timeout
|
2019-12-01 01:08:11 +00:00
|
|
|
|
2020-12-06 20:56:17 +00:00
|
|
|
// If the last crash time was within the last `timeout` seconds we do not want to perform
|
|
|
|
// an automatic reboot of the process. Return an error that can be handled.
|
|
|
|
//
|
|
|
|
// If timeout is set to 0, always reboot the server (this is probably a terrible idea, but some people want it)
|
|
|
|
if timeout != 0 && !c.IsZero() && c.Add(time.Second*time.Duration(config.Get().System.CrashDetection.Timeout)).After(time.Now()) {
|
|
|
|
s.PublishConsoleOutputFromDaemon("Aborting automatic restart, last crash occurred less than " + strconv.Itoa(timeout) + " seconds ago.")
|
2019-12-01 00:43:18 +00:00
|
|
|
return &crashTooFrequent{}
|
|
|
|
}
|
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
s.crasher.SetLastCrash(time.Now())
|
2019-12-01 00:43:18 +00:00
|
|
|
|
2023-01-17 18:47:27 +00:00
|
|
|
return errors.Wrap(s.HandlePowerAction(PowerActionStart), "failed to start server after crash detection")
|
2020-09-05 19:08:40 +00:00
|
|
|
}
|