denoland/deno · error · RangeError

ERR_INVALID_ARG_VALUE

ERR_INVALID_ARG_VALUE

Error message

The property 'prevValue.user' is invalid

What it means

process.cpuUsage(previousValue) requires previousValue.user (and .system) to be numbers in [0, Number.MAX_SAFE_INTEGER]. When user is a number but negative, NaN, Infinity, or above the safe range, validation passes the type check (validateNumber) and then throws ERR_INVALID_ARG_VALUE. Non-number values throw a different ERR_INVALID_ARG_TYPE error instead.

Source

Thrown at ext/node/polyfills/process.ts:290

  user: number;
  system: number;
}

// Ensure that a previously passed in value is valid. Currently, the native
// implementation always returns numbers <= Number.MAX_SAFE_INTEGER.
function previousCpuUsageValueIsValid(num) {
  return typeof num === "number" && num >= 0 && num <= NumberMAX_SAFE_INTEGER;
}

export function cpuUsage(previousValue?: CpuUsage): CpuUsage {
  const cpuValues = Deno.cpuUsage(previousValue);

  if (previousValue) {
    if (!previousCpuUsageValueIsValid(previousValue.user)) {
      validateObject(previousValue, "prevValue");

      validateNumber(previousValue.user, "prevValue.user");
      throw new ERR_INVALID_ARG_VALUE_RANGE(
        "prevValue.user",
        previousValue.user,
      );
    }

    if (!previousCpuUsageValueIsValid(previousValue.system)) {
      validateNumber(previousValue.system, "prevValue.system");
      throw new ERR_INVALID_ARG_VALUE_RANGE(
        "prevValue.system",
        previousValue.system,
      );
    }

    return {
      user: cpuValues.user - previousValue.user,
      system: cpuValues.system - previousValue.system,
    };
  }

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Only pass the verbatim object returned by a previous process.cpuUsage() call
  2. Keep raw snapshots, not computed diffs, as baselines: const start = process.cpuUsage(); ... process.cpuUsage(start)
  3. If baselines come from storage, validate/reject them instead of calling the API: both fields must be finite numbers >= 0
  4. threadCpuUsage has the same constraint — apply the same rule there

Example fix

// before
const delta = process.cpuUsage(start);
process.cpuUsage(delta); // diff-of-diff, may be invalid

// after
const start = process.cpuUsage();
// ... work ...
const delta = process.cpuUsage(start); // always pass a raw snapshot
Defensive patterns

Strategy: validation

Validate before calling

function isCpuUsageSnapshot(v) {
  return typeof v === "object" && v !== null &&
    Number.isFinite(v.user) && v.user >= 0 && v.user <= Number.MAX_SAFE_INTEGER &&
    Number.isFinite(v.system) && v.system >= 0 && v.system <= Number.MAX_SAFE_INTEGER;
}
const delta = isCpuUsageSnapshot(prev) ? process.cpuUsage(prev) : process.cpuUsage();

Type guard

const isCpuUsageSnapshot = (v) =>
  v != null && typeof v === "object" &&
  [v.user, v.system].every((n) => Number.isFinite(n) && n >= 0 && n <= Number.MAX_SAFE_INTEGER);

Try / catch

try {
  delta = process.cpuUsage(prev);
} catch (err) {
  if (err.code === "ERR_INVALID_ARG_VALUE" && /prevValue/.test(err.message)) {
    prev = process.cpuUsage(); // take a fresh snapshot and restart the window
    delta = process.cpuUsage(prev);
  } else throw err;
}

Prevention

When it happens

Trigger: process.cpuUsage({ user: -1, system: 0 }); { user: NaN } from a bad computation; { user: Infinity } from dividing by zero; feeding a hand-constructed or mutated baseline object instead of a genuine snapshot.

Common situations: Re-feeding a previously computed diff (which can be 0/negative after rounding) as the new baseline; serializing snapshots through float32/JSON losing precision past MAX_SAFE_INTEGER; copying only some fields off a real snapshot.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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