fix(mattermost): deactivate slash state before async cleanup to prevent race

Snapshot registered commands, then deactivate state immediately on abort.
Prevents race where new monitor activates fresh state that gets wiped
by the delayed .then() of the old monitor's cleanup promise.
This commit is contained in:
Echo 2026-02-15 03:28:32 -05:00 committed by Muhammed Mukhthar CM
parent 5bbe16f2ce
commit 434dc0ffd5
1 changed files with 7 additions and 4 deletions

View File

@ -1134,13 +1134,16 @@ export async function monitorMattermostProvider(opts: MonitorMattermostOpts = {}
// Clean up slash commands on shutdown
if (slashEnabled) {
const onAbortCleanup = () => {
// Snapshot registered commands before deactivating state
const commands = getSlashCommandState(account.accountId)?.registeredCommands ?? [];
// Deactivate state immediately to prevent race with re-activation
deactivateSlashCommands(account.accountId);
// Then clean up remote registrations asynchronously
void cleanupSlashCommands({
client,
commands: getSlashCommandState(account.accountId)?.registeredCommands ?? [],
commands,
log: (msg) => runtime.log?.(msg),
})
.then(() => deactivateSlashCommands(account.accountId))
.catch((err) => runtime.error?.(`mattermost: slash cleanup failed: ${String(err)}`));
}).catch((err) => runtime.error?.(`mattermost: slash cleanup failed: ${String(err)}`));
};
opts.abortSignal?.addEventListener("abort", onAbortCleanup, { once: true });
}