fix crash if ServerService is disposed before being started

This commit is contained in:
Hazelnoot 2025-10-01 12:35:57 -04:00
parent 08fcb7f88c
commit 0684691993

View file

@ -40,7 +40,7 @@ const _dirname = fileURLToPath(new URL('.', import.meta.url));
@Injectable()
export class ServerService implements OnApplicationShutdown {
private logger: Logger;
#fastify: FastifyInstance;
#fastify?: FastifyInstance;
constructor(
@Inject(DI.config)
@ -316,7 +316,7 @@ export class ServerService implements OnApplicationShutdown {
await this.streamingApiServerService.detach();
this.logger.info('Disconnecting HTTP clients....;');
await this.#fastify.close();
await this.#fastify?.close();
this.logger.info('Server disposed.');
}
@ -325,6 +325,9 @@ export class ServerService implements OnApplicationShutdown {
* Get the Fastify instance for testing.
*/
public get fastify(): FastifyInstance {
if (!this.#fastify) {
throw new Error('Cannot get fastify before starting server');
}
return this.#fastify;
}