fix(plugins): harden typing lease refreshes

This commit is contained in:
Vincent Koc 2026-03-13 14:19:16 -07:00
parent 36a323c8af
commit 6fc600b0f6
4 changed files with 57 additions and 2 deletions

View File

@ -35,4 +35,23 @@ describe("createDiscordTypingLease", () => {
leaseB.stop();
});
it("swallows background pulse failures", async () => {
vi.useFakeTimers();
const pulse = vi
.fn<(params: { channelId: string; accountId?: string; cfg?: unknown }) => Promise<void>>()
.mockResolvedValueOnce(undefined)
.mockRejectedValueOnce(new Error("boom"));
const lease = await createDiscordTypingLease({
channelId: "123",
intervalMs: 2_000,
pulse,
});
await expect(vi.advanceTimersByTimeAsync(2_000)).resolves.toBe(vi);
expect(pulse).toHaveBeenCalledTimes(2);
lease.stop();
});
});

View File

@ -1,3 +1,5 @@
import { logWarn } from "../../logger.js";
export type CreateDiscordTypingLeaseParams = {
channelId: string;
accountId?: string;
@ -38,7 +40,10 @@ export async function createDiscordTypingLease(params: CreateDiscordTypingLeaseP
await pulse();
timer = setInterval(() => {
void pulse();
// Background lease refreshes must never escape as unhandled rejections.
void pulse().catch((err) => {
logWarn(`plugins: discord typing pulse failed: ${String(err)}`);
});
}, intervalMs);
timer.unref?.();

View File

@ -35,4 +35,30 @@ describe("createTelegramTypingLease", () => {
leaseB.stop();
});
it("swallows background pulse failures", async () => {
vi.useFakeTimers();
const pulse = vi
.fn<
(params: {
to: string;
accountId?: string;
cfg?: unknown;
messageThreadId?: number;
}) => Promise<unknown>
>()
.mockResolvedValueOnce(undefined)
.mockRejectedValueOnce(new Error("boom"));
const lease = await createTelegramTypingLease({
to: "telegram:123",
intervalMs: 2_000,
pulse,
});
await expect(vi.advanceTimersByTimeAsync(2_000)).resolves.toBe(vi);
expect(pulse).toHaveBeenCalledTimes(2);
lease.stop();
});
});

View File

@ -1,4 +1,5 @@
import type { OpenClawConfig } from "../../config/config.js";
import { logWarn } from "../../logger.js";
export type CreateTelegramTypingLeaseParams = {
to: string;
@ -36,8 +37,12 @@ export async function createTelegramTypingLease(params: CreateTelegramTypingLeas
await refresh();
const timer = setInterval(() => {
void refresh();
// Background lease refreshes must never escape as unhandled rejections.
void refresh().catch((err) => {
logWarn(`plugins: telegram typing pulse failed: ${String(err)}`);
});
}, intervalMs);
timer.unref?.();
return {
refresh,