fix: guard the abort listener against a double invocation

Copilot review round 1. The helper's contract is exactly-once teardown
and the test asserted it must not lean on Hono's own re-entry guard, but
neither the implementation nor the test actually enforced that: the
listener had no local guard and the test aborted only once.

Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
cliffhall 2026-08-17 14:41:24 -04:00
commit e58c80440b
2 changed files with 11 additions and 1 deletions

View file

@ -138,9 +138,12 @@ describe("primeAndHoldSseStream (#1999)", () => {
s.finishWrite();
s.abort();
// Hono guards re-entry itself, but the helper must not depend on that —
// so drive the listener a second time directly.
s.abort();
await withinTick(held);
// Hono guards re-entry itself, but the helper must not depend on that.
expect(cleanups).toBe(1);
expect(s.closes).toBe(1);
});
it("does not reject when cleanup's own close loses the race", async () => {

View file

@ -147,8 +147,15 @@ export async function primeAndHoldSseStream(
stream: PrimableSseStream,
cleanup: () => void,
): Promise<void> {
let done = false;
const aborted = new Promise<void>((resolve) => {
stream.onAbort(() => {
// Hono's `abort()` guards its own re-entry, but the guard belongs here
// too: this helper's contract is exactly-once teardown, and running a
// caller's cleanup twice would double-delete a subscriber that has
// since been re-added by a reconnect.
if (done) return;
done = true;
cleanup();
closeAbortedStream(stream);
resolve();