mirror of
https://github.com/getpaseo/paseo.git
synced 2026-07-29 12:01:31 +00:00
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
|
||||
const connectPromise = client.connect()
|
||||
let timeoutHandle: ReturnType<typeof setTimeout> | null = null
|
||||
const timeoutPromise = new Promise<never>((_, reject) => {
|
||||
setTimeout(() => {
|
||||
timeoutHandle = setTimeout(() => {
|
||||
reject(new Error(`Connection timeout after ${timeout}ms`))
|
||||
}, timeout)
|
||||
})
|
||||
|
||||
try {
|
||||
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}` })
|
||||
return client
|
||||
} catch (err) {
|
||||
// Clear the timeout on error too
|
||||
if (timeoutHandle) {
|
||||
clearTimeout(timeoutHandle)
|
||||
}
|
||||
await client.close().catch(() => {})
|
||||
throw err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user