Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
117 changes: 117 additions & 0 deletions src/tests/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down