From 5fcf295ab81dc30fa9d48d70faf5398f2f7e18ad Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Wed, 24 Jul 2024 22:20:24 +0200 Subject: [PATCH] fix: Fix race condition in frontend extension (#23412) --- lib/extension/frontend.ts | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/lib/extension/frontend.ts b/lib/extension/frontend.ts index 7dea080289..1f448f7b71 100644 --- a/lib/extension/frontend.ts +++ b/lib/extension/frontend.ts @@ -31,20 +31,6 @@ export default class Frontend extends Extension { private fileServer: RequestHandler; private wss: WebSocket.Server = null; - constructor( - zigbee: Zigbee, - mqtt: MQTT, - state: State, - publishEntityState: PublishEntityState, - eventBus: EventBus, - enableDisableExtension: (enable: boolean, name: string) => Promise, - restartCallback: () => Promise, - addExtension: (extension: Extension) => Promise, - ) { - super(zigbee, mqtt, state, publishEntityState, eventBus, enableDisableExtension, restartCallback, addExtension); - this.eventBus.onMQTTMessagePublished(this, this.onMQTTPublishMessage); - } - private isHttpsConfigured(): boolean { if (this.sslCert && this.sslKey) { if (!fs.existsSync(this.sslCert) || !fs.existsSync(this.sslKey)) { @@ -82,6 +68,8 @@ export default class Frontend extends Extension { this.wss = new WebSocket.Server({noServer: true}); this.wss.on('connection', this.onWebSocketConnection); + this.eventBus.onMQTTMessagePublished(this, this.onMQTTPublishMessage); + if (!this.host) { this.server.listen(this.port); logger.info(`Started frontend on port ${this.port}`); @@ -169,12 +157,10 @@ export default class Frontend extends Extension { this.retainedMessages.set(topic, payload); } - if (this.wss) { - for (const client of this.wss.clients) { - /* istanbul ignore else */ - if (client.readyState === WebSocket.OPEN) { - client.send(stringify({topic, payload})); - } + for (const client of this.wss.clients) { + /* istanbul ignore else */ + if (client.readyState === WebSocket.OPEN) { + client.send(stringify({topic, payload})); } } }