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:
+12
-9
@@ -29,7 +29,6 @@ import (
|
||||
"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"
|
||||
)
|
||||
@@ -333,12 +332,12 @@ func (w *Worker) GetJobState(jobID livekit.JobID) (*livekit.JobState, error) {
|
||||
if !ok {
|
||||
return nil, ErrJobNotFound
|
||||
}
|
||||
return utils.CloneProto(j.State), nil
|
||||
return proto.CloneOf(j.State), nil
|
||||
}
|
||||
|
||||
func (w *Worker) AssignJob(ctx context.Context, job *livekit.Job, hook AssignmentHook) (*livekit.JobState, error) {
|
||||
availCh := make(chan *livekit.AvailabilityResponse, 1)
|
||||
job = utils.CloneProto(job)
|
||||
job = proto.CloneOf(job)
|
||||
jobID := livekit.JobID(job.Id)
|
||||
|
||||
w.mu.Lock()
|
||||
@@ -420,16 +419,20 @@ func (w *Worker) AssignJob(ctx context.Context, job *livekit.Job, hook Assignmen
|
||||
if hook != nil {
|
||||
send = hook(send)
|
||||
}
|
||||
state := proto.CloneOf(job.State)
|
||||
|
||||
// must be in runningJobs before the assignment is sent; stored as a copy because send reads job without w.mu.
|
||||
w.mu.Lock()
|
||||
w.runningJobs[jobID] = proto.CloneOf(job)
|
||||
w.mu.Unlock()
|
||||
|
||||
if err := send(&livekit.JobAssignment{Job: job, Token: token}); err != nil {
|
||||
w.mu.Lock()
|
||||
delete(w.runningJobs, jobID)
|
||||
w.mu.Unlock()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
state := utils.CloneProto(job.State)
|
||||
|
||||
w.mu.Lock()
|
||||
w.runningJobs[jobID] = job
|
||||
w.mu.Unlock()
|
||||
|
||||
// TODO sweep jobs that are never started. We can't do this until all SDKs actually update the the JOB state
|
||||
|
||||
return state, nil
|
||||
|
||||
Reference in New Issue
Block a user