Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/concurrent-start-ownership.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"opencode-drive": patch
---

Prevent concurrent detached launchers from stealing prepared instance ownership and spawning competing daemon processes.
17 changes: 12 additions & 5 deletions packages/drive/src/cli/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,22 @@ export const start = Effect.fn("DriveCli.start")((options: StartOptions) =>
)

const startScoped = Effect.fn("DriveCli.startScoped")(function* (options: StartOptions) {
const initializerPid = Number.parseInt(
options.daemon ? process.env.OPENCODE_DRIVE_INITIALIZER_PID ?? "" : "",
10,
)
if (options.daemon) delete process.env.OPENCODE_DRIVE_INITIALIZER_PID
const initialized = yield* fromPromise(() =>
initializeManifest(
options.name,
process.cwd(),
() => initializeInstance(options.name),
{ temporary: true },
{
temporary: true,
...(Number.isInteger(initializerPid) && initializerPid > 0
? { adoptPid: initializerPid }
: {}),
},
),
)
configureLogFile(initialized.artifacts)
Expand Down Expand Up @@ -394,10 +404,6 @@ const startDetached = Effect.fn("DriveCli.startDetached")(function* (
options: StartOptions,
artifacts: string,
) {
const existing = yield* fromPromise(() =>
resolveInstance(options.name, { ready: false }).catch(() => undefined),
)
if (existing) throw new Error(`drive instance "${options.name}" is already running`)
const ownerLog = join(registryDirectory(), `${options.name}.log`)
yield* fromPromise(() => mkdir(registryDirectory(), { recursive: true }))
yield* fromPromise(() => rm(ownerLog, { force: true }))
Expand All @@ -419,6 +425,7 @@ const startDetached = Effect.fn("DriveCli.startDetached")(function* (
...process.env,
OPENCODE_DRIVE_LOG: configureLogFile(artifacts),
OPENCODE_DRIVE_OWNER_LOG: ownerLog,
OPENCODE_DRIVE_INITIALIZER_PID: String(process.pid),
},
stdin: "ignore",
stdout: "ignore",
Expand Down
36 changes: 31 additions & 5 deletions packages/drive/src/instance/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,43 @@ export async function initializeManifest(
name: string,
cwd: string,
create: () => Promise<string>,
options: { readonly temporary?: boolean } = {},
options: {
readonly temporary?: boolean
readonly adoptPid?: number
} = {},
) {
let initialized: InitializedManifest | undefined
await withLock(name, false, async () => {
let existing = await read(manifestPath(name))
if (existing?.status === "initialized") {
if (
options.temporary &&
options.adoptPid !== undefined &&
existing.pid === options.adoptPid
) {
initialized = { ...existing, pid: process.pid }
await write(initialized)
return
}
if (!keepInitialized(existing)) {
await Promise.all([
rm(manifestPath(name), { force: true }),
rm(controlPath(name), { force: true }),
])
existing = undefined
} else {
initialized =
options.temporary && existing.temporary
? { ...existing, pid: process.pid }
: existing
let available = existing
if (existing.pid !== undefined && existing.pid !== process.pid) {
if (isProcessAlive(existing.pid))
throw new Error(`drive instance "${name}" is already starting`)
const { pid: _, ...released } = existing
available = released
}
if (options.temporary && available.pid === undefined) {
initialized = { ...available, pid: process.pid }
} else {
initialized = available
}
if (initialized !== existing) await write(initialized)
return
}
Expand Down Expand Up @@ -101,6 +121,12 @@ export async function register(manifest: InstanceManifest) {
}
await withLock(manifest.name, false, async () => {
const existing = await read(manifestPath(manifest.name))
if (
existing?.status === "initialized" &&
existing.pid !== undefined &&
existing.pid !== manifest.pid
)
throw new Error(`drive instance "${manifest.name}" changed ownership`)
if (existing && existing.status !== "initialized" && isProcessAlive(existing.pid))
throw new Error(`drive instance "${manifest.name}" is already running`)
await Promise.all([
Expand Down
Loading