From 3066b79ee266ca7e2144444c9d8881830ff3b726 Mon Sep 17 00:00:00 2001 From: duke-101 <202591183+duke-101@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:44:40 +0900 Subject: [PATCH] Avoid caching native V8 stack overflow exceptions A native stack overflow can disappear once its caller has unwound, while its query/input dependencies remain unchanged. Keeping that exception as a clean memoized value prevents a later shallow call from recovering. Leave only the observed V8 RangeError dirty before notifying parents, so parents that caught the failure also reevaluate their fallback results. Preserve normal result caching, ordinary exception identity and caching, and subscription cleanup. Guard classification of arbitrary thrown values. Add regression tests for native overflow retry, transitive parent fallback, ordinary throw values with throwing getters, and subscription cleanup. The new tests fail on the original implementation. After the change, both ESM and CJS suites pass 50 tests with one existing pending case. The packed npm artifact also fixes the Apollo reproduction across 3.13.4 and 4.3.8, Node 22.23.5 and 26.11.1, and both NODE_ENV modes. Refs apollographql/apollo-client#13534. --- src/entry.ts | 17 ++++++ src/tests/exceptions.ts | 117 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) diff --git a/src/entry.ts b/src/entry.ts index 7a767d1..e5713a5 100644 --- a/src/entry.ts +++ b/src/entry.ts @@ -232,6 +232,23 @@ function mightBeDirty(entry: AnyEntry) { } function setClean(entry: AnyEntry) { + // A native stack overflow depends on the current call depth, not the cache + // key. Keep the entry dirty before reporting clean to its parents, so a + // parent that caught the error also retries instead of caching its fallback. + // Existing parents already know this entry might be dirty when it recomputes. + if (entry.value.length === 2) { + try { + const error = entry.value[1]; + if (error instanceof RangeError && + error.message === "Maximum call stack size exceeded") { + entry.dirty = true; + return; + } + } catch { + // Inspecting an arbitrary thrown value must not replace its exception. + } + } + entry.dirty = false; if (mightBeDirty(entry)) { diff --git a/src/tests/exceptions.ts b/src/tests/exceptions.ts index 31f7cd7..38a232d 100644 --- a/src/tests/exceptions.ts +++ b/src/tests/exceptions.ts @@ -2,6 +2,123 @@ import * as assert from "assert"; import { wrap } from "../index.js"; describe("exceptions", function () { + it("should retry after a native stack overflow and then cache the result", function () { + function overflow(): never { + return overflow(); + } + let shouldOverflow = true; + let calls = 0; + const result = {}; + const wrapper = wrap(() => { + ++calls; + return shouldOverflow ? overflow() : result; + }); + + assert.throws(wrapper, (error: unknown) => + error instanceof RangeError && + error.message === "Maximum call stack size exceeded" + ); + shouldOverflow = false; + assert.strictEqual(wrapper(), result); + assert.strictEqual(wrapper(), result); + assert.strictEqual(calls, 2); + }); + + it("should retry a parent's fallback after a dirty child overflows", function () { + let state = "initial"; + const child = wrap(() => { + if (state === "overflow") { + throw new RangeError("Maximum call stack size exceeded"); + } + return state; + }); + const middle = wrap(() => child()); + const parent = wrap(() => { + try { + return middle(); + } catch { + return "fallback"; + } + }); + + assert.strictEqual(parent(), "initial"); + state = "overflow"; + child.dirty(); + assert.strictEqual(parent(), "fallback"); + state = "recovered"; + assert.strictEqual(parent(), "recovered"); + assert.strictEqual(parent(), "recovered"); + }); + + it("should preserve ordinary thrown values even when inspecting them throws", function () { + const messageGetterError = new RangeError(); + Object.defineProperty(messageGetterError, "message", { + get() { throw new Error("message getter"); }, + }); + const prototypeGetterError = new Proxy({}, { + getPrototypeOf() { throw new Error("prototype getter"); }, + }); + const errors = [ + new Error("Maximum call stack size exceeded"), + new RangeError("ordinary range error"), + messageGetterError, + prototypeGetterError, + undefined, + null, + 0, + ]; + errors.forEach(error => { + let calls = 0; + const wrapper = wrap(() => { + ++calls; + throw error; + }); + function check() { + try { + wrapper(); + assert.fail("expected the original thrown value"); + } catch (actual) { + assert.strictEqual(actual, error); + } + } + check(); + check(); + assert.strictEqual(calls, 1); + wrapper.dirty(); + check(); + assert.strictEqual(calls, 2); + }); + }); + + it("should clean up subscriptions when retrying and forgetting overflowed entries", function () { + let shouldOverflow = true; + let activeSubscriptions = 0; + let calls = 0; + const wrapper = wrap(() => { + ++calls; + if (shouldOverflow) { + throw new RangeError("Maximum call stack size exceeded"); + } + return "recovered"; + }, { + subscribe() { + ++activeSubscriptions; + return () => --activeSubscriptions; + }, + }); + + assert.throws(wrapper, RangeError); + assert.throws(wrapper, RangeError); + assert.strictEqual(calls, 2); + assert.strictEqual(activeSubscriptions, 1); + shouldOverflow = false; + assert.strictEqual(wrapper(), "recovered"); + assert.strictEqual(wrapper(), "recovered"); + assert.strictEqual(calls, 3); + wrapper.forget(); + assert.strictEqual(activeSubscriptions, 0); + }); + it("should be cached", function () { const error = new Error("expected"); let threw = false;