fix: clear timeout handle to prevent event loop from staying alive
This commit is contained in:
@@ -48,17 +48,26 @@ export async function connectToDaemon(options?: ConnectOptions): Promise<DaemonC
|
|||||||
|
|
||||||
// Connect with timeout
|
// Connect with timeout
|
||||||
const connectPromise = client.connect()
|
const connectPromise = client.connect()
|
||||||
|
let timeoutHandle: ReturnType<typeof setTimeout> | null = null
|
||||||
const timeoutPromise = new Promise<never>((_, reject) => {
|
const timeoutPromise = new Promise<never>((_, reject) => {
|
||||||
setTimeout(() => {
|
timeoutHandle = setTimeout(() => {
|
||||||
reject(new Error(`Connection timeout after ${timeout}ms`))
|
reject(new Error(`Connection timeout after ${timeout}ms`))
|
||||||
}, timeout)
|
}, timeout)
|
||||||
})
|
})
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Promise.race([connectPromise, timeoutPromise])
|
await Promise.race([connectPromise, timeoutPromise])
|
||||||
|
// Clear the timeout so it doesn't keep the event loop alive
|
||||||
|
if (timeoutHandle) {
|
||||||
|
clearTimeout(timeoutHandle)
|
||||||
|
}
|
||||||
client.subscribeAgentUpdates({ subscriptionId: `cli:${process.pid}` })
|
client.subscribeAgentUpdates({ subscriptionId: `cli:${process.pid}` })
|
||||||
return client
|
return client
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
// Clear the timeout on error too
|
||||||
|
if (timeoutHandle) {
|
||||||
|
clearTimeout(timeoutHandle)
|
||||||
|
}
|
||||||
await client.close().catch(() => {})
|
await client.close().catch(() => {})
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user