From e29789114cfd79a741a7117da170018ce1a3f113 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 11 Oct 2020 15:02:37 -0700 Subject: [PATCH] Fix server disk usage not being reported properly; closes pterodactyl/panel#2445 --- server/filesystem/disk_space.go | 10 ++++++++++ server/resources.go | 26 +++++++++++++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/server/filesystem/disk_space.go b/server/filesystem/disk_space.go index 4922b7a..3c7ef79 100644 --- a/server/filesystem/disk_space.go +++ b/server/filesystem/disk_space.go @@ -75,6 +75,16 @@ func (fs *Filesystem) HasSpaceAvailable(allowStaleValue bool) bool { return size <= fs.MaxDisk() } +// Returns the cached value for the amount of disk space used by the filesystem. Do not rely on this +// function for critical logical checks. It should only be used in areas where the actual disk usage +// does not need to be perfect, e.g. API responses for server resource usage. +func (fs *Filesystem) CachedUsage() int64 { + fs.mu.RLock() + defer fs.mu.RUnlock() + + return fs.diskUsed +} + // Internal helper function to allow other parts of the codebase to check the total used disk space // as needed without overly taxing the system. This will prioritize the value from the cache to avoid // excessive IO usage. We will only walk the filesystem and determine the size of the directory if there diff --git a/server/resources.go b/server/resources.go index db7bea8..d3a3d2c 100644 --- a/server/resources.go +++ b/server/resources.go @@ -1,6 +1,7 @@ package server import ( + "encoding/json" "github.com/pterodactyl/wings/environment" "sync" ) @@ -17,17 +18,34 @@ type ResourceUsage struct { // The current server status. State string `json:"state" default:"offline"` - // The current disk space being used by the server. This is cached to prevent slow lookup - // issues on frequent refreshes. + // The current disk space being used by the server. This value is not guaranteed to be accurate + // at all times. It is "manually" set whenever server.Proc() is called. This is kind of just a + // hacky solution for now to avoid passing events all over the place. Disk int64 `json:"disk_bytes"` } +// Alias the resource usage so that we don't infinitely recurse when marshaling the struct. +type IResourceUsage ResourceUsage + +// Custom marshaler to ensure that the object is locked when we're converting it to JSON in +// order to avoid race conditions. +func (ru *ResourceUsage) MarshalJSON() ([]byte, error) { + ru.mu.Lock() + defer ru.mu.Unlock() + + return json.Marshal(IResourceUsage(*ru)) +} + // Returns the resource usage stats for the server instance. If the server is not running, only the // disk space currently used will be returned. When the server is running all of the other stats will // be returned. // // When a process is stopped all of the stats are zeroed out except for the disk. func (s *Server) Proc() *ResourceUsage { + s.resources.SetDisk(s.Filesystem().CachedUsage()) + + // Get a read lock on the resources at this point. Don't do this before setting + // the disk, otherwise you'll cause a deadlock. s.resources.mu.RLock() defer s.resources.mu.RUnlock() @@ -35,11 +53,9 @@ func (s *Server) Proc() *ResourceUsage { } func (s *Server) emitProcUsage() { - s.resources.mu.RLock() - if err := s.Events().PublishJson(StatsEvent, s.resources); err != nil { + if err := s.Events().PublishJson(StatsEvent, s.Proc()); err != nil { s.Log().WithField("error", err).Warn("error while emitting server resource usage to listeners") } - s.resources.mu.RUnlock() } // Returns the servers current state.