fix(kilo-sessions): handle sync throws in inflight cache

Wrap callback execution in a promise microtask so synchronous errors flow
through the existing `.catch()` and the in-flight entry is reliably cleaned
up. Update tests to await the microtask before asserting call count.
This commit is contained in:
Igor Šćekić
2026-02-02 15:41:37 +01:00
parent cd62b61774
commit b04e8e877c
2 changed files with 6 additions and 1 deletions
@@ -37,7 +37,9 @@ export function withInFlightCache<T>(
inflight: undefined,
}
const task = cb()
// Guard against synchronous throws in `cb()` by forcing it into the promise chain.
// This ensures errors flow through the `.catch()` below and the cache entry is cleaned up.
const task = Promise.resolve().then(() => cb())
.then((value) => {
if (value === undefined) {
// `undefined` is treated as a non-cacheable sentinel.
@@ -39,6 +39,9 @@ describe("withInFlightCache", () => {
const b = withInFlightCache(key, 10_000, cb)
expect(a).toBe(b)
// cb() is invoked via a microtask in withInFlightCache().
// Allow it to run before asserting call count.
await Promise.resolve()
expect(calls.count).toBe(1)
job.resolve(123)