fix(matrix): ignore shutdown sync-state noise

This commit is contained in:
Gustavo Madeira Santana
2026-04-08 13:58:19 -04:00
parent 7d0f094dbf
commit 901bb767b5
2 changed files with 36 additions and 0 deletions

View File

@@ -114,6 +114,36 @@ describe("createMatrixMonitorSyncLifecycle", () => {
);
});
it("ignores non-terminal sync states emitted during intentional shutdown", async () => {
const client = createClientEmitter();
const setStatus = vi.fn();
let stopping = false;
const statusController = createMatrixMonitorStatusController({
accountId: "default",
statusSink: setStatus,
});
const lifecycle = createMatrixMonitorSyncLifecycle({
client: client as never,
statusController,
isStopping: () => stopping,
});
const waitPromise = lifecycle.waitForFatalStop();
stopping = true;
client.emit("sync.state", "ERROR", "RECONNECTING", new Error("shutdown noise"));
lifecycle.dispose();
statusController.markStopped();
await expect(waitPromise).resolves.toBeUndefined();
expect(setStatus).toHaveBeenLastCalledWith(
expect.objectContaining({
accountId: "default",
healthState: "stopped",
lastError: null,
}),
);
});
it("does not downgrade a fatal error to stopped during shutdown", async () => {
const client = createClientEmitter();
const setStatus = vi.fn();

View File

@@ -47,6 +47,12 @@ export function createMatrixMonitorSyncLifecycle(params: {
if (fatalError) {
return;
}
// Operator-initiated shutdown can still emit transient sync states before
// the final STOPPED. Ignore that churn so intentional stops do not look
// like runtime failures.
if (params.isStopping?.() && !isMatrixTerminalSyncState(state)) {
return;
}
params.statusController.noteSyncState(state, error);
};