Skip to content

Commit c1ada30

Browse files
fix: Opencode hangs after exit (#3481)
Co-authored-by: Aiden Cline <[email protected]>
1 parent 51e4c9f commit c1ada30

12 files changed

Lines changed: 86 additions & 36 deletions

File tree

‎packages/opencode/src/acp/session.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { McpServer } from "@agentclientprotocol/sdk"
2-
import { Identifier } from "../id/id"
32
import { Session } from "../session"
43
import { Provider } from "../provider/provider"
54
import type { ACPSessionState } from "./types"

‎packages/opencode/src/cli/bootstrap.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ export async function bootstrap<T>(directory: string, cb: () => Promise<T>) {
66
directory,
77
init: InstanceBootstrap,
88
fn: async () => {
9-
const result = await cb()
10-
await Instance.dispose()
11-
return result
9+
try {
10+
const result = await cb()
11+
return result
12+
} finally {
13+
await Instance.dispose()
14+
}
1215
},
1316
})
1417
}

‎packages/opencode/src/cli/cmd/acp.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ export const AcpCommand = cmd({
5656
}, stream)
5757

5858
log.info("setup connection")
59+
process.stdin.resume()
60+
await new Promise((resolve, reject) => {
61+
process.stdin.on("end", resolve)
62+
process.stdin.on("error", reject)
63+
})
5964
})
60-
process.stdin.resume()
6165
},
6266
})

‎packages/opencode/src/cli/cmd/serve.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ export const ServeCommand = cmd({
2727
})
2828
console.log(`opencode server listening on http://${server.hostname}:${server.port}`)
2929
await new Promise(() => {})
30-
server.stop()
30+
await server.stop()
3131
},
3232
})

‎packages/opencode/src/config/config.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,16 @@ export namespace Config {
7777
log.debug("loading config from OPENCODE_CONFIG_DIR", { path: Flag.OPENCODE_CONFIG_DIR })
7878
}
7979

80+
const promises: Promise<void>[] = []
8081
for (const dir of directories) {
8182
await assertValid(dir)
82-
installDependencies(dir)
83+
promises.push(installDependencies(dir))
8384
result.command = mergeDeep(result.command ?? {}, await loadCommand(dir))
8485
result.agent = mergeDeep(result.agent, await loadAgent(dir))
8586
result.agent = mergeDeep(result.agent, await loadMode(dir))
8687
result.plugin.push(...(await loadPlugin(dir)))
8788
}
89+
await Promise.all(promises)
8890

8991
// Migrate deprecated mode field to agent field
9092
for (const [name, mode] of Object.entries(result.mode)) {

‎packages/opencode/src/file/watcher.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ export namespace FileWatcher {
6363
return { sub }
6464
},
6565
async (state) => {
66-
state.sub?.unsubscribe()
66+
if (!state.sub) return
67+
await state.sub?.unsubscribe()
6768
},
6869
)
6970

‎packages/opencode/src/index.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import { AttachCommand } from "./cli/cmd/attach"
2222
import { AcpCommand } from "./cli/cmd/acp"
2323
import { EOL } from "os"
2424

25-
const cancel = new AbortController()
26-
2725
process.on("unhandledRejection", (e) => {
2826
Log.Default.error("rejection", {
2927
e: e instanceof Error ? e.message : e,
@@ -135,6 +133,10 @@ try {
135133
console.error(e)
136134
}
137135
process.exitCode = 1
136+
} finally {
137+
// Some subprocesses don't react properly to SIGTERM and similar signals.
138+
// Most notably, some docker-container-based MCP servers don't handle such signals unless
139+
// run using `docker run --init`.
140+
// Explicitly exit to avoid any hanging subprocesses.
141+
process.exit();
138142
}
139-
140-
cancel.abort()

‎packages/opencode/src/lsp/index.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ export namespace LSP {
101101
}
102102
},
103103
async (state) => {
104-
for (const client of state.clients) {
105-
await client.shutdown()
106-
}
104+
await Promise.all(state.clients.map((client) => client.shutdown()))
107105
},
108106
)
109107

‎packages/opencode/src/mcp/index.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ export namespace MCP {
4545
}
4646
},
4747
async (state) => {
48-
for (const client of Object.values(state.clients)) {
49-
client.close()
50-
}
48+
await Promise.all(Object.values(state.clients).map((client) => client.close()))
5149
},
5250
)
5351

‎packages/opencode/src/project/bootstrap.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export async function InstanceBootstrap() {
1111
await Plugin.init()
1212
Share.init()
1313
Format.init()
14-
LSP.init()
14+
await LSP.init()
1515
FileWatcher.init()
1616
File.init()
1717
}

0 commit comments

Comments
 (0)