-
Notifications
You must be signed in to change notification settings - Fork 1
Force factory daemon exit after graceful stop #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,8 @@ interface FleetCliDeps { | |
| probeCloser?: ProbeCloser | ||
| now?: () => number | ||
| stopSignalProcessLike?: Pick<NodeJS.Process, 'once' | 'off'> | ||
| daemonExit?: (code: number) => void | ||
| flushDaemonOutput?: () => Promise<void> | ||
| } | ||
|
|
||
| interface GlobalOptions { | ||
|
|
@@ -215,10 +217,19 @@ async function runFactoryCommand( | |
| if (command.action === 'start') { | ||
| const waiter = createStopSignalWaiter() | ||
| let stoppedBySignal = false | ||
| const flushAndExit = async (code: number): Promise<void> => { | ||
| try { | ||
| await (deps.flushDaemonOutput ?? flushProcessOutput)() | ||
| } finally { | ||
| const daemonExit = deps.daemonExit ?? ((exitCode: number) => process.exit(exitCode)) | ||
| daemonExit(code) | ||
| waiter.resolve(code) | ||
| } | ||
| } | ||
| const removeSignalHandlers = installFactoryStopSignalHandlers(factory, { | ||
| exit: (code) => { | ||
| stoppedBySignal = true | ||
| waiter.resolve(code) | ||
| void flushAndExit(code) | ||
| }, | ||
| processLike: deps.stopSignalProcessLike, | ||
| }) | ||
|
|
@@ -467,6 +478,23 @@ function writeJson(out: Pick<NodeJS.WriteStream, 'write'>, value: unknown): void | |
| out.write(`${JSON.stringify(value, null, 2)}\n`) | ||
| } | ||
|
|
||
| async function flushProcessOutput(): Promise<void> { | ||
| await Promise.all([ | ||
| flushWritable(process.stdout), | ||
| flushWritable(process.stderr), | ||
| ]) | ||
| } | ||
|
|
||
| function flushWritable(stream: NodeJS.WriteStream): Promise<void> { | ||
| if (stream.destroyed || stream.writableEnded || stream.writable === false) { | ||
| return Promise.resolve() | ||
| } | ||
|
|
||
| return new Promise((resolve) => { | ||
| stream.write('', () => resolve()) | ||
| }) | ||
| } | ||
|
Comment on lines
+481
to
+496
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the output streams ( To prevent this, we should:
async function flushProcessOutput(): Promise<void> {
let timeoutId: NodeJS.Timeout | undefined
const flushPromise = Promise.all([
flushWritable(process.stdout),
flushWritable(process.stderr),
])
const timeoutPromise = new Promise<void>((resolve) => {
timeoutId = setTimeout(resolve, 200)
})
await Promise.race([flushPromise, timeoutPromise])
if (timeoutId) {
clearTimeout(timeoutId)
}
}
function flushWritable(stream: NodeJS.WriteStream): Promise<void> {
if (stream.destroyed || stream.writableEnded || stream.writable === false) {
return Promise.resolve()
}
return new Promise<void>((resolve) => {
try {
stream.write('', () => resolve())
} catch {
resolve()
}
})
} |
||
|
|
||
| function isCapability(value: string | undefined): value is Capability { | ||
| return value === 'spawn:codex' || value === 'spawn:claude' | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
deps.flushDaemonOutputorflushProcessOutputrejects, the error will propagate out offlushAndExitbecause there is nocatchblock. SinceflushAndExitis called as a floating promise (void flushAndExit(code)), this will trigger an unhandled promise rejection. This is especially problematic in tests wheredaemonExitdoes not terminate the process.Adding a
catchblock ensures that any flush errors are safely ignored and do not cause unhandled rejections.