Skip to content

Commit 6c8d919

Browse files
committed
Add a timeout in case posthog is holding up exit
1 parent c7957aa commit 6c8d919

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

cli/src/hooks/use-exit-handler.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import { useCallback, useEffect, useRef, useState } from 'react'
33
import { getCurrentChatId } from '../project-files'
44
import { flushAnalytics } from '../utils/analytics'
55
import { cleanupRenderer } from '../utils/renderer-cleanup'
6+
import { withTimeout } from '../utils/terminal-color-detection'
67

78
import type { InputValue } from '../state/chat-store'
89

10+
// Timeout for analytics flush during exit - don't block exit for too long
11+
const EXIT_FLUSH_TIMEOUT_MS = 1000
12+
913
interface UseExitHandlerOptions {
1014
inputValue: string
1115
setInputValue: (value: InputValue) => void
@@ -65,7 +69,7 @@ export const useExitHandler = ({
6569
exitWarningTimeoutRef.current = null
6670
}
6771

68-
flushAnalytics().then(() => {
72+
withTimeout(flushAnalytics(), EXIT_FLUSH_TIMEOUT_MS, undefined).then(() => {
6973
cleanupRenderer()
7074
process.exit(0)
7175
})
@@ -79,16 +83,12 @@ export const useExitHandler = ({
7983
exitWarningTimeoutRef.current = null
8084
}
8185

82-
const flushed = flushAnalytics()
83-
if (flushed && typeof (flushed as Promise<void>).finally === 'function') {
84-
;(flushed as Promise<void>).finally(() => {
86+
withTimeout(flushAnalytics(), EXIT_FLUSH_TIMEOUT_MS, undefined).finally(
87+
() => {
8588
cleanupRenderer()
8689
process.exit(0)
87-
})
88-
} else {
89-
cleanupRenderer()
90-
process.exit(0)
91-
}
90+
},
91+
)
9292
}
9393

9494
process.on('SIGINT', handleSigint)

0 commit comments

Comments
 (0)