diff --git a/tests/persona-matrix-mouth.test.mjs b/tests/persona-matrix-mouth.test.mjs index f08426e..1e33f64 100644 --- a/tests/persona-matrix-mouth.test.mjs +++ b/tests/persona-matrix-mouth.test.mjs @@ -1285,3 +1285,21 @@ test("chat_read round-trips and refuses a malformed tuple", () => { assert.throws(() => docs.parseRead(["eureka", "#r", "$e", -1]), /at_unix_ms/); assert.throws(() => docs.parseRead(["eureka", "#r", "$e"])); }); + +test("a read report that throws does not wedge the room: the next report still posts and nothing is left unhandled", async () => { + const hs = await fakeHomeserver({ sync: receiptScript }); + let boom = false; + const fetch = (url, init) => boom && String(url).includes("/presence/") ? Promise.reject(new Error("net")) : hs.fetch(url, init); + const mouth = await withMail(hs, { fetch, log: line => { if (boom && line.startsWith("presence not set")) throw new Error("logger down"); } }); // the presence failure and a throwing logger make run() reject + const unhandled = [], note = error => unhandled.push(String(error)); + process.on("unhandledRejection", note); + try { + boom = true; + await assert.rejects(reportRead(mouth, A, "$a1"), /logger down/); + boom = false; + assert.equal(await reportRead(mouth, A, "$a2"), null, "the room's next report is judged and posted"); + await new Promise(resolve => setTimeout(resolve, 50)); + } finally { process.off("unhandledRejection", note); } + assert.deepEqual(unhandled, [], "no rejection escapes the tail's cleanup"); + assert.equal(hs.receipts().length, 1); +}); diff --git a/tools/persona-matrix-mouth.mjs b/tools/persona-matrix-mouth.mjs index ad7f114..658c728 100644 --- a/tools/persona-matrix-mouth.mjs +++ b/tools/persona-matrix-mouth.mjs @@ -409,9 +409,9 @@ export function createMouth({ readThrough.set(known.roomId, { seq: known.seq, eventId }); return null; }; - const tail = (readTails.get(known.roomId) ?? Promise.resolve()).then(run); + const tail = (readTails.get(known.roomId) ?? Promise.resolve()).then(run, run); // a report that threw must not wedge the room's later ones readTails.set(known.roomId, tail); - tail.then(() => { if (readTails.get(known.roomId) === tail) readTails.delete(known.roomId); }); + tail.then(() => { if (readTails.get(known.roomId) === tail) readTails.delete(known.roomId); }, () => { if (readTails.get(known.roomId) === tail) readTails.delete(known.roomId); }); return tail; }