denoland/deno · error · TypeError

Failed to set value

Error message

Failed to set value

What it means

Kv.set() commits through op_kv_atomic_write and expects a fresh versionstamp string back. When the native op returns a null versionstamp the write was not applied, and set() throws TypeError('Failed to set value'). set() attaches no checks, so unlike a failed atomic().commit() (which cleanly returns {ok:false} on a failed check), a null versionstamp here is an abnormal commit failure, not a normal conflict path.

Solutions

  1. Guarantee kv.close() is not called while writes are in flight — await pending writes before closing in the same task.
  2. Catch the error, re-open via Deno.openKv(path), and retry the set once (design values to tolerate a rare duplicate write if needed).
  3. Note that newer Deno versions throw 'Kv is closed' for closed handles — upgrading gives a clearer diagnosis.
  4. Open one Kv handle per subsystem instead of sharing a single handle across tasks with independent shutdown.

Example fix

// before
await kv.close();
await kv.set(["users", 1], user); // Failed to set value

// after
await kv.set(["users", 1], user);
await kv.close();
Defensive patterns

Strategy: retry

Try / catch

async function setSafe(kv: Deno.Kv, key: Deno.KvKey, value: unknown, path?: string) {
  try {
    return await kv.set(key, value);
  } catch (e) {
    if (e instanceof TypeError && e.message === "Failed to set value") {
      const reopened = await Deno.openKv(path ?? ":memory:");
      return reopened.set(key, value); // retry once on the fresh handle
    }
    throw e;
  }
}

Prevention

When it happens

Trigger: `await kv.set(key, value)` where op_kv_atomic_write returns null — e.g. the underlying database resource was invalidated or closed concurrently by another task while the write was in flight. Not caused by unserializable values (V8 serialization errors throw earlier from core.serialize) or by key validation (which throws separately).

Common situations: kv.close() racing pending writes during shutdown; background queue handlers writing through a shared handle that another worker or signal handler already closed; long-lived scripts that close the DB on SIGINT while a save is still awaiting.

Related errors


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

Appendix: source

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

        return {
          key: keys[i],
          value: null,
          versionstamp: null,
        };
      }
      return deserializeValue(entries[0]);
    });
  }

  async set(key: Deno.KvKey, value: unknown, options?: { expireIn?: number }) {
    validateExpireIn(options?.expireIn);
    const versionstamp = await doAtomicWriteInPlace(
      this.#rid,
      [],
      [[key, "set", serializeValue(value), options?.expireIn]],
      [],
    );
    if (versionstamp === null) throw new TypeError("Failed to set value");
    return { ok: true, versionstamp };
  }

  async delete(key: Deno.KvKey) {
    const result = await doAtomicWriteInPlace(
      this.#rid,
      [],
      [[key, "delete", null, undefined]],
      [],
    );
    if (!result) throw new TypeError("Failed to set value");
  }

  list(
    selector: Deno.KvListSelector,
    options: {
      limit?: number;
      batchSize?: number;

View on GitHub (pinned to 9ad36f7a2c)