Agent jobs terminated by the server are never released (leaks psrpc handlers with agents-js)
Describe the bug
When the server terminates an agent job, the job's JobTerminate psrpc handler and its jobToWorker entry in AgentHandler are never released unless the worker later sends an ended UpdateJobStatus. The handler (goroutines plus bus subscriptions) leaks for the life of the process.
How it happens:
- When an agent participant leaves its room,
Room.RemoveParticipantsendsTerminateJob(AGENT_LEFT_ROOM)(pkg/rtc/room.go).DeleteAgentDispatchalso terminates jobs. AgentHandler.JobTerminate(pkg/service/agentservice.go) callsWorker.TerminateJob, which marks the job ended and removes it from the worker'srunningJobs. It does not callderegisterJob.- The job is then only released by:
- an ended
UpdateJobStatusfrom the worker (agentHandlerWorker.HandleUpdateJob), or - worker disconnect (
deregisterWorker), which only deregisters jobs still inrunningJobs, and this job has already been removed.
- an ended
agents-js does not currently send updateJob (agents/src/worker.ts only sends register, availability, updateWorker and simulateJob), so with JS workers this leaks on every job ended by the agent leaving the room. The Python SDK reports job status when the job process closes, so it is only affected if the worker disconnects before sending that update.
This looks like a regression: in #2919, TerminateJob's status update reached AgentHandler.HandleWorkerJobStatus, which deregistered the job. #3018 left HandleWorkerJobStatus uncalled, and #3042 restored cleanup only for worker-sent status updates.
Server
- Version: v1.13.7 and master (1e539510)
- Environment: reproduced with the local and Redis message buses
Client
- SDK: agents-js
- Version: 1.9.0 (current main)
To Reproduce
- Register an agent worker that does not send
UpdateJobStatus(agents-js, or the simulated worker inpkg/agent/testutils). - Dispatch a job to it.
- Terminate the job through
JobTerminatewithAGENT_LEFT_ROOM(what happens when the agent participant leaves). - Disconnect the worker.
- The job's
JobTerminatehandler is still registered: a laterJobTerminatefor that job ID is answered with "no running job for given jobID" instead of getting no response.
Measured over a Redis bus, comparing runtime.NumGoroutine and the number of JobTerminate pub/sub channels before and after: 100 jobs terminated this way left +300 goroutines and +200 Redis pub/sub channel subscriptions (3 and 2 per job), still present after the worker disconnected.
Expected behavior
A job terminated by the server is fully released on the server (terminate handler deregistered, jobToWorker entry removed), whether or not the worker reports its status afterwards.
Additional context Fix and regression test: #4893.
Source: livekit/livekit