Deregister agent job when the server terminates it (#4893)
* Deregister agent job when the server terminates it JobTerminate ended the job on the worker but left its JobTerminate handler and jobToWorker entry registered. Those were only cleaned up when the worker later sent an ended job status, or on worker disconnect for jobs still running. Workers that do not report job status after a termination (agents-js never sends UpdateJobStatus) leaked a psrpc handler for every job terminated by the server, e.g. each time an agent participant left its room. * Wait for worker registration in JobTerminate test The worker's registration response is sent before its job request topic is registered, so wait for the WorkerRegistered event before requesting jobs. Also cover a worker reporting an ended status after the server terminated the job, and clarify the JobTerminate comment.
This commit is contained in:
@@ -479,6 +479,15 @@ func (h *AgentHandler) JobTerminate(ctx context.Context, req *rpc.JobTerminateRe
|
||||
}
|
||||
|
||||
state, err := w.TerminateJob(livekit.JobID(req.JobId), req.Reason)
|
||||
|
||||
// the job is no longer tracked as running, and the worker may never send an
|
||||
// ended status update for it, so deregister it here. deregistering from
|
||||
// inside this handler is safe because psrpc force closes the handler
|
||||
// without waiting for in-flight requests.
|
||||
h.mu.Lock()
|
||||
h.deregisterJob(livekit.JobID(req.JobId))
|
||||
h.mu.Unlock()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user