denoland/deno · error · Error

Cannot specify two IPv4 addresses

Error message

Cannot specify two IPv4 addresses

What it means

ChannelWrap allows binding at most one IPv4 and one IPv6 local address. When the primary address (addr0) is already IPv4 and the secondary address (addr1) is also IPv4, the local-address pair is ambiguous for c-ares, so this error is thrown.

Source

Thrown at ext/node/polyfills/internal_binding/cares_wrap.ts:932

    //
    // We validate the addresses here and record them so the call no longer
    // throws and matches Node's observable behavior. The underlying resolver op
    // (`op_dns_resolve`) does not yet expose a per-query bind address, so the
    // stored value is not applied to outgoing queries; see
    // https://github.com/denoland/deno/issues/36518.
    let type0: 4 | 6;
    if (isIPv4(addr0)) {
      type0 = 4;
    } else if (isIPv6(addr0)) {
      type0 = 6;
    } else {
      throw new Error(`Invalid IP address: ${addr0}`);
    }

    if (addr1 !== undefined) {
      if (isIPv4(addr1)) {
        if (type0 === 4) {
          throw new Error("Cannot specify two IPv4 addresses");
        }
      } else if (isIPv6(addr1)) {
        if (type0 === 6) {
          throw new Error("Cannot specify two IPv6 addresses");
        }
      } else {
        throw new Error(`Invalid IP address: ${addr1}`);
      }
    }

    this.#localAddress = {
      ipv4: type0 === 4 ? addr0 : addr1,
      ipv6: type0 === 6 ? addr0 : addr1,
    };
  }

  cancel() {
    for (const req of new SafeSetIterator(this.#pendingQueries)) {

View on GitHub (pinned to a961cdec3b)

Solutions

  1. Supply at most one IPv4 and one IPv6 local address (one of each family).
  2. Ensure the IPv6 entry is a real IPv6 literal, not another IPv4 address.
  3. Drop the duplicate-family address from the localAddress configuration.

Example fix

// before
new ChannelWrap('0.0.0.0', '10.0.0.2'); // two IPv4
// after
new ChannelWrap('0.0.0.0', '::'); // one IPv4 + one IPv6
Defensive patterns

Strategy: validation

Validate before calling

function validLocalPair(addr0, addr1) {
  if (addr1 === undefined) return true;
  return !(isIPv4(addr0) && isIPv4(addr1));
}
if (!validLocalPair(a, b)) throw new Error('localAddresses must contain at most one IPv4 and one IPv6');

Type guard

function isSingleFamilyPair(a, b) {
  return b === undefined || (isIPv4(a) !== isIPv4(b));
}

Try / catch

try {
  channel.lookup(req, hostname);
} catch (err) {
  if (err.message === 'Cannot specify two IPv4 addresses') {
    // retry with only the first local address
  }
}

Prevention

When it happens

Trigger: Calling a ChannelWrap lookup with two local addresses where both addr0 and addr1 are valid IPv4 literals, e.g. localAddress '10.0.0.1' plus a second address that is also IPv4.

Common situations: Configuration that supplies a list of local addresses where the intent was one IPv4 + one IPv6 but both values happen to be IPv4 (e.g. dual NICs on the same family).

Related errors


AI-assisted analysis of denoland/deno@a961cdec3b (2026-09-03). Data as JSON: /api/errors/00d9abf460cd4d58. Report an issue: GitHub.