fix(core): release location holders on shutdown (#59399)

This commit is contained in:
Aiden Cline
2026-10-30 23:28:04 -05:00
committed by GitHub
Unverified
parent e469e85a29
commit 1908fcb8b7
10 changed files with 357 additions and 15 deletions
+2 -1
View File
@@ -11,7 +11,7 @@ import { Socket } from "effect/socket"
import { Api } from "../api"
import { CorsConfig, isAllowedRequestOrigin } from "../cors"
import { locationErrors, requestRef, response } from "../location"
import { type Outbound, runPtySocket } from "./pty-socket"
import { holdLocation, type Outbound, runPtySocket } from "./pty-socket"
const notFound = <A, R>(effect: Effect.Effect<A, Mcp.NotFoundError, R>) =>
effect.pipe(Effect.mapError((error) => new McpServerNotFoundError({ server: error.server, message: error.message })))
@@ -112,6 +112,7 @@ export const McpHandler = HttpApiBuilder.group(Api, "server.mcp", (handlers) =>
const { McpSocketClient } = yield* Effect.promise(() => import("@opencode/core/mcp/socket-client"))
const socket = yield* Effect.orDie(ctx.request.upgrade)
const outbox = yield* Queue.unbounded<Outbound>()
yield* holdLocation(outbox)
const state = { attached: false, disconnected: false }
const transport = McpSocketClient.make({
send: (text) => Queue.offerUnsafe(outbox, text),
@@ -1,3 +1,4 @@
import { LocationLifecycle } from "@opencode/core/location-lifecycle"
import { Effect, Queue } from "effect"
import { Socket } from "effect/socket"
@@ -31,3 +32,16 @@ export function runPtySocket<R>(input: {
Effect.orDie,
)
}
// A socket borrows its Location for as long as it stays open. Its client reconnects right away when the socket
// closes, so it keeps the idle sweep from evicting the Location (eviction would only boot a fresh one). Any other
// shutdown (reload, VCS init, debug eviction) closes the socket with 1012, which releases the borrow so the
// Location's scope can close, and the client reconnects to the replacement.
export const holdLocation = Effect.fnUntraced(function* (outbox: Queue.Enqueue<Outbound>) {
const lifecycle = yield* LocationLifecycle.Service
yield* lifecycle.retain
yield* lifecycle.awaitShutdown.pipe(
Effect.andThen(Queue.offer(outbox, new Socket.CloseEvent(1012, "location shut down"))),
Effect.forkChild,
)
})
+3 -2
View File
@@ -16,7 +16,7 @@ import {
PTY_CONNECT_TOKEN_HEADER_VALUE,
} from "@opencode/protocol/groups/pty"
import { locationErrors, requestRef, response } from "../location"
import { type Outbound, runPtySocket } from "./pty-socket"
import { holdLocation, type Outbound, runPtySocket } from "./pty-socket"
const notFound = (ptyID: Pty.ID) => new PtyNotFoundError({ ptyID, message: `PTY session not found: ${ptyID}` })
@@ -137,6 +137,7 @@ export const PtyHandler = HttpApiBuilder.group(Api, "server.pty", (handlers) =>
const socket = yield* Effect.orDie(ctx.request.upgrade)
// TODO: Integrate graceful-shutdown socket tracking before clients migrate to this route.
const outbox = yield* Queue.unbounded<Outbound>()
yield* holdLocation(outbox)
const attachment = yield* pty
.attach(ctx.params.ptyID, {
cursor,
@@ -173,7 +174,7 @@ export const PtyHandler = HttpApiBuilder.group(Api, "server.pty", (handlers) =>
detach: () => attachment?.detach(),
})
return HttpServerResponse.empty()
}).pipe(Effect.provide(locations.get(ref)), locationErrors)
}).pipe(Effect.scoped, Effect.provide(locations.get(ref)), locationErrors)
}),
)
}),
+65 -1
View File
@@ -1,15 +1,18 @@
import { expect } from "bun:test"
import { NodeHttpServer } from "@effect/platform-node"
import { Location } from "@opencode/core/location"
import { AbsolutePath } from "@opencode/core/schema"
import { LocationLifecycle } from "@opencode/core/location-lifecycle"
import { LocationServiceMap } from "@opencode/core/location-service-map"
import { Mcp } from "@opencode/core/mcp/index"
import { Session } from "@opencode/core/session"
import { Tool } from "@opencode/core/tool"
import { McpTool } from "@opencode/core/tool/mcp"
import { Context, Effect, Exit, Layer, Schedule, Scope } from "effect"
import { Context, Effect, Exit, Layer, MutableHashMap, Option, Schedule, Scope } from "effect"
import { HttpRouter, HttpServer } from "effect/http"
import { createServer } from "node:http"
import path from "node:path"
import { connectClientMcp } from "../../client/src/mcp"
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
import { it } from "../../core/test/lib/effect"
import { ServerProcess } from "../src/process"
@@ -47,6 +50,7 @@ const serve = Effect.fnUntraced(function* () {
return {
base,
directory,
context,
url: (query: Record<string, string>) => {
const url = new URL("/api/experimental/client-mcp", base)
url.protocol = "ws:"
@@ -168,6 +172,66 @@ it.live(
20_000,
)
it.live(
"closes a client MCP socket with 1012 on reload and reattaches to the fresh location",
() =>
Effect.gen(function* () {
const server = yield* serve()
const events: string[] = []
class RecordingWebSocket extends globalThis.WebSocket {
constructor(...args: ConstructorParameters<typeof globalThis.WebSocket>) {
super(...args)
this.addEventListener("close", (event) => events.push(`close ${event.code}`))
this.addEventListener("message", (event) => {
if (String(event.data).includes('"notifications/initialized"')) events.push("attached")
})
}
}
const connection = yield* Effect.acquireRelease(
Effect.sync(() =>
connectClientMcp({
url: server.base,
name: "tui",
directory: server.directory,
headers: { authorization },
WebSocket: RecordingWebSocket,
tools: [{ name: "open_terminal", handler: () => ({ content: [{ type: "text", text: "opened" }] }) }],
}),
),
(connection) => Effect.promise(() => connection.close()),
)
yield* until(() => events.includes("attached"))
const locations = Context.get(server.context, LocationServiceMap.Service)
const ref = LocationServiceMap.canonical(Location.Ref.make({ directory: AbsolutePath.make(server.directory) }))
const entry = () =>
locations.rcMap.state._tag === "Open"
? Option.getOrUndefined(MutableHashMap.get(locations.rcMap.state.map, ref))
: undefined
const old = entry()
expect(old).toBeDefined()
// An attached client keeps the idle sweep away from its location.
const lifecycle = Context.get(yield* locations.contextEffect(ref).pipe(Effect.scoped), LocationLifecycle.Service)
expect(lifecycle.isRetained()).toBe(true)
const response = yield* Effect.promise(() =>
fetch(new URL("/api/location/reload", server.base), { method: "POST", headers: { authorization } }),
)
expect(response.status).toBe(204)
// The socket was the old location's last borrower, so closing it let the old location close.
yield* until(() => old?.scope.state._tag === "Closed")
// The client reconnected on its own and the replacement location attached it.
yield* until(() => events.length === 3)
expect(events).toEqual(["attached", "close 1012", "attached"])
expect(entry()).toBeDefined()
expect(entry()).not.toBe(old)
// Disconnect before the server stops, like the other tests here.
yield* Effect.promise(() => connection.close())
yield* until(() => events.length === 4)
}),
20_000,
)
it.live(
"accepts a single-use ticket instead of credentials",
() =>
@@ -128,6 +128,69 @@ live(
20_000,
)
live(
"PTY websocket closes with 1012 when its location reloads",
() =>
Effect.gen(function* () {
const directory = yield* Effect.acquireRelease(
Effect.promise(() => fs.mkdtemp(path.join(os.tmpdir(), "opencode-pty-reload-"))),
(directory) => Effect.promise(() => fs.rm(directory, { recursive: true, force: true })),
)
const server = yield* ServerProcess.start<never, never>({
hostname: "127.0.0.1",
port: 0,
password: "secret",
app: { version: "test-version" },
database: { path: ":memory:" },
models: { fetch: false },
config: { directory, project: false, content: "{}" },
fs: { filewatcher: false },
})
const base = HttpServer.formatAddress(server.address)
const client = OpenCode.make({ baseUrl: base, headers: { authorization: `Basic ${btoa("opencode:secret")}` } })
const location = { directory }
yield* Effect.promise(async () => {
const terminal = await client.pty.create({
location,
command: "/bin/sh",
args: ["-c", "sleep 30"],
cwd: directory,
})
const token = await client.pty.connect.token({ location, ptyID: terminal.data.id, "x-opencode-ticket": "1" })
const url = new URL(`/api/pty/${terminal.data.id}/connect`, base)
url.protocol = "ws:"
url.searchParams.set("location[directory]", directory)
url.searchParams.set("ticket", token.data.ticket)
const socket = new WebSocket(url)
const state = { open: false, code: 0 }
socket.addEventListener("open", () => (state.open = true))
socket.addEventListener("close", (event) => (state.code = event.code))
try {
await waitFor(() => state.open)
// The socket holds the location; reload must still close it so the old location can shut down.
await client.location.reload()
await waitFor(() => state.code !== 0)
expect(state.code).toBe(1012)
// Releasing the socket let the old location close, and its shell with it.
await waitFor(() => !alive(terminal.data.pid))
expect(await client.pty.get({ location, ptyID: terminal.data.id }).catch(() => undefined)).toBeUndefined()
} finally {
socket.close()
}
})
}),
20_000,
)
function alive(pid: number) {
try {
process.kill(pid, 0)
return true
} catch {
return false
}
}
async function waitFor(predicate: () => boolean) {
for (let attempt = 0; attempt < 200; attempt++) {
if (predicate()) return