Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/matter-server/src/MatterServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ let config: ConfigStorage;
let legacyData: LegacyData;
let legacyDataWriter: LegacyDataWriter | undefined;
let fileLoggerClose: (() => Promise<void>) | undefined;
let stopping = false;
let startCompleted: Promise<void> = Promise.resolve();

async function start() {
// Set up file logging additionally to the console if configured
Expand Down Expand Up @@ -190,6 +192,19 @@ async function start() {
}

async function stop() {
if (stopping) {
return;
}
stopping = true;

// Wait for start() to finish (or fail) before tearing down, so we don't
// race against in-flight initialization that could re-create resources.
try {
await startCompleted;
} catch {
// start() failed - that's fine, we still need to clean up
}

try {
await server?.stop();
} catch (err) {
Expand Down Expand Up @@ -229,8 +244,10 @@ async function stop() {
}
}

start().catch(async err => {
console.error(err);
startCompleted = start().catch(async err => {
if (!stopping) {
console.error(err);
}
await config?.close();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,10 @@ export class ControllerCommandHandler {
}

async close() {
await this.#customClusterPoller.stop();
if (!this.#started) {
return;
}
await this.#customClusterPoller.stop();
return this.#controller.close();
}

Expand Down
Loading