From f03e2f3f7fbe9d1d79b7ef5993eadb2e2f747003 Mon Sep 17 00:00:00 2001 From: gnuxie Date: Wed, 15 Jan 2025 21:42:31 +0000 Subject: [PATCH] Stop hanging if appservice crashes at startup. Fixes https://github.com/the-draupnir-project/Draupnir/issues/503. --- src/appservice/AppService.ts | 26 ++++++++++++++++---------- src/appservice/cli.ts | 11 +---------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/appservice/AppService.ts b/src/appservice/AppService.ts index ec53bc63..ea9f6c89 100644 --- a/src/appservice/AppService.ts +++ b/src/appservice/AppService.ts @@ -225,16 +225,22 @@ export class MjolnirAppService { ): Promise { Logger.configure(config.logging ?? { console: "debug" }); const dataStore = new PgDataStore(config.db.connectionString); - await dataStore.init(); - const service = await MjolnirAppService.makeMjolnirAppService( - config, - dataStore, - DefaultEventDecoder, - registrationFilePath - ); - // The call to `start` MUST happen last. As it needs the datastore, and the mjolnir manager to be initialized before it can process events from the homeserver. - await service.start(port); - return service; + try { + await dataStore.init(); + const service = await MjolnirAppService.makeMjolnirAppService( + config, + dataStore, + DefaultEventDecoder, + registrationFilePath + ); + // The call to `start` MUST happen last. As it needs the datastore, and the mjolnir manager to be initialized before it can process events from the homeserver. + await service.start(port); + return service; + } catch (e: unknown) { + log.error("Failed to start the appservice", e); + await dataStore.close(); + throw e; + } } public onUserQuery(_queriedUser: MatrixUser) { diff --git a/src/appservice/cli.ts b/src/appservice/cli.ts index f054732d..e982f2de 100644 --- a/src/appservice/cli.ts +++ b/src/appservice/cli.ts @@ -11,7 +11,6 @@ import { Cli } from "matrix-appservice-bridge"; import { MjolnirAppService } from "./AppService"; import { IConfig } from "./config/config"; -import { Task } from "matrix-protection-suite"; /** * This file provides the entrypoint for the appservice mode for draupnir. @@ -31,15 +30,7 @@ const cli = new Cli({ if (config === null) { throw new Error("Couldn't load config"); } - void Task( - (async () => { - await MjolnirAppService.run( - port, - config, - cli.getRegistrationFilePath() - ); - })() - ); + void MjolnirAppService.run(port, config, cli.getRegistrationFilePath()); }, });