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;