From 91f9010237863eb31fba02266fd11ea031ac78af Mon Sep 17 00:00:00 2001 From: Claudia Date: Sun, 1 Feb 2026 11:35:37 +0100 Subject: [PATCH] fix: also serialize matrix/index.js import in channel.ts The parallel account startup was crashing because the dynamic import of ./matrix/index.js returned undefined when called concurrently. Added importMatrixIndex() to import-mutex.ts and use it in channel.ts to ensure the module is only imported once during parallel startup. --- src/channel.ts | 4 +++- src/matrix/import-mutex.ts | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/channel.ts b/src/channel.ts index 9ffc53696..6b7ac5029 100644 --- a/src/channel.ts +++ b/src/channel.ts @@ -21,6 +21,7 @@ import { type ResolvedMatrixAccount, } from "./matrix/accounts.js"; import { resolveMatrixAuth } from "./matrix/client.js"; +import { importMatrixIndex } from "./matrix/import-mutex.js"; import { normalizeAllowListLower } from "./matrix/monitor/allowlist.js"; import { probeMatrix } from "./matrix/probe.js"; import { sendMessageMatrix } from "./matrix/send.js"; @@ -403,7 +404,8 @@ export const matrixPlugin: ChannelPlugin = { `[${account.accountId}] starting provider (${account.homeserver ?? "matrix"})`, ); // Lazy import: the monitor pulls the reply pipeline; avoid ESM init cycles. - const { monitorMatrixProvider } = await import("./matrix/index.js"); + // Use serialized import to prevent race conditions during parallel account startup. + const { monitorMatrixProvider } = await importMatrixIndex(); return monitorMatrixProvider({ runtime: ctx.runtime, abortSignal: ctx.abortSignal, diff --git a/src/matrix/import-mutex.ts b/src/matrix/import-mutex.ts index caf145b8a..820c9fa4a 100644 --- a/src/matrix/import-mutex.ts +++ b/src/matrix/import-mutex.ts @@ -68,3 +68,21 @@ export async function importCredentials(): Promise { + if (matrixIndexModule) return matrixIndexModule; + + const mod = await serializedImport( + "./matrix/index.js", + () => import("./index.js") + ); + matrixIndexModule = mod; + return mod; +}