From 60212bb08ecbcf5254e00d2b23f1b0fa75f53c90 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Tue, 18 Aug 2020 21:42:57 -0700 Subject: [PATCH] Fix race condition when booting wings --- server/resources.go | 14 ++++++++++++-- server/state.go | 5 +++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/server/resources.go b/server/resources.go index e047763..b9be26e 100644 --- a/server/resources.go +++ b/server/resources.go @@ -1,9 +1,9 @@ package server import ( + "encoding/json" "github.com/pterodactyl/wings/environment" "sync" - "sync/atomic" ) // Defines the current resource usage for a given server instance. If a server is offline you @@ -35,6 +35,14 @@ func (s *Server) Proc() *ResourceUsage { return &s.resources } +func (s *Server) emitProcUsage() { + s.resources.mu.RLock() + defer s.resources.mu.RUnlock() + + b, _ := json.Marshal(s.resources) + s.Events().Publish(StatsEvent, string(b)) +} + // Returns the servers current state. func (ru *ResourceUsage) getInternalState() string { ru.mu.RLock() @@ -51,5 +59,7 @@ func (ru *ResourceUsage) setInternalState(state string) { } func (ru *ResourceUsage) SetDisk(i int64) { - atomic.SwapInt64(&ru.Disk, i) + ru.mu.Lock() + ru.Disk = i + ru.mu.Unlock() } diff --git a/server/state.go b/server/state.go index b8802a6..b978520 100644 --- a/server/state.go +++ b/server/state.go @@ -103,10 +103,11 @@ func (s *Server) SetState(state string) error { // Reset the resource usage to 0 when the process fully stops so that all of the UI // views in the Panel correctly display 0. if state == system.ProcessOfflineState { + s.resources.mu.Lock() s.resources.Empty() + s.resources.mu.Unlock() - b, _ := json.Marshal(s.Proc()) - s.Events().Publish(StatsEvent, string(b)) + s.emitProcUsage() } // If server was in an online state, and is now in an offline state we should handle