Fix AgentHandler.DrainConnections deadlock on worker close. (#4710)
Snapshot workers and release h.mu before Close so HandleConnection can deregister without blocking drain.
This commit is contained in:
@@ -218,3 +218,41 @@ func TestConnectionClosedOnDispatchError(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDrainConnectionsDoesNotDeadlock(t *testing.T) {
|
||||||
|
for _, force := range []bool{false, true} {
|
||||||
|
t.Run(fmt.Sprintf("force=%v", force), func(t *testing.T) {
|
||||||
|
bus := psrpc.NewLocalMessageBus()
|
||||||
|
server := testutils.NewTestServer(bus)
|
||||||
|
t.Cleanup(server.Close)
|
||||||
|
|
||||||
|
worker := server.SimulateAgentWorker()
|
||||||
|
worker.Register("drain_agent", livekit.JobType_JT_ROOM)
|
||||||
|
responses := worker.RegisterWorkerResponses.Observe()
|
||||||
|
select {
|
||||||
|
case <-responses.Events():
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
require.Fail(t, "registration timeout")
|
||||||
|
}
|
||||||
|
responses.Stop()
|
||||||
|
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
server.DrainConnections(time.Millisecond, force)
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
require.Fail(t, "DrainConnections deadlocked while closing workers")
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-worker.Closed():
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
require.Fail(t, "worker should be closed after drain")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -486,6 +486,16 @@ func (h *AgentHandler) CheckEnabled(ctx context.Context, req *rpc.CheckEnabledRe
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *AgentHandler) DrainConnections(interval time.Duration, force bool) {
|
func (h *AgentHandler) DrainConnections(interval time.Duration, force bool) {
|
||||||
|
// Snapshot workers and release the lock before Close. Worker.Close closes the
|
||||||
|
// signal connection, which unblocks HandleConnection's read loop so it can call
|
||||||
|
// deregisterWorker — that needs h.mu. Holding the lock across Close deadlocks drain.
|
||||||
|
h.mu.Lock()
|
||||||
|
workers := make([]*agent.Worker, 0, len(h.workers))
|
||||||
|
for _, w := range h.workers {
|
||||||
|
workers = append(workers, w)
|
||||||
|
}
|
||||||
|
h.mu.Unlock()
|
||||||
|
|
||||||
if !force {
|
if !force {
|
||||||
// jitter drain start
|
// jitter drain start
|
||||||
time.Sleep(time.Duration(rand.Int63n(int64(interval))))
|
time.Sleep(time.Duration(rand.Int63n(int64(interval))))
|
||||||
@@ -493,19 +503,13 @@ func (h *AgentHandler) DrainConnections(interval time.Duration, force bool) {
|
|||||||
t := time.NewTicker(interval)
|
t := time.NewTicker(interval)
|
||||||
defer t.Stop()
|
defer t.Stop()
|
||||||
|
|
||||||
h.mu.Lock()
|
for _, w := range workers {
|
||||||
defer h.mu.Unlock()
|
|
||||||
|
|
||||||
for _, w := range h.workers {
|
|
||||||
w.Close()
|
w.Close()
|
||||||
<-t.C
|
<-t.C
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// drain as quickly as possible when forced
|
// drain as quickly as possible when forced
|
||||||
h.mu.Lock()
|
for _, w := range workers {
|
||||||
defer h.mu.Unlock()
|
|
||||||
|
|
||||||
for _, w := range h.workers {
|
|
||||||
w.Close()
|
w.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user