mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
fix(core): release location holders on shutdown (#59399)
This commit is contained in:
committed by
GitHub
Unverified
parent
e469e85a29
commit
1908fcb8b7
@@ -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,
|
||||
)
|
||||
})
|
||||
|
||||
@@ -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)
|
||||
}),
|
||||
)
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user