Skip to content
This repository was archived by the owner on Feb 25, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/opencode/src/cli/cmd/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Server } from "../../server/server"
import { cmd } from "./cmd"
import { withNetworkOptions, resolveNetworkOptions } from "../network"
import { Flag } from "../../flag/flag"
import { Instance } from "../../project/instance" // kilocode_change

export const ServeCommand = cmd({
command: "serve",
Expand All @@ -14,7 +15,20 @@ export const ServeCommand = cmd({
const opts = await resolveNetworkOptions(args)
const server = Server.listen(opts)
console.log(`kilo server listening on http://${server.hostname}:${server.port}`) // kilocode_change
await new Promise(() => {})
await server.stop()
// kilocode_change start - graceful signal shutdown
const abort = new AbortController()
const shutdown = async () => {
try {
await Instance.disposeAll()
await server.stop(true)
} finally {
abort.abort()
}
}
process.on("SIGTERM", shutdown)
process.on("SIGINT", shutdown)
process.on("SIGHUP", shutdown)
await new Promise((resolve) => abort.signal.addEventListener("abort", resolve))
// kilocode_change end
},
})
7 changes: 7 additions & 0 deletions packages/opencode/src/cli/cmd/tui/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ export const TuiThreadCommand = cmd({
process.on("SIGUSR2", async () => {
await client.call("reload", undefined)
})
// kilocode_change start - graceful shutdown on external signals
const shutdown = async () => {
await client.call("shutdown", undefined).catch(() => {})
}
process.on("SIGHUP", shutdown)
process.on("SIGTERM", shutdown)
// kilocode_change end

const prompt = await iife(async () => {
const piped = !process.stdin.isTTY ? await Bun.stdin.text() : undefined
Expand Down
18 changes: 16 additions & 2 deletions packages/opencode/src/cli/cmd/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { UI } from "../ui"
import { cmd } from "./cmd"
import { withNetworkOptions, resolveNetworkOptions } from "../network"
import { Flag } from "../../flag/flag"
import { Instance } from "../../project/instance" // kilocode_change
import open from "open"
import { networkInterfaces } from "os"

Expand Down Expand Up @@ -75,7 +76,20 @@ export const WebCommand = cmd({
open(displayUrl).catch(() => {})
}

await new Promise(() => {})
await server.stop()
// kilocode_change start - graceful signal shutdown
const abort = new AbortController()
const shutdown = async () => {
try {
await Instance.disposeAll()
await server.stop(true)
} finally {
abort.abort()
}
}
process.on("SIGTERM", shutdown)
process.on("SIGINT", shutdown)
process.on("SIGHUP", shutdown)
await new Promise((resolve) => abort.signal.addEventListener("abort", resolve))
// kilocode_change end
},
})
5 changes: 4 additions & 1 deletion packages/opencode/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import { EOL } from "os"
import { WebCommand } from "./cli/cmd/web"
import { PrCommand } from "./cli/cmd/pr"
import { SessionCommand } from "./cli/cmd/session"
// kilocode_change start - Import telemetry and legacy migration
// kilocode_change start - Import telemetry, instance disposal, and legacy migration
import { Telemetry } from "@kilocode/kilo-telemetry"
import { Instance } from "./project/instance" // kilocode_change
import { migrateLegacyKiloAuth, ENV_FEATURE } from "@kilocode/kilo-gateway"

// kilocode_change - set feature for tracking. 'serve' is spawned by other services
Expand Down Expand Up @@ -197,6 +198,8 @@ try {
await Telemetry.shutdown()
// kilocode_change end

await Instance.disposeAll() // kilocode_change - safety net disposal (no-op if already disposed)

// Some subprocesses don't react properly to SIGTERM and similar signals.
// Most notably, some docker-container-based MCP servers don't handle such signals unless
// run using `docker run --init`.
Expand Down
Loading