Skip to content

Commit

Permalink
feat(debug): Add some self-checks to hunt for stale connections
Browse files Browse the repository at this point in the history
  • Loading branch information
ashquarky committed Jan 2, 2025
1 parent cc0c0ca commit 4765018
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main

import (
"github.com/PretendoNetwork/minecraft-wiiu/globals"
"sync"
"time"

"github.com/PretendoNetwork/minecraft-wiiu/nex"
nex2 "github.com/PretendoNetwork/nex-go/v2"
)

var wg sync.WaitGroup
Expand All @@ -14,6 +17,61 @@ func main() {
// TODO - Add gRPC server
go nex.StartAuthenticationServer()
go nex.StartSecureServer()
go startSelfTesting()

wg.Wait()
}

func selfTest() {
globals.Logger.Info("Self-testing...")
var errors = 0
var seenPids map[uint64]struct{}
// apparently we hold a lock all the way thru this
globals.SecureEndpoint.Connections.Each(func(key string, pc *nex2.PRUDPConnection) bool {
if pc.PID() == nil || pc.PID().Value() == 0 {
if pc.ConnectionState == nex2.StateConnected {
globals.Logger.Warningf("PID connection invariant failed: %v %#v", key, pc)
errors++
}
// nil entry is ok but kinda weird
return false
}
pid := pc.PID().Value()
// expected invariant: valid connections do not have PIDs
if pc.ConnectionState != nex2.StateConnected {
globals.Logger.Warningf("Stale connection invariant failed: %v %#v", key, pc)
errors++
}
if _, ok := seenPids[pid]; ok {
globals.Logger.Warningf("Duplicate connection for PID: %v %#v", key, pc)
errors++
}
seenPids[pid] = struct{}{}
// check SocketConnections
var found = false
globals.SecureServer.Connections.Each(func(key string, sc *nex2.SocketConnection) bool {
return sc.Connections.Each(func(key uint8, pc2 *nex2.PRUDPConnection) bool {
if pc2 == pc {
if found {
globals.Logger.Warningf("Duplicate SocketConnection: %v %#v", key, pc)
errors++
}
found = true
return true
}
return false
})
})
if !found {
globals.Logger.Warningf("Connection has no SocketConnection: %v %#v", key, pc)
errors++
}
return false
})
globals.Logger.Infof("Self-test finished with %v errors", errors)
}
func startSelfTesting() {
for range time.Tick(10 * time.Second) {
selfTest()
}
}

0 comments on commit 4765018

Please sign in to comment.