denoland/deno · error · TypeError

Failed to enqueue value

Error message

Failed to enqueue value

What it means

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.

Solutions

  1. Ensure the producer's handle is open for the full duration of the enqueue (await it before close).
  2. Catch, re-open Deno.openKv(path), and re-enqueue — make consumers idempotent so a rare duplicate delivery is safe.
  3. Keep close() centralized in the owner task and await outstanding enqueue promises before closing.

Example fix

// before
await kv.close();
await kv.enqueue(job); // Failed to enqueue value

// after
await kv.enqueue(job);
await kv.close();
Defensive patterns

Strategy: retry

Try / catch

async function enqueueSafe(kv: Deno.Kv, msg: unknown, path?: string) {
  try {
    return await kv.enqueue(msg);
  } catch (e) {
    if (e instanceof TypeError && e.message === "Failed to enqueue value") {
      const reopened = await Deno.openKv(path ?? ":memory:");
      return reopened.enqueue(msg); // consumers must be idempotent
    }
    throw e;
  }
}

Prevention

When it happens

Trigger: `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.

Common situations: 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.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/91e2f7a337fe04bf. Report an issue: GitHub.

Appendix: source

Thrown at ext/kv/01_db.ts:319

    }
    if (opts?.backoffSchedule !== undefined) {
      validateBackoffSchedule(opts?.backoffSchedule);
    }

    const versionstamp = await doAtomicWriteInPlace(
      this.#rid,
      [],
      [],
      [
        [
          core.serialize(message, { forStorage: true }),
          opts?.delay ?? 0,
          opts?.keysIfUndelivered ?? [],
          opts?.backoffSchedule ?? null,
        ],
      ],
    );
    if (versionstamp === null) throw new TypeError("Failed to enqueue value");
    return { ok: true, versionstamp };
  }

  async listenQueue(
    handler: (message: unknown) => Promise<void> | void,
  ): Promise<void> {
    if (this.#isClosed) {
      throw new Error("Queue already closed");
    }
    const finishMessageOps = new SafeMap<number, Promise<void>>();
    while (true) {
      // Wait for the next message.
      const next: { 0: Uint8Array; 1: number } =
        await op_kv_dequeue_next_message(
          this.#rid,
        );
      if (next === null) {
        break;

View on GitHub (pinned to 9ad36f7a2c)