Skip to content

Commit 3774949

Browse files
committed
fix(cli): scope pipe resilience to service mode
1 parent 5098a58 commit 3774949

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

‎packages/cli/src/server-process.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Effect, FileSystem, Option, Redacted, Schedule, Schema } from "effect"
1212
import { HttpServer } from "effect/unstable/http"
1313
import { Env } from "./env"
1414
import { ServiceConfig } from "./services/service-config"
15+
import { guardServiceProcess } from "./stdio"
1516
import { Updater } from "./services/updater"
1617
import { WebUi } from "./services/web-ui"
1718

@@ -41,7 +42,11 @@ export const run = Effect.fnUntraced(function* (options: Options) {
4142

4243
const processEffect = Effect.fnUntraced(function* (options: Options) {
4344
const global = yield* Global.Service
44-
if (options.mode === "service") yield* Effect.sync(() => process.chdir(global.home))
45+
if (options.mode === "service")
46+
yield* Effect.sync(() => {
47+
guardServiceProcess()
48+
process.chdir(global.home)
49+
})
4550
return yield* Effect.scoped(
4651
Effect.gen(function* () {
4752
const foreground = options.mode === "default"

‎packages/cli/src/stdio.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
let installed = false
2+
let resilient = false
23

34
export function guardStdio() {
45
if (installed) return
56
installed = true
67
for (const stream of [process.stdout, process.stderr]) {
78
stream.on("error", (error) => {
8-
if (isEpipe(error)) return
9-
throw error
9+
if (!isEpipe(error)) throw error
10+
if (resilient) return
11+
if (stream === process.stdout) process.exit(0)
1012
})
1113
}
14+
}
15+
16+
export function guardServiceProcess() {
17+
guardStdio()
18+
if (resilient) return
19+
resilient = true
1220
process.on("uncaughtException", (error, origin) => {
1321
if (isEpipe(error)) return
1422
console.error(origin, error)

0 commit comments

Comments
 (0)