Backtestsvuerun-20261021-133314#15496

Issue #15496 Merged fix PR #15499 Run 21 Oct 2026 · run-20261021-133314 vuejs/core

looseEqual: comparing Map/Set values with circular references throws RangeError (regression in 3.5.42)

Better

Our patch was judged better than the merged fix.

In this judgment, Patch B = our agent, Patch A = the merged fix.

Both patches fix the root cause by tracking in-progress (a, b) pairs so cyclic Maps, Sets, arrays and plain objects terminate, and both pass every independent test. The differences are in semantics, API hygiene and test coverage.

Correctness: Patch B implements exactly what the issue proposes (a WeakMap/Map<object, Set<object>> of in-flight pairs, returning true on re-encounter and letting the outer comparison decide). This is coinductively sound: two structures that unroll identically compare equal. Patch A uses two bidirectional maps and returns false when a node is already paired with a different partner, which is stricter and can report structurally-identical-when-unrolled graphs as unequal depending on how nodes are shared (its own symmetric test asserts `self.next=self` vs a two-node cycle is unequal). That convention is defensible, but it is a design choice beyond what the issue asks for and is less obviously correct for a 'loose' equality used to match v-model option values. Patch B preserves the old `[]` vs `{}` false result via the `isArray(a) !== isArray(b)` guard and wraps object comparison in try/finally so the tracking set is always cleaned up.

Craft: Patch A adds an optional third `seen` parameter to the exported `looseEqual`, leaking an internal implementation detail into the public signature of a shared utility. Patch B keeps `looseEqual(a, b)` unchanged and routes recursion through a private `baseLooseEqual`, which is cleaner. Patch A does allocate lazily, a minor perf plus, but it is not decisive.

Tests: Patch B adds unit tests for cyclic Sets, Maps (including self-referencing maps), objects and arrays, with both equal and unequal cases and both argument orders, and additionally adds two runtime-dom vModel tests that reproduce the issue's actual `<select v-model>` scenario (the path that regressed). Patch A's tests are solid but unit-level only and spend a case locking in its stricter asymmetric semantics.

Scope is comparable; both also address the adjacent plain-object cycle case. Overall B is the better fix: matches the issue's suggested semantics, keeps the public API clean, and tests the regression at the integration level.

Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.

Solve

13m 45s

Wall-clock time in the container

Full test suite

No new failures

3,746 passed · 6 skipped · 3,752 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

222vs 138 merged

Changed lines, ours vs merged

Vue version

3.5.42 (also reproduced on current main, commit 5409708)

Link to minimal reproduction

https://play.vuejs.org/#eyJBcHAudnVlIjogIjxzY3JpcHQgc2V0dXA-XG5pbXBvcnQgeyByZWYgfSBmcm9tICd2dWUnXG5cbmNvbnN0IG1ha2VHcmFwaCA9ICgpID0-IHtcbiAgY29uc3QgbjEgPSB7IGlkOiAxIH1cbiAgY29uc3QgbjIgPSB7IGlkOiAyIH1cbiAgbjEucGVlciA9IG4yXG4gIG4yLnBlZXIgPSBuMSAgLy8gY2lyY3VsYXIgcmVmZXJlbmNlXG4gIHJldHVybiBuZXcgU2V0KFtuMSwgbjJdKVxufVxuXG5jb25zdCBvcHRBID0gbWFrZUdyYXBoKClcbmNvbnN0IG9wdEIgPSBtYWtlR3JhcGgoKVxuY29uc3Qgc2VsZWN0ZWQgPSByZWYob3B0Qilcbjwvc2NyaXB0PlxuXG48dGVtcGxhdGU-XG4gIDxzZWxlY3Qgdi1tb2RlbD1cInNlbGVjdGVkXCI-XG4gICAgPG9wdGlvbiA6dmFsdWU9XCJvcHRBXCI-QTwvb3B0aW9uPlxuICAgIDxvcHRpb24gOnZhbHVlPVwib3B0QlwiPkI8L29wdGlvbj5cbiAgPC9zZWxlY3Q-XG48L3RlbXBsYXRlPlxuIiwgImltcG9ydC1tYXAuanNvbiI6ICJ7XCJpbXBvcnRzXCI6IHtcInZ1ZVwiOiBcImh0dHBzOi8vdW5wa2cuY29tL0B2dWUvcnVudGltZS1kb21AMy41LjQyL2Rpc3QvcnVudGltZS1kb20uZXNtLWJyb3dzZXIuanNcIn19In0=

