refactor: give runtime RPC entry its shutdown implementation

This commit is contained in:
Neil
2026-11-08 22:49:04 -08:00
parent 7e60fae4f9
commit 23a3698bdf
5 changed files with 60 additions and 67 deletions
@@ -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,
+1 -1
View File
@@ -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,
@@ -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)
}
@@ -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<void> {
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<void> {
// 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.
}
}
+57 -2
View File
@@ -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<void> {
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<void> {
// 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.
}
}