fix(core): don't duplicate MCP servers started during initial reconciliation (#58349)

This commit is contained in:
Aiden Cline
2026-10-23 00:52:52 -05:00
committed by GitHub
Unverified
parent 7b14ad4be0
commit 8ecd6add75
2 changed files with 54 additions and 1 deletions
+7 -1
View File
@@ -678,7 +678,13 @@ export const layer = (options?: Options) =>
yield* bus.publish(McpEvent.StatusChanged, { server: name })
continue
}
fork(startServer(name, entry).pipe(locks.withLock(name)))
fork(
Effect.gen(function* () {
// connect() or disconnect() may have settled the server while this startup waited to run.
if (entry.status.status !== "pending") return yield* entry.startup.open
yield* startServer(name, entry)
}).pipe(locks.withLock(name)),
)
}
return
}
+47
View File
@@ -2099,6 +2099,53 @@ shutdownIt.effect("discards in-flight and queued MCP notifications after its lay
}),
)
describe.each(["connect", "disconnect"] as const)("%s during initial MCP startup", (operation) => {
shutdownIt.live("is not undone by the queued startup", () =>
Effect.gen(function* () {
const bus = yield* Bus.Service
const server = yield* resourceServer()
const entered = yield* Deferred.make<void>()
const release = yield* Deferred.make<void>()
const root = yield* Scope.make()
yield* Effect.addFinalizer(() =>
Deferred.succeed(release, undefined).pipe(Effect.andThen(State.shutdown(Scope.close(root, Exit.void)))),
)
const service = Context.get(yield* Layer.buildWithScope(Mcp.layer(), root), Mcp.Service)
// Pausing the disabled server's status event holds initial reconciliation after every entry exists
// but before the enabled server's startup is scheduled.
yield* Effect.acquireRelease(
bus.listen((event) =>
Effect.gen(function* () {
if (event.type !== McpEvent.StatusChanged.type) return
if (Schema.decodeUnknownSync(McpEvent.StatusChanged.data)(event.data).server !== "gate") return
yield* Deferred.succeed(entered, undefined)
yield* Deferred.await(release)
}),
),
(unsubscribe) => unsubscribe,
)
const reconciling = yield* service
.transform((editor) => {
editor.set("gate", { type: "local", command: ["unused"], disabled: true })
editor.set("resources", { type: "remote", url: server.url, oauth: false })
})
.pipe(Scope.provide(root), Effect.forkChild({ startImmediately: true }))
yield* Deferred.await(entered)
yield* service[operation]("resources")
yield* Deferred.succeed(release, undefined)
yield* Fiber.join(reconciling)
// The queued startup already holds or awaits the server lock, so this waits for it to finish.
yield* service.disconnect("resources")
expect(server.state.initializations).toBe(operation === "connect" ? 1 : 0)
expect((yield* service.servers()).find((item) => item.name === "resources")?.status).toEqual({
status: "disabled",
})
}),
)
})
test("serializes concurrent MCP lifecycle operations", async () => {
await Effect.runPromise(
Effect.scoped(