fix(synology-chat): deregister stale webhook route before re-registering on restart (#24971)
When the Synology Chat plugin restarts (auto-restart or health monitor), startAccount is called again without calling the previous stop(). The HTTP route is still registered, so registerPluginHttpRoute returns a no-op unregister function and logs "already registered". This triggers another restart, creating an infinite loop. Store the unregister function at module level keyed by account+path. Before registering, check for and call any stale unregister from the previous start cycle, ensuring a clean slate for route registration. Fixes #24894 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -20,6 +20,8 @@ import { createWebhookHandler } from "./webhook-handler.js";
|
||||
const CHANNEL_ID = "synology-chat";
|
||||
const SynologyChatConfigSchema = buildChannelConfigSchema(z.object({}).passthrough());
|
||||
|
||||
const activeRouteUnregisters = new Map<string, () => void>();
|
||||
|
||||
export function createSynologyChatPlugin() {
|
||||
return {
|
||||
id: CHANNEL_ID,
|
||||
@@ -270,7 +272,16 @@ export function createSynologyChatPlugin() {
|
||||
log,
|
||||
});
|
||||
|
||||
// Register HTTP route via the SDK
|
||||
// Deregister any stale route from a previous start (e.g. on auto-restart)
|
||||
// to avoid "already registered" collisions that trigger infinite loops.
|
||||
const routeKey = `${accountId}:${account.webhookPath}`;
|
||||
const prevUnregister = activeRouteUnregisters.get(routeKey);
|
||||
if (prevUnregister) {
|
||||
log?.info?.(`Deregistering stale route before re-registering: ${account.webhookPath}`);
|
||||
prevUnregister();
|
||||
activeRouteUnregisters.delete(routeKey);
|
||||
}
|
||||
|
||||
const unregister = registerPluginHttpRoute({
|
||||
path: account.webhookPath,
|
||||
pluginId: CHANNEL_ID,
|
||||
@@ -278,6 +289,7 @@ export function createSynologyChatPlugin() {
|
||||
log: (msg: string) => log?.info?.(msg),
|
||||
handler,
|
||||
});
|
||||
activeRouteUnregisters.set(routeKey, unregister);
|
||||
|
||||
log?.info?.(`Registered HTTP route: ${account.webhookPath} for Synology Chat`);
|
||||
|
||||
@@ -285,6 +297,7 @@ export function createSynologyChatPlugin() {
|
||||
stop: () => {
|
||||
log?.info?.(`Stopping Synology Chat channel (account: ${accountId})`);
|
||||
if (typeof unregister === "function") unregister();
|
||||
activeRouteUnregisters.delete(routeKey);
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user