fix(server): provider sessions clean up when their start is interrupted #15571
Mergedjuliusmarmingemerged 17 commits into
main fromrefs/pull/15571/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
juliusmarminge added labelsmacroscope-review
- apps/server/src/orchestration-v2/Adapters/PiAdapterV2.ts line 2397OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2382,6 +2382,8 @@23822382 }23832383 }).pipe(23842384 sessionEventPermit.withPermits(1),2385- Effect.tapError(() =>2385+ // On interrupt too: a Stop that lands here must not leave the2386+ // turn installed, or every later turn is rejected as active.2387+ Effect.onError(() =>23862388 Effect.sync(() => {23872389 if (state.activeTurn === activeTurn) state.activeTurn = null;apps/server/src/orchestration-v2/ProviderSessionManager.ts line 1791OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1771,5 +1785,7 @@17711785 })17721786 .pipe(17731787 Effect.provideService(Scope.Scope, sessionScope),1774- Effect.tapError(() =>1788+ // Any failure, including a Stop that interrupts a slow1789+ // handshake, stops the provider process this open started.1790+ Effect.onError(() =>17751791 Scope.close(sessionScope, Exit.void).pipe(
- apps/server/src/orchestration-v2/Adapters/PiAdapterV2.ts line 2387OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -2382,4 +2382,6 @@23822382 }23832383 }).pipe(23842384 sessionEventPermit.withPermits(1),2385- Effect.tapError(() =>2385+ // On interrupt too: a Stop that lands here must not leave the2386+ // turn installed, or every later turn is rejected as active.2387+ Effect.onError(() =>
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/orchestration-v2/Adapters/PiAdapterV2.ts line 2354OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2342,11 +2342,13 @@23422342 activeProviderRetry: null,23432343 failure: null,23442344 };2345+ // Set together with the send, with no gap for an interrupt to land in.2346+ let sent = false;23452347 // Only the install/send/start-event boundary excludes the event23462348 // pump. Earlier correlated requests must leave the pump free so23472349 // project trust, login, and session-switch dialogs can be shown23482350 // and answered instead of deadlocking the caller.23492351 yield* Effect.gen(function* () {23502352 state.activeTurn = activeTurn;23512353 if (compactCommand !== null) {2352- yield* connection.send(compactRpcRecord(compactCommand));2354+ yield* connection.send(compactRpcRecord(compactCommand)).pipe(
- apps/server/src/orchestration-v2/ProviderSessionManager.ts line 1284OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1273,17 +1279,6 @@12731279 }12741280 }).pipe(1275- Effect.tapError(() =>1276- removeThreadAttachment(input).pipe(1277- // Revoke only a credential this attach freshly minted: a REUSED1278- // credential is by definition held by another live provider1279- // process, and revoking it thread-wide would break that1280- // process's MCP client mid-conversation.1281- Effect.andThen(1282- Effect.suspend(() => {1283- dropReservation();1284- return preparedForCleanup?.issued === true1285- ? clearMcpSession(input.threadId, preparedForCleanup.mcpCredentialId)1286- : Effect.void;1287- }),1288- ),1289- ),1281+ // An interrupted attach is undone too, so the next attach writes1282+ // the attachment instead of finding the thread already attached.1283+ Effect.onError(() =>1284+ attachedHereapps/server/src/orchestration-v2/ProviderSessionManager.ts lines 1604 to 1633Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1510,9 +1520,7 @@15101520 ).pipe(15111521 Effect.andThen(observeActivity(providerSessionId, markBusy(providerSessionId))),15121522 Effect.andThen(runtime.startTurn(input)),1513- Effect.catch((error) =>1514- observeActivity(providerSessionId, markIdle(providerSessionId)).pipe(1515- Effect.andThen(Effect.fail(error)),1516- ),1517- ),1523+ // A start that fails or is stopped never emits turn.terminal, so1524+ // the busy mark is undone here or the session never goes idle.1525+ Effect.onError(() => observeActivity(providerSessionId, markIdle(providerSessionId))),15181526 ),
- apps/server/src/device/LocalDeviceHost.ts line 442Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -413,0 +394,21 @@394+ }),395+ ),396+ );397+ const startedAtMillis = yield* Clock.currentTimeMillis;398+ const hub: HubProcess = { child, scope, origin, startedAtMillis, nodePath };399+ yield* Effect.forkIn(observeHubOutput(hub), scope);400+ yield* waitForHttpReady({401+ baseUrl: origin,402+ path: "/readyz",403+ timeoutMs: HUB_READY_TIMEOUT_MS,404+ makeError: (info) =>405+ new DeviceHost.DeviceHostError({406+ hostId,407+ step: "waiting for the device hub to answer",408+ cause: info.cause,409+ }),410+ }).pipe(Effect.provideService(HttpClient.HttpClient, httpClient));411+ yield* recordHub(hub, hubTool);412+ yield* Effect.logInfo("Device hub started", { pid: Number(child.pid), port });413+ return hub;414+ }).pipe(Effect.onError(() => Scope.close(scope, Exit.void).pipe(Effect.ignore)));
- apps/server/src/device/LocalDeviceHost.ts line 627OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -622,8 +623,5 @@622623 );623624 yield* onPhase("starting");624625 const hub = yield* spawnHub(hubTool, nodePath);625- yield* pruneLocalDeviceTools(config.baseDir, nodePath, "hub").pipe(626- Effect.provideService(Path.Path, path),627- Effect.provideService(ProcessRunner.ProcessRunner, runner),628- Effect.ignore,629- );626+ // Until the hub is in runningRef, nothing else stops it.627+ const [axExists, cliExists] = yield* Effect.gen(function* () {
juliusmarminge merged this4b5c604
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed