Skip to content

Commit 3a64a89

Browse files
committed
fix: cleanup js_run_devserver watch protocol socket callbacks
1 parent 8a818b7 commit 3a64a89

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

js/private/js_run_devserver.mjs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -781,8 +781,13 @@ class AspectWatchProtocol {
781781
await new Promise((resolve, reject) => {
782782
// Initial connection + success vs failure
783783
this.connection.once('error', reject)
784-
this.connection.once('connect', resolve)
785-
this.connection.connect({ path: this.socketFile })
784+
try {
785+
this.connection.connect({ path: this.socketFile }, resolve)
786+
} catch (err) {
787+
reject(err)
788+
} finally {
789+
this.connection.off('error', reject)
790+
}
786791
})
787792

788793
await this._receive('NEGOTIATE')
@@ -847,14 +852,33 @@ class AspectWatchProtocol {
847852
const dataBufs = []
848853
const connection = this.connection
849854

850-
connection.on('data', function dataReceived(data) {
855+
connection.once('error', onError)
856+
connection.once('close', onError)
857+
connection.on('data', dataReceived)
858+
859+
// Destructor removing all temporary event handlers.
860+
function removeHandlers() {
861+
connection.off('error', onError)
862+
connection.off('close', onError)
863+
connection.off('data', dataReceived)
864+
}
865+
866+
// Error event handler
867+
function onError(err) {
868+
removeHandlers()
869+
reject(err)
870+
}
871+
872+
// Data event handler to receive data and determine when to resolve the promise.
873+
function dataReceived(data) {
851874
dataBufs.push(data)
852875

853876
if (data.at(data.byteLength - 1) !== '\n'.charCodeAt(0)) {
854877
return
855878
}
856879

857-
connection.off('data', dataReceived)
880+
// Removal all temporary event handlers before resolving the promise
881+
removeHandlers()
858882

859883
try {
860884
const msg = JSON.parse(
@@ -872,7 +896,7 @@ class AspectWatchProtocol {
872896
} catch (e) {
873897
reject(e)
874898
}
875-
})
899+
}
876900
})
877901
}
878902

0 commit comments

Comments
 (0)