agent: release JobTerminate handler when job ends before registration (#4908)
* agent: release JobTerminate handler when job ends before registration JobRequest registered the job's JobTerminate handler after AssignJob returned, so an ended UpdateJobStatus or worker disconnect handled in between deregistered nothing and the handler leaked. AssignJob also sent the assignment before recording the job as running, so an immediate ended update was dropped. Record the job before sending the assignment, and after registering the handler recheck under h.mu that the job is still running and its worker still registered, deregistering otherwise. Fixes #4901 * replace utils.CloneProto with proto.CloneOf * agent: wait on worker readiness and poll for released handlers in test
This commit is contained in:
@@ -18,13 +18,14 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/protocol/agent"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/rpc"
|
||||
"github.com/livekit/protocol/utils"
|
||||
"github.com/livekit/protocol/utils/guid"
|
||||
"github.com/livekit/psrpc"
|
||||
)
|
||||
@@ -120,7 +121,7 @@ func redactCreateAgentDispatchRequest(req *livekit.CreateAgentDispatchRequest) *
|
||||
return req
|
||||
}
|
||||
|
||||
clone := utils.CloneProto(req)
|
||||
clone := proto.CloneOf(req)
|
||||
|
||||
// replace with size of metadata to provide visibility on request size
|
||||
if clone.Metadata != "" {
|
||||
|
||||
@@ -423,14 +423,19 @@ func (h *AgentHandler) JobRequest(ctx context.Context, job *livekit.Job) (*rpc.J
|
||||
switch state.GetStatus() {
|
||||
case livekit.JobStatus_JS_RUNNING:
|
||||
logger.Infow("assigned job to worker", "apiKey", selected.APIKey())
|
||||
h.mu.Lock()
|
||||
h.jobToWorker[livekit.JobID(job.Id)] = selected
|
||||
h.mu.Unlock()
|
||||
|
||||
err = h.agentServer.RegisterJobTerminateTopic(job.Id)
|
||||
if err != nil {
|
||||
jobID := livekit.JobID(job.Id)
|
||||
if err := h.agentServer.RegisterJobTerminateTopic(job.Id); err != nil {
|
||||
logger.Errorw("failed to register JobTerminate handler", err)
|
||||
}
|
||||
|
||||
// checked under h.mu after registering so that either this or the job/worker cleanup, whichever runs second, releases the handler.
|
||||
h.mu.Lock()
|
||||
if _, err := selected.GetJobState(jobID); err == nil && h.workers[selected.ID] == selected {
|
||||
h.jobToWorker[jobID] = selected
|
||||
} else {
|
||||
h.deregisterJob(jobID)
|
||||
}
|
||||
h.mu.Unlock()
|
||||
fallthrough
|
||||
case livekit.JobStatus_JS_SUCCESS:
|
||||
return &rpc.JobRequestResponse{
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/thoas/go-funk"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/utils"
|
||||
@@ -203,7 +204,7 @@ func (s *LocalStore) StoreAgentDispatch(ctx context.Context, dispatch *livekit.A
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
clone := utils.CloneProto(dispatch)
|
||||
clone := proto.CloneOf(dispatch)
|
||||
if clone.State != nil {
|
||||
clone.State.Jobs = nil
|
||||
}
|
||||
@@ -242,13 +243,13 @@ func (s *LocalStore) ListAgentDispatches(ctx context.Context, roomName livekit.R
|
||||
|
||||
var js []*livekit.Job
|
||||
for _, j := range agentJobs {
|
||||
js = append(js, utils.CloneProto(j))
|
||||
js = append(js, proto.CloneOf(j))
|
||||
}
|
||||
var ds []*livekit.AgentDispatch
|
||||
|
||||
m := make(map[string]*livekit.AgentDispatch)
|
||||
for _, d := range agentDispatches {
|
||||
clone := utils.CloneProto(d)
|
||||
clone := proto.CloneOf(d)
|
||||
m[d.Id] = clone
|
||||
ds = append(ds, clone)
|
||||
}
|
||||
@@ -256,7 +257,7 @@ func (s *LocalStore) ListAgentDispatches(ctx context.Context, roomName livekit.R
|
||||
for _, j := range js {
|
||||
d := m[j.DispatchId]
|
||||
if d != nil {
|
||||
d.State.Jobs = append(d.State.Jobs, utils.CloneProto(j))
|
||||
d.State.Jobs = append(d.State.Jobs, proto.CloneOf(j))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,7 +268,7 @@ func (s *LocalStore) StoreAgentJob(ctx context.Context, job *livekit.Job) error
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
clone := utils.CloneProto(job)
|
||||
clone := proto.CloneOf(job)
|
||||
clone.Room = nil
|
||||
if clone.Participant != nil {
|
||||
clone.Participant = &livekit.ParticipantInfo{
|
||||
|
||||
@@ -548,7 +548,7 @@ func (s *RedisStore) storeIngress(_ context.Context, info *livekit.IngressInfo)
|
||||
}
|
||||
|
||||
// ignore state
|
||||
infoCopy := utils.CloneProto(info)
|
||||
infoCopy := proto.CloneOf(info)
|
||||
infoCopy.State = nil
|
||||
|
||||
data, err := proto.Marshal(infoCopy)
|
||||
@@ -851,7 +851,7 @@ func (s *RedisStore) DeleteIngress(_ context.Context, info *livekit.IngressInfo)
|
||||
}
|
||||
|
||||
func (s *RedisStore) StoreAgentDispatch(_ context.Context, dispatch *livekit.AgentDispatch) error {
|
||||
di := utils.CloneProto(dispatch)
|
||||
di := proto.CloneOf(dispatch)
|
||||
|
||||
// Do not store jobs with the dispatch
|
||||
if di.State != nil {
|
||||
@@ -914,7 +914,7 @@ func (s *RedisStore) StoreAgentJob(_ context.Context, job *livekit.Job) error {
|
||||
|
||||
key := AgentJobPrefix + string(job.Room.Name)
|
||||
|
||||
jb := utils.CloneProto(job)
|
||||
jb := proto.CloneOf(job)
|
||||
|
||||
// Do not store room with the job
|
||||
jb.Room = nil
|
||||
|
||||
@@ -347,7 +347,7 @@ func TestAgentStore(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, len(rd))
|
||||
|
||||
expected := utils.CloneProto(ad)
|
||||
expected := proto.CloneOf(ad)
|
||||
expected.State.Jobs = nil
|
||||
require.True(t, proto.Equal(expected, rd[0]))
|
||||
|
||||
@@ -358,7 +358,7 @@ func TestAgentStore(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, len(rd))
|
||||
|
||||
expected = utils.CloneProto(ad)
|
||||
expected = proto.CloneOf(ad)
|
||||
expected.State.Jobs[0].Room = nil
|
||||
expected.State.Jobs[0].Participant = &livekit.ParticipantInfo{
|
||||
Identity: "identity",
|
||||
@@ -372,7 +372,7 @@ func TestAgentStore(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, len(rd))
|
||||
|
||||
expected = utils.CloneProto(ad)
|
||||
expected = proto.CloneOf(ad)
|
||||
expected.State.Jobs = nil
|
||||
require.True(t, proto.Equal(expected, rd[0]))
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@ import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/utils"
|
||||
@@ -211,7 +213,7 @@ func (r *StandardRoomAllocator) applyNamedRoomConfiguration(req *livekit.CreateR
|
||||
return req, psrpc.NewErrorf(psrpc.InvalidArgument, "unknown room configuration in create room request")
|
||||
}
|
||||
|
||||
clone := utils.CloneProto(req)
|
||||
clone := proto.CloneOf(req)
|
||||
|
||||
if clone.EmptyTimeout == 0 {
|
||||
clone.EmptyTimeout = conf.EmptyTimeout
|
||||
@@ -223,12 +225,12 @@ func (r *StandardRoomAllocator) applyNamedRoomConfiguration(req *livekit.CreateR
|
||||
clone.MaxParticipants = conf.MaxParticipants
|
||||
}
|
||||
if clone.Egress == nil {
|
||||
clone.Egress = utils.CloneProto(conf.Egress)
|
||||
clone.Egress = proto.CloneOf(conf.Egress)
|
||||
}
|
||||
if clone.Agents == nil {
|
||||
clone.Agents = make([]*livekit.RoomAgentDispatch, 0, len(conf.Agents))
|
||||
for _, agent := range conf.Agents {
|
||||
clone.Agents = append(clone.Agents, utils.CloneProto(agent))
|
||||
clone.Agents = append(clone.Agents, proto.CloneOf(agent))
|
||||
}
|
||||
}
|
||||
if clone.MinPlayoutDelay == 0 {
|
||||
|
||||
Reference in New Issue
Block a user