mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-26 23:28:43 +00:00
fix(core): re-provision workspace when destroy and recreate race connect (#66684)
This commit is contained in:
parent
7290a572cc
commit
42542eab45
2 changed files with 113 additions and 12 deletions
|
|
@ -282,9 +282,8 @@ const layer = (options: Options) =>
|
|||
if (existing) return existing
|
||||
|
||||
const row = yield* load(workspaceID)
|
||||
// Bindings are persisted before provision resolves and never nulled; a raced
|
||||
// destroy deletes the whole row and surfaces as NotFound from load above.
|
||||
if (!row.binding) return yield* Effect.die(new Error(`workspace ${workspaceID} has no binding after provision`))
|
||||
// Unbound (fresh create, or destroy+create raced while waiting): caller provisions outside the lock.
|
||||
if (!row.binding) return undefined
|
||||
const driver = yield* registry.get(row.provider)
|
||||
const scope = yield* Scope.fork(lifetime)
|
||||
const environment = yield* driver
|
||||
|
|
@ -479,19 +478,22 @@ const layer = (options: Options) =>
|
|||
connect: (workspaceID) =>
|
||||
Effect.sync(() => {
|
||||
const acquire = Effect.acquireRelease(
|
||||
// A live connection implies the binding is already persisted, so skip the provision hop.
|
||||
Effect.suspend(() => (connections.has(workspaceID) ? Effect.void : provision(workspaceID))).pipe(
|
||||
Effect.andThen(
|
||||
locks.withLock(workspaceID)(
|
||||
Effect.gen(function* () {
|
||||
while (true) {
|
||||
const connection = yield* locks.withLock(workspaceID)(
|
||||
Effect.gen(function* () {
|
||||
const connection = yield* open(workspaceID)
|
||||
if (!connection) return undefined
|
||||
yield* Ref.set(connection.lastActivity, yield* Clock.currentTimeMillis)
|
||||
yield* Ref.update(connection.active, (active) => active + 1)
|
||||
return connection
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
if (connection) return connection
|
||||
// Provision takes the same lock. Release it first, then recheck in case destroy/create raced again.
|
||||
yield* provision(workspaceID)
|
||||
}
|
||||
}),
|
||||
(connection) =>
|
||||
locks.withLock(workspaceID)(
|
||||
Effect.gen(function* () {
|
||||
|
|
|
|||
|
|
@ -569,6 +569,8 @@ const makeAlarmHarness = (
|
|||
readonly pollInterval?: Workspace.Options["pollInterval"]
|
||||
readonly create?: WorkspaceDriver.Interface["create"]
|
||||
readonly connect?: WorkspaceDriver.Interface["connect"]
|
||||
readonly close?: Effect.Effect<void>
|
||||
readonly suspendForIdle?: WorkspaceDriver.Interface["suspendForIdle"]
|
||||
} = {},
|
||||
) => {
|
||||
const sharedDb = Database.configuredClient(sqliteLayer({ storage: makeDurableObjectStorage() }))
|
||||
|
|
@ -588,12 +590,14 @@ const makeAlarmHarness = (
|
|||
state.openCount += 1
|
||||
bindings.push(input.binding)
|
||||
}).pipe(Effect.andThen(options.connect ? options.connect(input) : Effect.succeed(memory))),
|
||||
() => Effect.sync(() => void (state.closeCount += 1)),
|
||||
() => Effect.sync(() => void (state.closeCount += 1)).pipe(Effect.andThen(options.close ?? Effect.void)),
|
||||
),
|
||||
suspendForIdle: (input) =>
|
||||
state.failSuspend
|
||||
? Effect.fail(new WorkspaceDriver.Error({ message: "provider timeout" }))
|
||||
: Effect.sync(() => void suspended.push(input.workspaceID)),
|
||||
: Effect.sync(() => void suspended.push(input.workspaceID)).pipe(
|
||||
Effect.andThen(options.suspendForIdle ? options.suspendForIdle(input) : Effect.void),
|
||||
),
|
||||
destroy: () => Effect.void,
|
||||
})
|
||||
|
||||
|
|
@ -623,6 +627,101 @@ const makeAlarmHarness = (
|
|||
}
|
||||
}
|
||||
|
||||
bareIt.effect("re-provisions when a warm connection is destroyed and recreated before acquisition", () =>
|
||||
Effect.gen(function* () {
|
||||
const gate = yield* makeGate()
|
||||
const harness = makeAlarmHarness({ keepWarm: "5 minutes", suspendForIdle: () => gate.wait })
|
||||
|
||||
yield* harness.run((workspace) =>
|
||||
Effect.gen(function* () {
|
||||
const id = yield* workspace.attach({ provider: "fake", binding: { name: "old" } })
|
||||
const files = makeFiles(yield* workspace.connect(id))
|
||||
yield* files.mkdir("/before-race")
|
||||
yield* TestClock.adjust("6 minutes")
|
||||
|
||||
const idle = yield* workspace.suspendIdle().pipe(Effect.forkScoped({ startImmediately: true }))
|
||||
yield* Deferred.await(gate.started)
|
||||
const recreate = yield* workspace
|
||||
.destroy(id)
|
||||
.pipe(
|
||||
Effect.andThen(workspace.create({ id, provider: "fake" })),
|
||||
Effect.forkScoped({ startImmediately: true }),
|
||||
)
|
||||
// The idle driver still holds the lock and the warm connection is still cached.
|
||||
expect(yield* workspace.connected(id)).toBe(true)
|
||||
const op = yield* files.mkdir("/after-race").pipe(Effect.forkScoped({ startImmediately: true }))
|
||||
|
||||
yield* Deferred.succeed(gate.release, undefined)
|
||||
yield* Fiber.join(idle)
|
||||
expect(yield* Fiber.join(recreate)).toBe(id)
|
||||
yield* Fiber.join(op)
|
||||
expect(harness.bindings).toEqual([{ name: "old" }, { workspaceID: id, generation: 0 }])
|
||||
expect(harness.counts()).toEqual({ openCount: 2, closeCount: 1 })
|
||||
}),
|
||||
)
|
||||
expect(harness.counts()).toEqual({ openCount: 2, closeCount: 2 })
|
||||
}),
|
||||
)
|
||||
|
||||
bareIt.effect("returns NotFound when a warm connection is destroyed before acquisition", () =>
|
||||
Effect.gen(function* () {
|
||||
const gate = yield* makeGate()
|
||||
const harness = makeAlarmHarness({ keepWarm: "5 minutes", suspendForIdle: () => gate.wait })
|
||||
|
||||
yield* harness.run((workspace) =>
|
||||
Effect.gen(function* () {
|
||||
const id = yield* workspace.attach({ provider: "fake", binding: { name: "deleted" } })
|
||||
const files = makeFiles(yield* workspace.connect(id))
|
||||
yield* files.mkdir("/before-delete")
|
||||
yield* TestClock.adjust("6 minutes")
|
||||
|
||||
const idle = yield* workspace.suspendIdle().pipe(Effect.forkScoped({ startImmediately: true }))
|
||||
yield* Deferred.await(gate.started)
|
||||
const destroy = yield* workspace.destroy(id).pipe(Effect.forkScoped({ startImmediately: true }))
|
||||
expect(yield* workspace.connected(id)).toBe(true)
|
||||
const op = yield* files.mkdir("/after-delete").pipe(Effect.flip, Effect.forkScoped({ startImmediately: true }))
|
||||
|
||||
yield* Deferred.succeed(gate.release, undefined)
|
||||
yield* Fiber.join(idle)
|
||||
expect(yield* Fiber.join(destroy)).toEqual({ destroyed: true })
|
||||
expect(yield* Fiber.join(op)).toEqual(
|
||||
new Failed({ path: "/after-delete", cause: new Workspace.NotFound({ workspaceID: id }) }),
|
||||
)
|
||||
expect(yield* workspace.exists(id)).toBe(false)
|
||||
expect(harness.counts()).toEqual({ openCount: 1, closeCount: 1 })
|
||||
}),
|
||||
)
|
||||
expect(harness.counts()).toEqual({ openCount: 1, closeCount: 1 })
|
||||
}),
|
||||
)
|
||||
|
||||
bareIt.effect("keepWarm 0 serializes a new acquisition behind the last release without leaking scopes", () =>
|
||||
Effect.gen(function* () {
|
||||
const gate = yield* makeGate()
|
||||
const harness = makeAlarmHarness({ close: gate.wait })
|
||||
|
||||
yield* harness.run((workspace) =>
|
||||
Effect.gen(function* () {
|
||||
const id = yield* workspace.attach({ provider: "fake", binding: { name: "closing" } })
|
||||
const files = makeFiles(yield* workspace.connect(id))
|
||||
const first = yield* files.mkdir("/first-close").pipe(Effect.forkScoped({ startImmediately: true }))
|
||||
yield* Deferred.await(gate.started)
|
||||
|
||||
const second = yield* files.mkdir("/second-close").pipe(Effect.forkScoped({ startImmediately: true }))
|
||||
expect(yield* workspace.connected(id)).toBe(false)
|
||||
expect(harness.counts()).toEqual({ openCount: 1, closeCount: 1 })
|
||||
|
||||
yield* Deferred.succeed(gate.release, undefined)
|
||||
yield* Fiber.join(first)
|
||||
yield* Fiber.join(second)
|
||||
expect(yield* workspace.connected(id)).toBe(false)
|
||||
expect(harness.counts()).toEqual({ openCount: 2, closeCount: 2 })
|
||||
}),
|
||||
)
|
||||
expect(harness.counts()).toEqual({ openCount: 2, closeCount: 2 })
|
||||
}),
|
||||
)
|
||||
|
||||
bareIt.effect("keepWarm 0 shares a connection across overlapping ops and closes immediately on last release", () =>
|
||||
Effect.gen(function* () {
|
||||
const gate = yield* makeGate()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue