{"record":{"id":"bed4c2ac82126b90","repo":"parcel-bundler/parcel","slug":"cannot-add-a-worker-call-if-workerfarm-is-ending","errorCode":null,"errorMessage":"Cannot add a worker call if workerfarm is ending.","messagePattern":"Cannot add a worker call if workerfarm is ending\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/workers/src/WorkerFarm.js","lineNumber":422,"sourceCode":"        result = errorResponseFromError(e);\n      }\n    }\n\n    if (awaitResponse) {\n      if (worker) {\n        worker.send(result);\n      } else {\n        if (result.contentType === 'error') {\n          throw new ThrowableDiagnostic({diagnostic: result.content});\n        }\n        return result.content;\n      }\n    }\n  }\n\n  addCall(method: string, args: Array<any>): Promise<any> {\n    if (this.ending) {\n      throw new Error('Cannot add a worker call if workerfarm is ending.');\n    }\n\n    return new Promise((resolve, reject) => {\n      this.callQueue.push({\n        method,\n        args: args,\n        retries: 0,\n        resolve,\n        reject,\n      });\n      this.processQueue();\n    });\n  }\n\n  async end(): Promise<void> {\n    this.ending = true;\n\n    await Promise.all(","sourceCodeStart":404,"sourceCodeEnd":440,"githubUrl":"https://github.com/parcel-bundler/parcel/blob/59484858a1a0bcbb71f74088956bb437a2db6505/packages/core/workers/src/WorkerFarm.js#L404-L440","documentation":"Thrown by WorkerFarm.addCall when this.ending is true — i.e. the farm has been told to shut down (end()/terminate()) and refuses to enqueue new work. This prevents new promises from being created against a farm that will never process them.","triggerScenarios":"Calling farm.run(...) or farm.addCall(...) after farm.end()/farm.terminate() has been invoked, or concurrently while the shutdown is in progress.","commonSituations":"A watch/build script that ends the farm then triggers a late plugin callback; concurrent code paths where shutdown races with queued work; reusing a WorkerFarm instance across builds instead of getting the shared one.","solutions":["Do not call the farm after end()/terminate(); track lifecycle and gate new calls.","Use WorkerFarm.getShared() so a single managed instance is reused across builds rather than manually ending it.","Await farm.end() fully before discarding the reference and ensure no timers/listeners queue calls afterward.","If late calls are legitimate, re-create the farm or get the shared instance again."],"exampleFix":"// before\nfarm.end();\nfarm.run('transform', file); // -> Cannot add a worker call\n\n// after\nawait farm.end();\nconst farm2 = WorkerFarm.getShared(); // fresh/shared instance\nfarm2.run('transform', file);","handlingStrategy":"validation","validationCode":"function assertFarmAlive(farm) {\n  if (farm.ending) throw new Error('WorkerFarm is ending; no new calls allowed.');\n}\nassertFarmAlive(farm);\nfarm.run('transform', file);","typeGuard":"function isFarmUsable(farm: { ending?: boolean }): boolean {\n  return !farm.ending;\n}","tryCatchPattern":"try { await farm.addCall(method, args); }\ncatch (e) {\n  if (/Cannot add a worker call if workerfarm is ending/.test(e.message)) {\n    // obtain a fresh/shared farm and retry, or abort gracefully\n  } else throw e;\n}","preventionTips":["Use WorkerFarm.getShared() so lifecycle is managed centrally across builds.","Await farm.end() before dereferencing it and silence timers/listeners that may queue calls.","Track an 'ended' flag in your orchestrator and short-circuit late callbacks."],"tags":["worker-farm","lifecycle","shutdown","race-condition"],"backgroundTag":null,"analyzedSha":"59484858a1a0bcbb71f74088956bb437a2db6505","analyzedAt":"2026-08-13T04:06:35.925Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}