diff --git a/src/main/orcad/orcad-managed-idle-exit-host.ts b/src/main/orcad/orcad-managed-idle-exit-host.ts index 9700765063b..cf8c9eb7608 100644 --- a/src/main/orcad/orcad-managed-idle-exit-host.ts +++ b/src/main/orcad/orcad-managed-idle-exit-host.ts @@ -1,5 +1,5 @@ import { getAppEnvironment } from '../../shared/app-environment' -import type { RuntimeRpcClientActivity } from '../runtime/rpc/server/runtime-rpc-shutdown' +import type { RuntimeRpcClientActivity } from '../runtime/rpc/server/runtime-rpc' import type { OrcadIdleExitEvidence } from './orcad-idle-exit-monitor' import { activationFenceExists, diff --git a/src/main/orcad/orcad-managed-idle-exit.ts b/src/main/orcad/orcad-managed-idle-exit.ts index f655d746ce5..cc77791741b 100644 --- a/src/main/orcad/orcad-managed-idle-exit.ts +++ b/src/main/orcad/orcad-managed-idle-exit.ts @@ -3,7 +3,7 @@ import { stat } from 'node:fs/promises' import { join } from 'node:path' import { RELAY_INSTALL_LOCK_NAME } from '../../shared/relay-install-lock-name' import { hasErrorCode } from '../daemon/endpoint/identity/daemon-process-inspection' -import type { RuntimeRpcClientActivity } from '../runtime/rpc/server/runtime-rpc-shutdown' +import type { RuntimeRpcClientActivity } from '../runtime/rpc/server/runtime-rpc' import { ORCAD_MANAGED_ACTIVATION_ROOT_ENV } from '../../shared/orcad-managed-launch-env' import { ORCAD_IDLE_EXIT_TIMEOUT_MS, diff --git a/src/main/runtime/rpc/server/runtime-metadata.ts b/src/main/runtime/rpc/server/runtime-metadata.ts index d5aecee20c0..5aae7afe52b 100644 --- a/src/main/runtime/rpc/server/runtime-metadata.ts +++ b/src/main/runtime/rpc/server/runtime-metadata.ts @@ -5,7 +5,7 @@ import { writeSecureJsonFile } from '../../../../shared/secure-storage/secure-fi export function writeRuntimeMetadata(userDataPath: string, metadata: RuntimeMetadata): void { const metadataPath = getRuntimeMetadataPath(userDataPath) - writeMetadataFile(metadataPath, metadata) + writeSecureJsonFile(metadataPath, metadata) } export function readRuntimeMetadata(userDataPath: string): RuntimeMetadata | null { @@ -55,7 +55,3 @@ export function clearRuntimeMetadataIfOwned( } clearRuntimeMetadata(userDataPath) } - -function writeMetadataFile(path: string, metadata: RuntimeMetadata): void { - writeSecureJsonFile(path, metadata) -} diff --git a/src/main/runtime/rpc/server/runtime-rpc-shutdown.ts b/src/main/runtime/rpc/server/runtime-rpc-shutdown.ts deleted file mode 100644 index 67bb8d5bc2a..00000000000 --- a/src/main/runtime/rpc/server/runtime-rpc-shutdown.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { RuntimeRpcMobilePairing } from './runtime-rpc-mobile-pairing' -import { publishHostDescriptor } from '../../hosts/platform-probe/host-descriptor' - -export type RuntimeRpcClientActivity = { - openConnections: number - requestsInFlight: number - lastRequestAt: number -} - -export class RuntimeRpcShutdown extends RuntimeRpcMobilePairing { - /** Why: test-only seam — runs one ownership check instead of waiting out the poll interval. */ - checkRuntimeMetadataOwnership(): Promise { - return this.metadataOwnershipWatch?.check() ?? Promise.resolve() - } - - /** What a host's idle exit reads to know whether any client is still using this server. */ - readClientActivity(): RuntimeRpcClientActivity { - return { - openConnections: this.mobileSocketWiring?.connectionCount ?? 0, - requestsInFlight: this.clientRequestsInFlight, - lastRequestAt: this.lastClientRequestAt - } - } - - async stop(): Promise { - // Fence new exposure and await pending rebind before snapshotting transports, or late wide listeners can leak into cleared arrays (STA-2370). - this.stopping = true - const pendingExposure = this.networkExposurePromise - if (pendingExposure) { - await pendingExposure.catch(() => { - // Failed widening already logged/recovered; await only its transport-array mutations. - }) - } - const transports = this.activeTransports - this.activeTransports = [] - this.transports = [] - this.metadataOwnershipWatch?.stop() - this.metadataOwnershipWatch = null - if (this.hostDescriptor) { - publishHostDescriptor(this.runtime.getRuntimeId(), null) - } - this.mobileSocketWiring?.dispose() - this.mobileSocketWiring = null - this.detachPairedServerRows?.() - this.detachPairedServerRows = null - this.detachWebSocketWiring = null - const stopResults = await Promise.allSettled( - transports.map(async (transport) => transport.stop()) - ) - // Relay input is fenced before quit; direct auth may still update lastSeen while transports close. - this.deviceRegistry?.flushPendingLastSeen() - const failedStop = stopResults.find((result) => result.status === 'rejected') - if (failedStop?.status === 'rejected') { - throw failedStop.reason - } - // Shared userData may describe another live runtime; do not erase its metadata. - } -} diff --git a/src/main/runtime/rpc/server/runtime-rpc.ts b/src/main/runtime/rpc/server/runtime-rpc.ts index 40911b9896a..628d7a20455 100644 --- a/src/main/runtime/rpc/server/runtime-rpc.ts +++ b/src/main/runtime/rpc/server/runtime-rpc.ts @@ -1,9 +1,64 @@ // Why: the single security boundary for the bundled CLI — auth-token enforcement, metadata publication, transport orchestration. -import { RuntimeRpcShutdown } from './runtime-rpc-shutdown' +import { RuntimeRpcMobilePairing } from './runtime-rpc-mobile-pairing' +import { publishHostDescriptor } from '../../hosts/platform-probe/host-descriptor' import type { OrcaRuntimeRpcServerOptions } from './runtime-rpc-pairing-types' -export class OrcaRuntimeRpcServer extends RuntimeRpcShutdown { +export type RuntimeRpcClientActivity = { + openConnections: number + requestsInFlight: number + lastRequestAt: number +} + +export class OrcaRuntimeRpcServer extends RuntimeRpcMobilePairing { constructor(options: OrcaRuntimeRpcServerOptions) { super(options) } + + /** Why: test-only seam — runs one ownership check instead of waiting out the poll interval. */ + checkRuntimeMetadataOwnership(): Promise { + return this.metadataOwnershipWatch?.check() ?? Promise.resolve() + } + + /** What a host's idle exit reads to know whether any client is still using this server. */ + readClientActivity(): RuntimeRpcClientActivity { + return { + openConnections: this.mobileSocketWiring?.connectionCount ?? 0, + requestsInFlight: this.clientRequestsInFlight, + lastRequestAt: this.lastClientRequestAt + } + } + + async stop(): Promise { + // Fence new exposure and await pending rebind before snapshotting transports, or late wide listeners can leak into cleared arrays (STA-2370). + this.stopping = true + const pendingExposure = this.networkExposurePromise + if (pendingExposure) { + await pendingExposure.catch(() => { + // Failed widening already logged/recovered; await only its transport-array mutations. + }) + } + const transports = this.activeTransports + this.activeTransports = [] + this.transports = [] + this.metadataOwnershipWatch?.stop() + this.metadataOwnershipWatch = null + if (this.hostDescriptor) { + publishHostDescriptor(this.runtime.getRuntimeId(), null) + } + this.mobileSocketWiring?.dispose() + this.mobileSocketWiring = null + this.detachPairedServerRows?.() + this.detachPairedServerRows = null + this.detachWebSocketWiring = null + const stopResults = await Promise.allSettled( + transports.map(async (transport) => transport.stop()) + ) + // Relay input is fenced before quit; direct auth may still update lastSeen while transports close. + this.deviceRegistry?.flushPendingLastSeen() + const failedStop = stopResults.find((result) => result.status === 'rejected') + if (failedStop?.status === 'rejected') { + throw failedStop.reason + } + // Shared userData may describe another live runtime; do not erase its metadata. + } }