Fix race condition when booting wings

This commit is contained in:
Dane Everitt 2020-08-18 21:42:57 -07:00
parent 5b241fdf36
commit 60212bb08e
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 15 additions and 4 deletions

View File

@ -1,9 +1,9 @@
package server package server
import ( import (
"encoding/json"
"github.com/pterodactyl/wings/environment" "github.com/pterodactyl/wings/environment"
"sync" "sync"
"sync/atomic"
) )
// Defines the current resource usage for a given server instance. If a server is offline you // 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 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. // Returns the servers current state.
func (ru *ResourceUsage) getInternalState() string { func (ru *ResourceUsage) getInternalState() string {
ru.mu.RLock() ru.mu.RLock()
@ -51,5 +59,7 @@ func (ru *ResourceUsage) setInternalState(state string) {
} }
func (ru *ResourceUsage) SetDisk(i int64) { func (ru *ResourceUsage) SetDisk(i int64) {
atomic.SwapInt64(&ru.Disk, i) ru.mu.Lock()
ru.Disk = i
ru.mu.Unlock()
} }

View File

@ -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 // Reset the resource usage to 0 when the process fully stops so that all of the UI
// views in the Panel correctly display 0. // views in the Panel correctly display 0.
if state == system.ProcessOfflineState { if state == system.ProcessOfflineState {
s.resources.mu.Lock()
s.resources.Empty() s.resources.Empty()
s.resources.mu.Unlock()
b, _ := json.Marshal(s.Proc()) s.emitProcUsage()
s.Events().Publish(StatsEvent, string(b))
} }
// If server was in an online state, and is now in an offline state we should handle // If server was in an online state, and is now in an offline state we should handle