diff --git a/src/stream.ts b/src/stream.ts index 6fd71dd..e78a232 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -400,7 +400,11 @@ async function* streamChatEvents(args: { // ignore } try { - void resp.body?.cancel(); + // cancel() on an already-errored stream rejects with the stored + // error (e.g. undici TypeError("terminated") after the server drops + // the socket). try/catch cannot observe that rejection, so swallow + // it explicitly or it escapes as an uncaughtException and kills pi. + void resp.body?.cancel()?.catch(() => {}); } catch { // ignore }