diff --git a/extensions/mattermost/src/mattermost/monitor.ts b/extensions/mattermost/src/mattermost/monitor.ts index 5ebe3c4bfa5..11d50c7094c 100644 --- a/extensions/mattermost/src/mattermost/monitor.ts +++ b/extensions/mattermost/src/mattermost/monitor.ts @@ -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 }); }