Wait for the power lock when stopping or killing a booting server
Fixes pterodactyl/panel#5712
This commit is contained in:
1 parent
1bb0c10f5e
commit
0ae3a4b574
4 files changed
+189
-2
No files matched your search
+24
-1
@@ -113,12 +113,27 @@ func (s *Server) HandlePowerAction(action PowerAction, waitSeconds ...int) error
|
||||
//
|
||||
// If we did successfully acquire the lock, make sure we release it once we're done
|
||||
// executiong the power actions.
|
||||
if err := s.powerLock.Acquire(); err == nil {
|
||||
err := s.powerLock.Acquire()
|
||||
|
||||
// A lock held while the server is booting belongs to a start or restart that has not
|
||||
// created the container yet, so there is nothing to kill. Wait for the boot like any
|
||||
// other action instead of marking a server that is still starting as offline.
|
||||
booting := errors.Is(err, system.ErrLockerLocked) && s.isBooting()
|
||||
if booting && wait > 0 {
|
||||
ctx, cancel := context.WithTimeout(s.ctx, time.Second*time.Duration(wait))
|
||||
defer cancel()
|
||||
|
||||
err = s.powerLock.TryAcquire(ctx)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
log.Info("acquired exclusive lock on power actions, processing event...")
|
||||
defer cleanup()
|
||||
} else if errors.Is(err, system.ErrLockerDestroyed) {
|
||||
// The server is being deleted.
|
||||
return err
|
||||
} else if booting {
|
||||
return errors.Wrap(err, "failed to acquire exclusive lock for power actions while the server is starting")
|
||||
} else {
|
||||
log.Warn("failed to acquire exclusive lock, ignoring failure for termination event")
|
||||
}
|
||||
@@ -170,6 +185,14 @@ func (s *Server) HandlePowerAction(action PowerAction, waitSeconds ...int) error
|
||||
return errors.New("attempting to handle unknown power action")
|
||||
}
|
||||
|
||||
// isBooting reports whether the server process is offline or starting. When the power lock
|
||||
// is held in one of these states, a start or restart is running its pre-boot steps or
|
||||
// creating and starting the container.
|
||||
func (s *Server) isBooting() bool {
|
||||
st := s.Environment.State()
|
||||
return st == environment.ProcessOfflineState || st == environment.ProcessStartingState
|
||||
}
|
||||
|
||||
// Execute a few functions before actually calling the environment start commands. This ensures
|
||||
// that everything is ready to go for environment booting, and that the server can even be started.
|
||||
func (s *Server) onBeforeStart() error {
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"emperror.dev/errors"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/pterodactyl/wings/environment"
|
||||
"github.com/pterodactyl/wings/system"
|
||||
)
|
||||
|
||||
// bootTestEnvironment reports a fixed process state and records kill signals.
|
||||
type bootTestEnvironment struct {
|
||||
environment.ProcessEnvironment
|
||||
state string
|
||||
terminated chan struct{}
|
||||
}
|
||||
|
||||
func (e *bootTestEnvironment) State() string { return e.state }
|
||||
|
||||
func (e *bootTestEnvironment) Terminate(context.Context, string) error {
|
||||
close(e.terminated)
|
||||
return nil
|
||||
}
|
||||
|
||||
func newBootTestServer(t *testing.T, state string) (*Server, *bootTestEnvironment) {
|
||||
s, err := New(nil)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(s.CtxCancel)
|
||||
env := &bootTestEnvironment{state: state, terminated: make(chan struct{})}
|
||||
s.Environment = env
|
||||
return s, env
|
||||
}
|
||||
|
||||
// A kill sent while a boot holds the power lock runs once the boot releases it.
|
||||
func TestKillWhileBootingWaitsForBoot(t *testing.T) {
|
||||
s, env := newBootTestServer(t, environment.ProcessStartingState)
|
||||
require.NoError(t, s.powerLock.Acquire())
|
||||
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- s.HandlePowerAction(PowerActionTerminate, 5) }()
|
||||
|
||||
select {
|
||||
case <-env.terminated:
|
||||
t.Fatal("the server was killed before the boot released the power lock")
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
}
|
||||
|
||||
s.powerLock.Release()
|
||||
|
||||
select {
|
||||
case <-env.terminated:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("the server was not killed after the boot released the power lock")
|
||||
}
|
||||
require.NoError(t, <-done)
|
||||
require.False(t, s.ExecutingPowerAction())
|
||||
}
|
||||
|
||||
// A kill sent while a boot holds the power lock, without a wait, reports the lock
|
||||
// instead of marking the server offline.
|
||||
func TestKillWhileBootingWithoutWaitReturnsLockError(t *testing.T) {
|
||||
s, env := newBootTestServer(t, environment.ProcessOfflineState)
|
||||
require.NoError(t, s.powerLock.Acquire())
|
||||
|
||||
err := s.HandlePowerAction(PowerActionTerminate)
|
||||
require.True(t, errors.Is(err, system.ErrLockerLocked), "got %v", err)
|
||||
|
||||
select {
|
||||
case <-env.terminated:
|
||||
t.Fatal("the server was killed while it was booting")
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
// A kill still passes through the power lock held by a stop, so a server that is
|
||||
// stuck stopping can be killed.
|
||||
func TestKillWhileStoppingSkipsLock(t *testing.T) {
|
||||
s, env := newBootTestServer(t, environment.ProcessStoppingState)
|
||||
require.NoError(t, s.powerLock.Acquire())
|
||||
|
||||
require.NoError(t, s.HandlePowerAction(PowerActionTerminate, 5))
|
||||
|
||||
select {
|
||||
case <-env.terminated:
|
||||
default:
|
||||
t.Fatal("the server was not killed while stopping")
|
||||
}
|
||||
require.True(t, s.ExecutingPowerAction())
|
||||
}
|
||||
Reference in new issue
Block a user