@@ -58,6 +58,20 @@ const DAEMON_POLL_MS = 100;
5858const HUB_RESTART_STABLE_UPTIME_MS = 60_000 ;
5959const HUB_RESTART_MAX_DELAY_MS = 30_000 ;
6060
61+ /**
62+ * Written beside the agent-device state so a server that dies without running
63+ * its finalizers (SIGKILL, dev-runner restarts) does not leave a hub bound to
64+ * a loopback port forever. The next start reads it, kills only a process that
65+ * is still that hub, and replaces the file.
66+ */
67+ const HubStateFile = Schema . Struct ( {
68+ pid : Schema . Int ,
69+ port : Schema . Int ,
70+ entryPath : Schema . String ,
71+ } ) ;
72+ const decodeHubStateFile = Schema . decodeUnknownEffect ( Schema . fromJsonString ( HubStateFile ) ) ;
73+ const encodeHubStateFile = Schema . encodeUnknownEffect ( Schema . fromJsonString ( HubStateFile ) ) ;
74+
6175const AgentDeviceDaemonFile = Schema . Struct ( {
6276 httpPort : Schema . Int ,
6377 token : Schema . String ,
@@ -135,9 +149,75 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
135149 const stopHub = ( hub : HubProcess | undefined ) =>
136150 hub ? Scope . close ( hub . scope , Exit . void ) . pipe ( Effect . ignore ) : Effect . void ;
137151
152+ const hubStatePath = ( ) => path . join ( agentDeviceStateDir ( path , config . stateDir ) , "hub.json" ) ;
153+
154+ const isProcessAlive = ( pid : number ) =>
155+ Effect . sync ( ( ) => {
156+ try {
157+ process . kill ( pid , 0 ) ;
158+ return true ;
159+ } catch {
160+ return false ;
161+ }
162+ } ) ;
163+
164+ /**
165+ * A hub left behind by a previous server is identified by pid plus the
166+ * command line's entry path, so a recycled pid belonging to something else
167+ * is never touched.
168+ */
169+ const reapStaleHub = Effect . gen ( function * ( ) {
170+ const previous = yield * fs
171+ . readFileString ( hubStatePath ( ) )
172+ . pipe ( Effect . flatMap ( decodeHubStateFile ) , Effect . option ) ;
173+ if ( previous . _tag === "None" ) return ;
174+ const alive = yield * isProcessAlive ( previous . value . pid ) ;
175+ if ( alive ) {
176+ const commandLine = yield * runner
177+ . run ( {
178+ command : "ps" ,
179+ args : [ "-o" , "command=" , "-p" , String ( previous . value . pid ) ] ,
180+ timeout : Duration . seconds ( 5 ) ,
181+ timeoutBehavior : "timedOutResult" ,
182+ } )
183+ . pipe (
184+ Effect . map ( ( result ) => result . stdout ) ,
185+ Effect . orElseSucceed ( ( ) => "" ) ,
186+ ) ;
187+ if ( commandLine . includes ( previous . value . entryPath ) ) {
188+ yield * Effect . logWarning ( "Stopping a device hub left behind by a previous server" , {
189+ pid : previous . value . pid ,
190+ port : previous . value . port ,
191+ } ) ;
192+ yield * Effect . sync ( ( ) => {
193+ try {
194+ process . kill ( previous . value . pid , "SIGTERM" ) ;
195+ } catch {
196+ // Already gone.
197+ }
198+ } ) ;
199+ }
200+ }
201+ yield * fs . remove ( hubStatePath ( ) , { force : true } ) . pipe ( Effect . ignore ) ;
202+ } ) . pipe ( Effect . catchCause ( ( ) => Effect . void ) ) ;
203+
204+ const recordHub = ( hub : HubProcess , tools : DeviceToolchainPaths ) =>
205+ encodeHubStateFile ( {
206+ pid : Number ( hub . child . pid ) ,
207+ port : Number ( new URL ( hub . origin ) . port ) ,
208+ entryPath : tools . hub . entryPath ,
209+ } ) . pipe (
210+ Effect . flatMap ( ( json ) => fs . writeFileString ( hubStatePath ( ) , json ) ) ,
211+ Effect . ignore ,
212+ ) ;
213+
138214 const spawnHub = Effect . fn ( "LocalDeviceHost.spawnHub" ) ( function * (
139215 tools : DeviceToolchainPaths ,
140216 ) : Effect . fn . Return < HubProcess , DeviceHostError > {
217+ yield * reapStaleHub ;
218+ yield * fs
219+ . makeDirectory ( agentDeviceStateDir ( path , config . stateDir ) , { recursive : true } )
220+ . pipe ( Effect . ignore ) ;
141221 const port = yield * net . reserveLoopbackPort ( "127.0.0.1" ) . pipe (
142222 Effect . mapError (
143223 ( cause ) =>
@@ -203,6 +283,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
203283 Effect . provideService ( HttpClient . HttpClient , httpClient ) ,
204284 Effect . tapError ( ( ) => stopHub ( hub ) ) ,
205285 ) ;
286+ yield * recordHub ( hub , tools ) ;
206287 yield * Effect . logInfo ( "Device hub started" , { pid : Number ( child . pid ) , port } ) ;
207288 return hub ;
208289 } ) ;
@@ -399,6 +480,7 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
399480 Effect . gen ( function * ( ) {
400481 const running = yield * Ref . getAndSet ( runningRef , null ) ;
401482 yield * stopHub ( running ?. hub ) ;
483+ yield * fs . remove ( hubStatePath ( ) , { force : true } ) . pipe ( Effect . ignore ) ;
402484 yield * stopAgentDeviceDaemon ( toolsRef ) ;
403485 } ) ,
404486 ) ;
0 commit comments