Wait for the power lock when stopping or killing a booting server

Fixes pterodactyl/panel#5712
This commit is contained in:
dualfroz committed 2026-10-16 09:53:58 +02:00
1 parent 1bb0c10f5e
commit 0ae3a4b574
4 files changed
+189 -2

No files matched your search

+4 -1
View File
@@ -86,7 +86,10 @@ func postServerPower(c *gin.Context) {
// we can immediately return a response from the server. Some of these actions
// can take quite some time, especially stopping or restarting.
go func(s *server.Server) {
if data.WaitSeconds < 0 || data.WaitSeconds > 300 {
// The response has already been sent, so an action that cannot get the power lock
// right away would be dropped without the caller knowing. Wait for the lock by default
// so that a stop or kill sent while the server is booting runs once the boot finishes.
if data.WaitSeconds <= 0 || data.WaitSeconds > 300 {
data.WaitSeconds = 30
}
if err := s.HandlePowerAction(data.Action, data.WaitSeconds); err != nil {
+68
View File
@@ -0,0 +1,68 @@
package router
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/apex/log"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
"github.com/pterodactyl/wings/environment"
wserver "github.com/pterodactyl/wings/server"
)
// powerTestEnvironment reports each stop on stops and holds it until release is closed.
type powerTestEnvironment struct {
environment.ProcessEnvironment
stops chan struct{}
release chan struct{}
}
func (e *powerTestEnvironment) WaitForStop(context.Context, time.Duration, bool) error {
e.stops <- struct{}{}
<-e.release
return nil
}
// A stop sent without wait_seconds while another power action holds the lock runs
// once the lock is released, instead of being dropped after the 202 response.
func TestStopWithoutWaitSecondsWaitsForPowerLock(t *testing.T) {
s, err := wserver.New(nil)
require.NoError(t, err)
t.Cleanup(s.CtxCancel)
env := &powerTestEnvironment{stops: make(chan struct{}, 2), release: make(chan struct{})}
s.Environment = env
go func() { _ = s.HandlePowerAction(wserver.PowerActionStop) }()
<-env.stops
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodPost, "/api/servers/test/power", strings.NewReader(`{"action":"stop"}`))
c.Request.Header.Set("Content-Type", "application/json")
c.Set("server", s)
c.Set("logger", log.WithField("test", t.Name()))
postServerPower(c)
require.Equal(t, http.StatusAccepted, c.Writer.Status())
select {
case <-env.stops:
t.Fatal("the stop ran while another power action held the lock")
case <-time.After(100 * time.Millisecond):
}
close(env.release)
select {
case <-env.stops:
case <-time.After(2 * time.Second):
t.Fatal("the stop was dropped instead of waiting for the power lock")
}
}