Steps to reproduce

Open the reproduction above: a <select v-model> whose option :values are Sets containing mutually-referencing objects. The component throws during mount, before any interaction.

<script setup>
import { ref } from 'vue'

const makeGraph = () => {
  const n1 = { id: 1 }
  const n2 = { id: 2 }
  n1.peer = n2
  n2.peer = n1  // circular reference
  return new Set([n1, n2])
}

const optA = makeGraph()
const optB = makeGraph()
const selected = ref(optB)
</script>

<template>
  <select v-model="selected">
    <option :value="optA">A</option>
    <option :value="optB">B</option>
  </select>
</template>

What is expected?

Mounting the select should not throw. looseEqual should safely terminate on cyclic structures (e.g. by tracking already-visited pairs) and return true for two structurally identical cyclic Sets/Maps.

What is actually happening?

Mounting throws RangeError: Maximum call stack size exceeded:

RangeError: Maximum call stack size exceeded
    at looseCompareCollections (packages/shared/src/looseEqual.ts:16)
    at looseEqual (packages/shared/src/looseEqual.ts:64)
    at looseCompareCollections (packages/shared/src/looseEqual.ts:22)
    at looseEqual (packages/shared/src/looseEqual.ts:64)
    ...
[Vue warn]: Unhandled error during execution of directive hook

System Info

