@samitouri / QOSami-HFS / commits / 9cdbd3b4

fix: admin/internet: small memory leak at each access of the page, generating console warning after a while

Massimo Melina committed Apr 14, 2025 at 00:16 UTC 9cdbd3b4a5f4633d84687cdcf319d6205277c3b1
1 file changed +16 -6
src/ddns.ts
+16 -6
@@ -7,6 +7,7 @@ import { isIPv6 } from 'net'
7 import { VERSION } from './const'
8 import events from './events'
9 import { getPublicIps } from './nat'
10 +import { Readable } from 'node:stream'
11
12 // optionally you can append '>' and a regular expression to determine what body is considered successful
13 const dynamicDnsUrl = defineConfig(CFG.dynamic_dns_url, '')
@@ -57,10 +58,19 @@ dynamicDnsUrl.sub(v => {
58 }, { callNow: true })
59 })
60
60 -export async function* get_dynamic_dns_error() {
61 - if (last) yield last
62 - while (1) {
63 - const res = await events.once('dynamicDnsError')
64 - yield res[0]
65 - }
61 +export async function get_dynamic_dns_error() {
62 + let unsub: any
63 + return new Readable({
64 + objectMode: true,
65 + async read() {
66 + if (unsub) return
67 + if (last)
68 + this.push(last) // start by sending current state
69 + unsub = events.on('dynamicDnsError', x => this.push(x)) // send updates, if any. This simplified way to manage the data stream is acceptable for this case of extremely low throughput
70 + },
71 + async destroy() {
72 + unsub()
73 + this.push(null)
74 + }
75 + })
76 }
\ No newline at end of file