Skip to content

Commit 60039f8

Browse files
anandgupta42claude
andcommitted
fix: flush session traces to disk on crash to prevent log loss (#593)
Bun Workers don't receive OS signals — only the main thread does. When altimate-code crashes, all in-memory traces were lost because the worker had no crash handlers. - worker.ts: add `flushAllTracesSync()` with idempotency guard, called from `uncaughtException` and `process.once("exit")` - thread.ts: add signal handlers (SIGINT/SIGTERM/SIGHUP) that call `worker.terminate()` + `Bun.sleepSync(50)` to trigger worker exit - index.ts: safety net for headless mode — flush `Trace.active` on `uncaughtException` and SIGHUP Closes #593 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 99270e5 commit 60039f8

3 files changed

Lines changed: 77 additions & 1 deletion

File tree

‎packages/opencode/src/cli/cmd/tui/thread.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,37 @@ export const TuiThreadCommand = cmd({
152152
process.on("unhandledRejection", error)
153153
process.on("SIGUSR2", reload)
154154

155+
// altimate_change start — crash: flush worker traces on signals
156+
// Bun Workers don't receive OS signals — only the main thread does.
157+
// On SIGINT/SIGTERM/SIGHUP, terminate the worker and briefly wait so
158+
// its "exit" handler fires and flushes all active session traces to disk.
159+
// Bun.sleepSync gives the worker thread time to process the termination
160+
// before the main thread continues to process.exit().
161+
const emergencyTerminate = () => {
162+
try {
163+
worker.terminate()
164+
Bun.sleepSync(50)
165+
} catch {
166+
// best-effort — crash handler must never throw
167+
}
168+
}
169+
process.on("SIGINT", emergencyTerminate)
170+
process.on("SIGTERM", emergencyTerminate)
171+
process.on("SIGHUP", emergencyTerminate)
172+
// altimate_change end
173+
155174
let stopped = false
156175
const stop = async () => {
157176
if (stopped) return
158177
stopped = true
159178
process.off("uncaughtException", error)
160179
process.off("unhandledRejection", error)
161180
process.off("SIGUSR2", reload)
181+
// altimate_change start — crash: remove emergency handlers on clean shutdown
182+
process.off("SIGINT", emergencyTerminate)
183+
process.off("SIGTERM", emergencyTerminate)
184+
process.off("SIGHUP", emergencyTerminate)
185+
// altimate_change end
162186
await withTimeout(client.call("shutdown", undefined), 5000).catch((error) => {
163187
Log.Default.warn("worker shutdown failed", {
164188
error: error instanceof Error ? error.message : String(error),

‎packages/opencode/src/cli/cmd/tui/worker.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ process.on("uncaughtException", (e) => {
3434
Log.Default.error("exception", {
3535
e: e instanceof Error ? e.message : e,
3636
})
37+
// altimate_change start — crash: flush traces on uncaught exception
38+
// After logging, write all active traces to disk so crash context is preserved.
39+
// The process may continue or exit depending on the exception — either way the
40+
// trace snapshot will reflect the crash.
41+
flushAllTracesSync(`Uncaught exception: ${e instanceof Error ? e.message : String(e)}`)
42+
// altimate_change end
3743
})
3844

3945
// Subscribe to global events and forward them via RPC
@@ -325,6 +331,29 @@ export const rpc = {
325331

326332
Rpc.listen(rpc)
327333

334+
// altimate_change start — crash: flush active traces on unexpected exit
335+
// When the worker is terminated (via worker.terminate() from the main thread,
336+
// or on uncaught exceptions), write all in-flight traces to disk synchronously.
337+
//
338+
// NOTE: Bun Workers do NOT receive OS signals (SIGINT, SIGTERM, SIGHUP) —
339+
// those are delivered only to the main thread. Signal-based flush is handled
340+
// in thread.ts by terminating the worker, which triggers the "exit" event here.
341+
let hasFlushed = false
342+
function flushAllTracesSync(reason: string) {
343+
if (hasFlushed) return
344+
hasFlushed = true
345+
for (const [, trace] of sessionTraces) {
346+
try {
347+
trace.flushSync(reason)
348+
} catch {
349+
// flushSync is best-effort — must never throw in an exit handler
350+
}
351+
}
352+
}
353+
354+
process.once("exit", () => { flushAllTracesSync("Process exited") })
355+
// altimate_change end
356+
328357
function getAuthorizationHeader(): string | undefined {
329358
const password = Flag.OPENCODE_SERVER_PASSWORD
330359
if (!password) return undefined

‎packages/opencode/src/index.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ import { Database } from "./storage/db"
4646
// altimate_change start - telemetry import
4747
import { Telemetry } from "./telemetry"
4848
// altimate_change end
49+
// altimate_change start — crash: import Trace for crash handlers
50+
import { Trace } from "./altimate/observability/tracing"
51+
// altimate_change end
4952
// altimate_change start - welcome banner
5053
import { showWelcomeBannerIfNeeded } from "./cli/welcome"
5154
// altimate_change end
@@ -60,12 +63,32 @@ process.on("uncaughtException", (e) => {
6063
Log.Default.error("exception", {
6164
e: e instanceof Error ? e.message : e,
6265
})
66+
// altimate_change start — crash: flush active trace on uncaught exception
67+
// Trace.active is set by run.ts (headless mode only — TUI traces live in
68+
// the worker's isolated memory and are flushed via worker.terminate()).
69+
// This is a safety net for the headless path where run.ts registers its
70+
// own handlers but an exception could bubble past them.
71+
try {
72+
Trace.active?.flushSync(`Uncaught exception: ${e instanceof Error ? e.message : String(e)}`)
73+
} catch {
74+
// Trace module may not be initialized — best-effort
75+
}
76+
// altimate_change end
6377
})
6478

6579
// Ensure the process exits on terminal hangup (eg. closing the terminal tab).
6680
// Without this, long-running commands like `serve` block on a never-resolving
6781
// promise and survive as orphaned processes.
68-
process.on("SIGHUP", () => process.exit())
82+
// altimate_change start — crash: flush active trace before SIGHUP exit
83+
process.on("SIGHUP", () => {
84+
try {
85+
Trace.active?.flushSync("Terminal hangup (SIGHUP)")
86+
} catch {
87+
// best-effort
88+
}
89+
process.exit()
90+
})
91+
// altimate_change end
6992

7093
let cli = yargs(hideBin(process.argv))
7194
.parserConfiguration({ "populate--": true })

0 commit comments

Comments
 (0)