Reproduced locally with vuejs/core@main (5409708) via vitest: both the unit-level looseEqual calls and the component-level render path (same shape as the test added in #15328) throw RangeError.

Any additional comments?

This is a regression introduced by #15328 ("fix(shared): correctly compare Map and Set values", released in v3.5.42), which added looseCompareCollections in packages/shared/src/looseEqual.ts:

function looseCompareCollections(
  a: Map<any, any> | Set<any>,
  b: Map<any, any> | Set<any>,
) {
  if (a.size !== b.size) return false
  const candidates = Array.from(b)
  const matched = new Uint8Array(candidates.length)
  for (const item of a) {
    let index = -1
    for (let i = 0; i < candidates.length; i++) {
      if (!matched[i] && looseEqual(item, candidates[i])) {
        index = i
        break
      }
    }
    if (index < 0) return false
    matched[index] = 1
  }
  return true
}

looseEqual(item, candidates[i]) recurses into each [key, value] pair (Map) or element (Set), so a Map/Set whose entries point back to the collection itself or to each other causes unbounded recursion. Before 3.5.42, Maps/Sets never recursed into their entries (they fell through to String() comparison), so this exact code path — a <select v-model> with Map/Set values, which is precisely the scenario #15320/#15328 added support for — did not throw on 3.5.41; it silently compared wrong, which #15328 fixed.

Note: #5318 covers the same root cause for cyclic plain objects (pre-existing, still open). The Map/Set case is new in 3.5.42: cyclic Maps/Sets did not throw before.

Suggested fix: make looseEqual cycle-safe by tracking visited (a, b) pairs (e.g. with a WeakMap<object, Set<object>> and returning true on re-encounter), which also fixes #5318.

Additional local repro cases that all throw RangeError on current main:

  • looseEqual(new Set([a1, a2]), new Set([b1, b2])) where a1.peer = a2; a2.peer = a1 and b1.peer = b2; b2.peer = b1
  • looseEqual(mapA, mapB) where both maps hold mutually-referencing values
  • looseEqual(s1, s2) where s1.add(s1) and s2.add(s2)

The two patches

Our patch+192 −30
packages/runtime-dom/__tests__/directives/vModel.spec.tsindex a94cb8e0d..bbb1b1d46 100644@@ -1902,4 +1902,69 @@ describe('vModel', () => {      expect(root.querySelector('select').selectedIndex).toBe(1)   })++  it('selects the matching option when values are sets with circular references', () => {+    const makeGraph = (id1: number, id2: number) => {+      const n1: any = { id: id1 }+      const n2: any = { id: id2 }+      n1.peer = n2+      n2.peer = n1+      return new Set([n1, n2])+    }+    const optA = makeGraph(1, 2)+    const optB = makeGraph(3, 4)+    const value = ref(makeGraph(3, 4))+    const component = defineComponent(+      () => () =>+        withVModel(+          h(+            'select',+            {+              'onUpdate:modelValue': (val: Set<any>) => (value.value = val),+            },+            [+              h('option', { value: optA }, 'A'),+              h('option', { value: optB }, 'B'),+            ],+          ),+          value.value,+        ),+    )++    render(h(component), root)++    expect(root.querySelector('select').selectedIndex).toBe(1)+  })++  it('selects the matching option when values are self-referencing maps', () => {+    const makeMap = (id: number) => {+      const map = new Map<string, any>([['id', id]])+      map.set('self', map)+      return map+    }+    const mapA = makeMap(1)+    const mapB = makeMap(2)+    const value = ref(makeMap(2))+    const component = defineComponent(+      () => () =>+        withVModel(+          h(+            'select',+            {+              'onUpdate:modelValue': (val: Map<string, any>) =>+                (value.value = val),+            },+            [+              h('option', { value: mapA }, 'A'),+              h('option', { value: mapB }, 'B'),+            ],+          ),+          value.value,+        ),+    )++    render(h(component), root)++    expect(root.querySelector('select').selectedIndex).toBe(1)+  }) })packages/shared/__tests__/looseEqual.spec.tsindex 777f3cfad..307ea57aa 100644@@ -288,4 +288,62 @@ describe('utils/looseEqual', () => {     expect(looseEqual(new Set<any>([1, '1']), new Set<any>([1, 2]))).toBe(false)     expect(looseEqual(new Set(), new Map())).toBe(false)   })++  test('compares sets with circular references', () => {+    const makeGraph = (id1: number, id2: number) => {+      const n1: any = { id: id1 }+      const n2: any = { id: id2 }+      n1.peer = n2+      n2.peer = n1+      return new Set([n1, n2])+    }+    expect(looseEqual(makeGraph(1, 2), makeGraph(1, 2))).toBe(true)+    expect(looseEqual(makeGraph(1, 2), makeGraph(2, 1))).toBe(true)+    expect(looseEqual(makeGraph(1, 2), makeGraph(1, 3))).toBe(false)++    const s1 = new Set<any>([1])+    s1.add(s1)+    const s2 = new Set<any>([1])+    s2.add(s2)+    const s3 = new Set<any>([2])+    s3.add(s3)+    expect(looseEqual(s1, s2)).toBe(true)+    expect(looseEqual(s1, s3)).toBe(false)+  })++  test('compares maps with circular references', () => {+    const makeMap = (id1: number, id2: number) => {+      const n1: any = { id: id1 }+      const n2: any = { id: id2 }+      n1.peer = n2+      n2.peer = n1+      const map = new Map<any, any>([+        ['a', n1],+        ['b', n2],+      ])+      map.set('self', map)+      return map+    }+    expect(looseEqual(makeMap(1, 2), makeMap(1, 2))).toBe(true)+    expect(looseEqual(makeMap(1, 2), makeMap(2, 1))).toBe(false)+    expect(looseEqual(makeMap(1, 2), makeMap(1, 3))).toBe(false)+  })++  // #5318+  test('compares objects and arrays with circular references', () => {+    const makeObject = (id: number) => {+      const obj: any = { id, list: [] }+      obj.self = obj+      obj.list.push(obj, obj.list)+      return obj+    }+    expect(looseEqual(makeObject(1), makeObject(1))).toBe(true)+    expect(looseEqual(makeObject(1), makeObject(2))).toBe(false)++    const a: any = { id: 1 }+    a.self = a+    const b: any = { id: 1, self: { id: 1, self: { id: 2 } } }+    expect(looseEqual(a, b)).toBe(false)+    expect(looseEqual(b, a)).toBe(false)+  }) })packages/shared/src/looseEqual.tsindex d037b0a57..0a75078f0 100644@@ -1,10 +1,14 @@ import { isArray, isDate, isMap, isObject, isSet, isSymbol } from './general' -function looseCompareArrays(a: any[], b: any[]) {+// pairs of objects currently being compared further up the call stack,+// tracked so that comparing cyclic structures terminates+type ComparingPairs = Map<object, Set<object>>++function looseCompareArrays(a: any[], b: any[], comparing: ComparingPairs) {   if (a.length !== b.length) return false   let equal = true   for (let i = 0; equal && i < a.length; i++) {-    equal = looseEqual(a[i], b[i])+    equal = baseLooseEqual(a[i], b[i], comparing)   }   return equal }@@ -12,6 +16,7 @@ function looseCompareArrays(a: any[], b: any[]) { function looseCompareCollections(   a: Map<any, any> | Set<any>,   b: Map<any, any> | Set<any>,+  comparing: ComparingPairs, ) {   if (a.size !== b.size) return false   const candidates = Array.from(b)@@ -19,7 +24,7 @@ function looseCompareCollections(   for (const item of a) {     let index = -1     for (let i = 0; i < candidates.length; i++) {-      if (!matched[i] && looseEqual(item, candidates[i])) {+      if (!matched[i] && baseLooseEqual(item, candidates[i], comparing)) {         index = i         break       }@@ -30,7 +35,48 @@ function looseCompareCollections(   return true } -export function looseEqual(a: any, b: any): boolean {+function looseCompareObjects(a: any, b: any, comparing: ComparingPairs) {+  let aValidType = isArray(a)+  let bValidType = isArray(b)+  if (aValidType || bValidType) {+    return aValidType && bValidType+      ? looseCompareArrays(a, b, comparing)+      : false+  }+  aValidType = isMap(a)+  bValidType = isMap(b)+  if (aValidType || bValidType) {+    return aValidType && bValidType+      ? looseCompareCollections(a, b, comparing)+      : false+  }+  aValidType = isSet(a)+  bValidType = isSet(b)+  if (aValidType || bValidType) {+    return aValidType && bValidType+      ? looseCompareCollections(a, b, comparing)+      : false+  }+  const aKeysCount = Object.keys(a).length+  const bKeysCount = Object.keys(b).length+  if (aKeysCount !== bKeysCount) {+    return false+  }+  for (const key in a) {+    const aHasKey = a.hasOwnProperty(key)+    const bHasKey = b.hasOwnProperty(key)+    if (+      (aHasKey && !bHasKey) ||+      (!aHasKey && bHasKey) ||+      !baseLooseEqual(a[key], b[key], comparing)+    ) {+      return false+    }+  }+  return String(a) === String(b)+}++function baseLooseEqual(a: any, b: any, comparing: ComparingPairs): boolean {   if (a === b) return true   let aValidType = isDate(a)   let bValidType = isDate(b)@@ -44,8 +90,8 @@ export function looseEqual(a: any, b: any): boolean {   }   aValidType = isArray(a)   bValidType = isArray(b)-  if (aValidType || bValidType) {-    return aValidType && bValidType ? looseCompareArrays(a, b) : false+  if (aValidType !== bValidType) {+    return false   }   aValidType = isObject(a)   bValidType = isObject(b)@@ -53,36 +99,29 @@ export function looseEqual(a: any, b: any): boolean {     if (!aValidType || !bValidType) {       return false     }-    aValidType = isMap(a)-    bValidType = isMap(b)-    if (aValidType || bValidType) {-      return aValidType && bValidType ? looseCompareCollections(a, b) : false-    }-    aValidType = isSet(a)-    bValidType = isSet(b)-    if (aValidType || bValidType) {-      return aValidType && bValidType ? looseCompareCollections(a, b) : false+    // re-encountering a pair that is already being compared means the+    // structures are cyclic: assume equal here and let the outer comparison+    // of that pair decide the result+    let comparingWithA = comparing.get(a)+    if (!comparingWithA) {+      comparing.set(a, (comparingWithA = new Set()))+    } else if (comparingWithA.has(b)) {+      return true     }-    const aKeysCount = Object.keys(a).length-    const bKeysCount = Object.keys(b).length-    if (aKeysCount !== bKeysCount) {-      return false-    }-    for (const key in a) {-      const aHasKey = a.hasOwnProperty(key)-      const bHasKey = b.hasOwnProperty(key)-      if (-        (aHasKey && !bHasKey) ||-        (!aHasKey && bHasKey) ||-        !looseEqual(a[key], b[key])-      ) {-        return false-      }+    comparingWithA.add(b)+    try {+      return looseCompareObjects(a, b, comparing)+    } finally {+      comparingWithA.delete(b)     }   }   return String(a) === String(b) } +export function looseEqual(a: any, b: any): boolean {+  return baseLooseEqual(a, b, new Map())+}+ export function looseIndexOf(arr: any[], val: any): number {   return arr.findIndex(item => looseEqual(item, val)) } 
Merged fix · PR #15499+107 −31
packages/shared/__tests__/looseEqual.spec.tsindex 777f3cfad..bc1bc3acd 100644@@ -288,4 +288,45 @@ describe('utils/looseEqual', () => {     expect(looseEqual(new Set<any>([1, '1']), new Set<any>([1, 2]))).toBe(false)     expect(looseEqual(new Set(), new Map())).toBe(false)   })++  test('compares circular references correctly', () => {+    const makeGraph = () => {+      const n1: any = { id: 1 }+      const n2: any = { id: 2 }+      n1.peer = n2+      n2.peer = n1+      return [n1, n2]+    }++    expect(looseEqual(new Set(makeGraph()), new Set(makeGraph()))).toBe(true)+    expect(+      looseEqual(new Map([['a', makeGraph()]]), new Map([['a', makeGraph()]])),+    ).toBe(true)+    expect(looseEqual(makeGraph(), makeGraph())).toBe(true)+    expect(looseEqual({ n: makeGraph() }, { n: makeGraph() })).toBe(true)++    const selfSet = new Set<any>()+    selfSet.add(selfSet)+    const otherSelfSet = new Set<any>()+    otherSelfSet.add(otherSelfSet)+    expect(looseEqual(selfSet, otherSelfSet)).toBe(true)+    expect(looseEqual(selfSet, new Set([new Set()]))).toBe(false)++    const shared = { id: 1 }+    expect(+      looseEqual({ a: shared, b: shared }, { a: { id: 1 }, b: { id: 1 } }),+    ).toBe(true)+  })++  test('compares circular references symmetrically', () => {+    const self: any = {}+    self.next = self+    const first: any = {}+    const second: any = {}+    first.next = second+    second.next = first++    expect(looseEqual(self, first)).toBe(false)+    expect(looseEqual(first, self)).toBe(false)+  }) })packages/shared/src/looseEqual.tsindex d037b0a57..7e3e7f44e 100644@@ -1,10 +1,12 @@ import { isArray, isDate, isMap, isObject, isSet, isSymbol } from './general' -function looseCompareArrays(a: any[], b: any[]) {+type ComparisonState = [Map<any, any>, Map<any, any>]++function looseCompareArrays(a: any[], b: any[], seen: ComparisonState) {   if (a.length !== b.length) return false   let equal = true   for (let i = 0; equal && i < a.length; i++) {-    equal = looseEqual(a[i], b[i])+    equal = looseEqual(a[i], b[i], seen)   }   return equal }@@ -12,6 +14,7 @@ function looseCompareArrays(a: any[], b: any[]) { function looseCompareCollections(   a: Map<any, any> | Set<any>,   b: Map<any, any> | Set<any>,+  seen: ComparisonState, ) {   if (a.size !== b.size) return false   const candidates = Array.from(b)@@ -19,7 +22,7 @@ function looseCompareCollections(   for (const item of a) {     let index = -1     for (let i = 0; i < candidates.length; i++) {-      if (!matched[i] && looseEqual(item, candidates[i])) {+      if (!matched[i] && looseEqual(item, candidates[i], seen)) {         index = i         break       }@@ -30,7 +33,62 @@ function looseCompareCollections(   return true } -export function looseEqual(a: any, b: any): boolean {+function looseCompareObjects(a: any, b: any, seen: ComparisonState) {+  let aValidType = isMap(a)+  let bValidType = isMap(b)+  if (aValidType || bValidType) {+    return aValidType && bValidType+      ? looseCompareCollections(a, b, seen)+      : false+  }+  aValidType = isSet(a)+  bValidType = isSet(b)+  if (aValidType || bValidType) {+    return aValidType && bValidType+      ? looseCompareCollections(a, b, seen)+      : false+  }+  const aKeysCount = Object.keys(a).length+  const bKeysCount = Object.keys(b).length+  if (aKeysCount !== bKeysCount) {+    return false+  }+  for (const key in a) {+    const aHasKey = a.hasOwnProperty(key)+    const bHasKey = b.hasOwnProperty(key)+    if (+      (aHasKey && !bHasKey) ||+      (!aHasKey && bHasKey) ||+      !looseEqual(a[key], b[key], seen)+    ) {+      return false+    }+  }+  return String(a) === String(b)+}++function looseCompareNested(+  a: any,+  b: any,+  seen: ComparisonState | undefined,+  compare: (a: any, b: any, seen: ComparisonState) => boolean,+) {+  if (!seen) {+    seen = [new Map(), new Map()]+  }+  const [seenA, seenB] = seen+  if (seenA.has(a) || seenB.has(b)) {+    return seenA.get(a) === b && seenB.get(b) === a+  }+  seenA.set(a, b)+  seenB.set(b, a)+  const equal = compare(a, b, seen)+  seenA.delete(a)+  seenB.delete(b)+  return equal+}++export function looseEqual(a: any, b: any, seen?: ComparisonState): boolean {   if (a === b) return true   let aValidType = isDate(a)   let bValidType = isDate(b)@@ -45,7 +103,9 @@ export function looseEqual(a: any, b: any): boolean {   aValidType = isArray(a)   bValidType = isArray(b)   if (aValidType || bValidType) {-    return aValidType && bValidType ? looseCompareArrays(a, b) : false+    return aValidType && bValidType+      ? looseCompareNested(a, b, seen, looseCompareArrays)+      : false   }   aValidType = isObject(a)   bValidType = isObject(b)@@ -53,32 +113,7 @@ export function looseEqual(a: any, b: any): boolean {     if (!aValidType || !bValidType) {       return false     }-    aValidType = isMap(a)-    bValidType = isMap(b)-    if (aValidType || bValidType) {-      return aValidType && bValidType ? looseCompareCollections(a, b) : false-    }-    aValidType = isSet(a)-    bValidType = isSet(b)-    if (aValidType || bValidType) {-      return aValidType && bValidType ? looseCompareCollections(a, b) : false-    }-    const aKeysCount = Object.keys(a).length-    const bKeysCount = Object.keys(b).length-    if (aKeysCount !== bKeysCount) {-      return false-    }-    for (const key in a) {-      const aHasKey = a.hasOwnProperty(key)-      const bHasKey = b.hasOwnProperty(key)-      if (-        (aHasKey && !bHasKey) ||-        (!aHasKey && bHasKey) ||-        !looseEqual(a[key], b[key])-      ) {-        return false-      }-    }+    return looseCompareNested(a, b, seen, looseCompareObjects)   }   return String(a) === String(b) }