{"record":{"id":"91e2f7a337fe04bf","repo":"denoland/deno","slug":"failed-to-enqueue-value","errorCode":null,"errorMessage":"Failed to enqueue value","messagePattern":"Failed to enqueue value","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/kv/01_db.ts","lineNumber":319,"sourceCode":"    }\n    if (opts?.backoffSchedule !== undefined) {\n      validateBackoffSchedule(opts?.backoffSchedule);\n    }\n\n    const versionstamp = await doAtomicWriteInPlace(\n      this.#rid,\n      [],\n      [],\n      [\n        [\n          core.serialize(message, { forStorage: true }),\n          opts?.delay ?? 0,\n          opts?.keysIfUndelivered ?? [],\n          opts?.backoffSchedule ?? null,\n        ],\n      ],\n    );\n    if (versionstamp === null) throw new TypeError(\"Failed to enqueue value\");\n    return { ok: true, versionstamp };\n  }\n\n  async listenQueue(\n    handler: (message: unknown) => Promise<void> | void,\n  ): Promise<void> {\n    if (this.#isClosed) {\n      throw new Error(\"Queue already closed\");\n    }\n    const finishMessageOps = new SafeMap<number, Promise<void>>();\n    while (true) {\n      // Wait for the next message.\n      const next: { 0: Uint8Array; 1: number } =\n        await op_kv_dequeue_next_message(\n          this.#rid,\n        );\n      if (next === null) {\n        break;","sourceCodeStart":301,"sourceCodeEnd":337,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/kv/01_db.ts#L301-L337","documentation":"Kv.enqueue() commits the serialized message (plus optional delay, keysIfUndelivered, backoffSchedule) through the same op_kv_atomic_write. If the op returns a null versionstamp the message was not persisted and enqueue() throws TypeError('Failed to enqueue value'). Invalid delay/backoffSchedule/keys throw earlier with their own dedicated messages, so reaching this error means the atomic commit itself failed.","triggerScenarios":"`await kv.enqueue(msg)` racing kv.close() or handle invalidation so the atomic write returns null; rare commit failures from the underlying storage. Serialization problems with the message throw separately from core.serialize before the write.","commonSituations":"Enqueueing work during shutdown after close; a producer sharing a handle whose lifecycle another task owns; retry wrappers that re-enqueue through a stale handle.","solutions":["Ensure the producer's handle is open for the full duration of the enqueue (await it before close).","Catch, re-open Deno.openKv(path), and re-enqueue — make consumers idempotent so a rare duplicate delivery is safe.","Keep close() centralized in the owner task and await outstanding enqueue promises before closing."],"exampleFix":"// before\nawait kv.close();\nawait kv.enqueue(job); // Failed to enqueue value\n\n// after\nawait kv.enqueue(job);\nawait kv.close();","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"async function enqueueSafe(kv: Deno.Kv, msg: unknown, path?: string) {\n  try {\n    return await kv.enqueue(msg);\n  } catch (e) {\n    if (e instanceof TypeError && e.message === \"Failed to enqueue value\") {\n      const reopened = await Deno.openKv(path ?? \":memory:\");\n      return reopened.enqueue(msg); // consumers must be idempotent\n    }\n    throw e;\n  }\n}","preventionTips":["Await enqueue promises before closing the handle","Make queue consumers idempotent so a retry-after-reopen duplicate is harmless","Validate delay/backoffSchedule separately — those have their own earlier errors"],"tags":["deno-kv","queue","enqueue","atomic-write"],"backgroundTag":"queue-enqueue-failed","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}