{"id":"eb6de3559a02edd9","repo":"jestjs/jest","slug":"queue-implementation-returned-processed-task","errorCode":null,"errorMessage":"Queue implementation returned processed task","messagePattern":"Queue implementation returned processed task","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-worker/src/Farm.ts","lineNumber":124,"sourceCode":"\n    promise.UNSTABLE_onCustomMessage = addCustomMessageListener;\n\n    return promise;\n  }\n\n  private _process(workerId: number): Farm {\n    if (this._isLocked(workerId)) {\n      return this;\n    }\n\n    const task = this._taskQueue.dequeue(workerId);\n\n    if (!task) {\n      return this;\n    }\n\n    if (task.request[1]) {\n      throw new Error('Queue implementation returned processed task');\n    }\n\n    // Reference the task object outside so it won't be retained by onEnd,\n    // and other properties of the task object, such as task.request can be\n    // garbage collected.\n    let taskOnEnd: OnEnd | null = task.onEnd;\n    const onEnd: OnEnd = (error, result) => {\n      if (taskOnEnd) {\n        taskOnEnd(error, result);\n      }\n      taskOnEnd = null;\n\n      this._unlock(workerId);\n      this._process(workerId);\n    };\n\n    task.request[1] = true;\n","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-worker/src/Farm.ts#L106-L142","documentation":"Thrown by `Farm._process` (Farm.ts:123-125) when a task dequeued from the custom `_taskQueue` already has `task.request[1] === true`. That flag is the 'already dispatched' marker, set at Farm.ts:141 right before handing the task to the worker. A dequeue returning an already-processed task means the TaskQueue implementation is violating its contract (a dequeue must return only pending tasks).","triggerScenarios":"Providing a custom `taskQueue` option to `jest-worker`'s `Worker` whose `dequeue(workerId)` returns a task that was previously dequeued and dispatched (i.e. the queue retains and reissues processed tasks). The stock `FifoQueue` never does this; only a third-party/custom queue can.","commonSituations":"Writing a priority/re-entrant queue and forgetting to remove a task from internal storage on `dequeue`; a queue that copies/mirrors tasks across worker buckets and returns duplicates; a queue shared across Farms where dequeue semantics differ from what Farm expects (one-task-per-dequeue, FIFO per worker).","solutions":["In your custom `TaskQueue.dequeue(workerId)`, ensure each returned task is removed and never returned again.","Match the `TaskQueue` interface exactly: `enqueue(task, workerId?)` and `dequeue(workerId)` that pops the next pending task for that worker.","Test the queue standalone: enqueue N tasks, dequeue must yield each exactly once.","If you don't need custom scheduling, drop the `taskQueue` option and use the default `FifoQueue`."],"exampleFix":"// before: custom queue returns the same task twice\nclass BuggyPriorityQueue {\n  dequeue() { return this.tasks[0]; } // never removes\n}\n// after\nclass PriorityQ {\n  dequeue(workerId) { return this.tasks.shift() ?? null; }\n}","handlingStrategy":"type-guard","validationCode":"// Contract-test a custom TaskQueue: each enqueued task dequeues exactly once\nfunction assertQueueContract(Q) {\n  const q = new Q();\n  const t1 = { request: [0, false] };\n  q.enqueue(t1);\n  if (q.dequeue() !== t1) throw new Error('dequeue must return enqueued task');\n  if (q.dequeue() != null) throw new Error('dequeue must not return processed task');\n}","typeGuard":"const isTaskQueue = (q: any): boolean =>\n  q != null && typeof q.enqueue === 'function' && typeof q.dequeue === 'function';","tryCatchPattern":null,"preventionTips":["Implement dequeue to atomically remove and return the next pending task for the worker.","Contract-test your queue: enqueue N, dequeue must yield each exactly once and null after.","If custom scheduling isn't needed, omit `taskQueue` and use the default FifoQueue."],"tags":["jest-worker","task-queue","api-misuse","internal-contract"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}