Dump goroutines when possible deadlock is detected (#568)
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
package routing
|
package routing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"runtime/pprof"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
@@ -22,6 +25,7 @@ const (
|
|||||||
// expire participant mappings after a day
|
// expire participant mappings after a day
|
||||||
participantMappingTTL = 24 * time.Hour
|
participantMappingTTL = 24 * time.Hour
|
||||||
statsUpdateInterval = 2 * time.Second
|
statsUpdateInterval = 2 * time.Second
|
||||||
|
statsMaxDelaySeconds = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
// RedisRouter uses Redis pub/sub to route signaling messages across different nodes
|
// RedisRouter uses Redis pub/sub to route signaling messages across different nodes
|
||||||
@@ -33,6 +37,7 @@ type RedisRouter struct {
|
|||||||
rc *redis.Client
|
rc *redis.Client
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
isStarted atomic.Bool
|
isStarted atomic.Bool
|
||||||
|
statsMu sync.Mutex
|
||||||
|
|
||||||
pubsub *redis.PubSub
|
pubsub *redis.PubSub
|
||||||
cancel func()
|
cancel func()
|
||||||
@@ -338,18 +343,35 @@ func (r *RedisRouter) getParticipantSignalNode(connectionID livekit.ConnectionID
|
|||||||
|
|
||||||
// update node stats and cleanup
|
// update node stats and cleanup
|
||||||
func (r *RedisRouter) statsWorker() {
|
func (r *RedisRouter) statsWorker() {
|
||||||
|
goroutineDumped := false
|
||||||
for r.ctx.Err() == nil {
|
for r.ctx.Err() == nil {
|
||||||
// update periodically seconds
|
// update periodically
|
||||||
select {
|
select {
|
||||||
case <-time.After(statsUpdateInterval):
|
case <-time.After(statsUpdateInterval):
|
||||||
_ = r.WriteNodeRTC(context.Background(), r.currentNode.Id, &livekit.RTCNodeMessage{
|
_ = r.WriteNodeRTC(context.Background(), r.currentNode.Id, &livekit.RTCNodeMessage{
|
||||||
Message: &livekit.RTCNodeMessage_KeepAlive{},
|
Message: &livekit.RTCNodeMessage_KeepAlive{},
|
||||||
})
|
})
|
||||||
|
r.statsMu.Lock()
|
||||||
|
stats := r.currentNode.Stats
|
||||||
|
r.statsMu.Unlock()
|
||||||
|
|
||||||
|
delaySeconds := time.Now().Unix() - stats.UpdatedAt
|
||||||
|
if delaySeconds > statsMaxDelaySeconds {
|
||||||
|
if !goroutineDumped {
|
||||||
|
goroutineDumped = true
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
_ = pprof.Lookup("goroutine").WriteTo(buf, 2)
|
||||||
|
logger.Errorw("status update delayed, possible deadlock", nil,
|
||||||
|
"delay", delaySeconds,
|
||||||
|
"goroutines", buf.String())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
goroutineDumped = false
|
||||||
|
}
|
||||||
case <-r.ctx.Done():
|
case <-r.ctx.Done():
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// worker that consumes redis messages intended for this node
|
// worker that consumes redis messages intended for this node
|
||||||
@@ -455,12 +477,14 @@ func (r *RedisRouter) handleRTCMessage(rm *livekit.RTCNodeMessage) error {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r.statsMu.Lock()
|
||||||
updated, err := prometheus.GetUpdatedNodeStats(r.currentNode.Stats)
|
updated, err := prometheus.GetUpdatedNodeStats(r.currentNode.Stats)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorw("could not update node stats", err)
|
logger.Errorw("could not update node stats", err)
|
||||||
} else {
|
} else {
|
||||||
r.currentNode.Stats = updated
|
r.currentNode.Stats = updated
|
||||||
}
|
}
|
||||||
|
r.statsMu.Unlock()
|
||||||
|
|
||||||
// TODO: check stats against config.Limit values
|
// TODO: check stats against config.Limit values
|
||||||
if err := r.RegisterNode(); err != nil {
|
if err := r.RegisterNode(); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user