Skip to content

Agent job's JobTerminate handler leaks when the job ends or its worker disconnects right after assignment #4901

Closed

Description

@mnjalale

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:

  1. 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 JobTerminate gets "no worker for jobID" if the jobToWorker entry was removed, or "no running job for given jobID" if it was set after the early deregistration, and in neither case deregisters it.
  2. The job ends before AssignJob records it as running. The update is ignored and the job stays in runningJobs, with its handler, until the worker disconnects.
  3. The worker disconnects before the handler is registered. deregisterWorker runs first, and the handler registered afterwards is never released. The jobToWorker entry 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

  1. Register a simulated worker whose assignment handler either sends UpdateJobStatus(JS_FAILED) or closes the worker as soon as it receives a job.
  2. Request jobs.
  3. After the worker's messages have been handled and the worker has disconnected, send JobTerminate for 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

  1. added 2 commits that reference this issue on Sep 25, 2026
    e03e0f4
    1ba2cad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions