#4894·livekit

Agent jobs terminated by the server are never released (leaks psrpc handlers with agents-js)

Author: mnjalaleCreated Sep 22, 2026Updated Sep 22, 2026

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:

  1. When an agent participant leaves its room, Room.RemoveParticipant sends TerminateJob(AGENT_LEFT_ROOM) (pkg/rtc/room.go). DeleteAgentDispatch also terminates jobs.
  2. AgentHandler.JobTerminate (pkg/service/agentservice.go) calls Worker.TerminateJob, which marks the job ended and removes it from the worker's runningJobs. It does not call deregisterJob.
  3. The job is then only released by:
    • an ended UpdateJobStatus from the worker (agentHandlerWorker.HandleUpdateJob), or
    • worker disconnect (deregisterWorker), which only deregisters jobs still in runningJobs, and this job has already been removed.

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

  1. Register an agent worker that does not send UpdateJobStatus (agents-js, or the simulated worker in pkg/agent/testutils).
  2. Dispatch a job to it.
  3. Terminate the job through JobTerminate with AGENT_LEFT_ROOM (what happens when the agent participant leaves).
  4. Disconnect the worker.
  5. The job's JobTerminate handler is still registered: a later JobTerminate for 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.