Skip to content

Commit c5bdc02

Browse files
tomopasCopilot
andcommitted
fix(cli): retry managed service port during shutdown
Closes anomalyco#54267 Co-authored-by: Copilot <[email protected]>
1 parent c458ad7 commit c5bdc02

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

‎packages/cli/src/server-process.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,14 @@ const processEffect = Effect.fnUntraced(function* (options: Options) {
151151
)
152152
const server = yield* launch.pipe(
153153
Effect.catch((error) => {
154-
if (findIncumbent === undefined || !addressInUse(error)) return Effect.fail(error)
154+
if (!addressInUse(error)) return Effect.fail(error)
155155
return Effect.gen(function* () {
156156
const deadline = Date.now() + 15_000
157157
while (Date.now() < deadline) {
158-
const found = yield* findIncumbent.pipe(Effect.timeoutOption(deadline - Date.now()))
159-
if (Option.isSome(found) && found.value !== undefined) return
158+
if (findIncumbent !== undefined) {
159+
const found = yield* findIncumbent.pipe(Effect.timeoutOption(deadline - Date.now()))
160+
if (Option.isSome(found) && found.value !== undefined) return
161+
}
160162
yield* Effect.sleep("100 millis")
161163
if (Date.now() >= deadline) break
162164
// Failed binds close their scope; a successful bind may take longer than this window to boot.

‎packages/cli/src/services/service-registration.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from "node:path"
55
import { Effect, FileSystem, Schedule, Schema } from "effect"
66
import { HttpServer } from "effect/http"
77
import { NetAddress } from "effect/net"
8+
import net from "node:net"
89
import { OPENCODE_VERSION } from "../version"
910

1011
const infoJson = Schema.fromJsonString(Service.Info)
@@ -66,7 +67,31 @@ export const register = Effect.fnUntraced(function* (options: {
6667
Effect.forkScoped,
6768
)
6869
return current.pipe(
69-
Effect.flatMap((found) => (owns(found) ? fs.remove(options.file) : Effect.void)),
70+
Effect.flatMap((found) =>
71+
owns(found)
72+
? Effect.promise(() => waitForPortRelease(options.address)).pipe(Effect.andThen(fs.remove(options.file)))
73+
: Effect.void,
74+
),
7075
Effect.ignore,
7176
)
7277
})
78+
79+
function waitForPortRelease(address: NetAddress.SocketAddress) {
80+
if (!NetAddress.isInetAddress(address)) return Promise.resolve()
81+
const host = NetAddress.formatIp(address.address)
82+
const port = address.port
83+
return new Promise<void>((resolve) => {
84+
let attempts = 0
85+
const check = () => {
86+
const socket = net.createConnection({ host, port })
87+
const release = (available: boolean) => {
88+
socket.destroy()
89+
if (available || ++attempts >= 150) resolve()
90+
else setTimeout(check, 100)
91+
}
92+
socket.once("error", () => release(true))
93+
socket.once("connect", () => release(false))
94+
}
95+
check()
96+
})
97+
}

0 commit comments

Comments
 (0)