Repository navigation
Description
Describe the bug
AgentHandler.JobRequest (pkg/service/agentservice.go) assigns a job with Worker.AssignJob, then sets jobToWorker and registers the job's JobTerminate handler. The cleanup paths for a job, an ended UpdateJobStatus from the worker (agentHandlerWorker.HandleUpdateJob) and worker disconnect (deregisterWorker), can run before that registration. They then deregister nothing, and the handler registered afterwards is never released.
Separately, Worker.AssignJob (pkg/agent/worker.go) sends the JobAssignment before adding the job to runningJobs. An ended update handled in between finds no job (Worker.HandleUpdateJob ignores the error), and the job is then recorded as running even though it has ended.
The orderings that leak:
- The job ends after it is recorded as running but before the handler is registered. The ended update deregisters the job early, and the handler registered afterwards stays. A later
JobTerminategets "no worker for jobID" if thejobToWorkerentry was removed, or "no running job for given jobID" if it was set after the early deregistration, and in neither case deregisters it. - The job ends before
AssignJobrecords it as running. The update is ignored and the job stays inrunningJobs, with its handler, until the worker disconnects. - The worker disconnects before the handler is registered.
deregisterWorkerruns first, and the handler registered afterwards is never released. ThejobToWorkerentry can also keep the closed*agent.Worker.
Realistic triggers are an agent whose entrypoint fails immediately (every job it receives ends right after assignment), and a worker crashing, restarting or losing its connection just as a job is assigned.
Server
- Version: master (1e53951), also v1.13.7
- Environment: reproduced with the local message bus
Client
- SDK: any (reproduced with the simulated worker in
pkg/agent/testutils)
To Reproduce
- Register a simulated worker whose assignment handler either sends
UpdateJobStatus(JS_FAILED)or closes the worker as soon as it receives a job. - Request jobs.
- After the worker's messages have been handled and the worker has disconnected, send
JobTerminatefor each job once. A job whose handler was released gets no response; a leaked one is still answered.
On master, checking each job once, the share of jobs that kept a handler varied widely between runs and between two local test harnesses:
- Worker reports the job failed on assignment: from 3% to 50%.
- Worker disconnects on assignment: from 12% to 77%.
Leaked handlers answered "no worker for jobID" or "no running job for given jobID", and some jobs were still recorded as running. These setups end every job at the worst moment, so they show the orderings are reachable rather than production rates.
Expected behavior
Once a job has ended or its worker has disconnected, its JobTerminate handler and jobToWorker entry are released, regardless of whether that happens before or after the handler is registered.
Additional context
Found while working on #4893, which fixes a different leak of the same handler (jobs terminated by the server). With #4893, a later JobTerminate also releases a leaked job whose jobToWorker entry is still set, but for a job that ends right after assignment the agent usually never joins the room, so nothing sends one.
I'm working on a fix: record the job as running before sending the assignment, and after registering the handler, deregister the job if it is no longer running or its worker is no longer registered. I'll open a PR shortly.
Activity
agent: release JobTerminate handler when job ends before registration
agent: release JobTerminate handler when job ends before registration (…