@@ -4,7 +4,7 @@ import { contenderPool, spawnServiceContender } from "../service-contender.js"
44import { defaultEnsureTiming , ensureTiming , type EnsureTiming } from "../service-timing.js"
55import { matchesVersion } from "../service-version.js"
66import { PtyHandoff } from "../pty-handoff.js"
7- import { decide , fallback , headers , probeResult , same } from "../service-probe.js"
7+ import { decide , fallback , headers , marker , probeResult , same } from "../service-probe.js"
88
99export * from "../service.js"
1010export { headers }
@@ -69,10 +69,12 @@ export const ensure = Effect.fn("service.ensure")(function* (options: EnsureOpti
6969 } )
7070 } )
7171 const found = yield * Effect . gen ( function * ( ) {
72- const registration = yield * registered ( options . file , timing . requestTimeout )
72+ const registrationFile = options . file ?? fallback ( )
73+ const registrationPresent = ( yield * read ( registrationFile ) ) !== undefined
74+ const registration = yield * registered ( registrationFile , timing . requestTimeout , marker ( options . file ) )
7375 const info = registration . info
7476 const service = registration . service
75- if ( registration . timedOut && info !== undefined ) {
77+ if ( registrationPresent && registration . timedOut && info !== undefined ) {
7678 timeouts = {
7779 info,
7880 count : timeouts !== undefined && same ( timeouts . info , info ) ? timeouts . count + 1 : 1 ,
@@ -105,9 +107,10 @@ export const ensure = Effect.fn("service.ensure")(function* (options: EnsureOpti
105107 return Option . none ( )
106108 }
107109
110+ if ( info !== undefined && processAlive ( info . pid ) ) return Option . none ( )
108111 const failed = pool . reap ( )
109112 if ( failed !== undefined ) return yield * Effect . fail ( failed )
110- if ( pool . shouldRecruit ( info !== undefined ) ) {
113+ if ( pool . shouldRecruit ( info !== undefined && ! processAlive ( info . pid ) ) ) {
111114 yield * announce ( "missing" )
112115 pool . add ( yield * spawnContender )
113116 }
@@ -157,12 +160,25 @@ const read = Effect.fnUntraced(function* (file?: string) {
157160 return yield * decode ( text . value ) . pipe ( Effect . option , Effect . map ( Option . getOrUndefined ) )
158161} )
159162
160- const registered = Effect . fnUntraced ( function * ( file ?: string , timeout ?: number ) {
163+ const registered = Effect . fnUntraced ( function * ( file ?: string , timeout ?: number , markerFile ?: string ) {
161164 const info = yield * read ( file )
165+ if ( info === undefined && markerFile !== undefined ) {
166+ const markerInfo = yield * read ( markerFile )
167+ if ( markerInfo !== undefined ) return { info : markerInfo , ...( yield * Effect . promise ( ( ) => probeResult ( markerInfo , timeout ) ) ) }
168+ }
162169 if ( info === undefined ) return { info : undefined , service : undefined , timedOut : false }
163170 return { info, ...( yield * Effect . promise ( ( ) => probeResult ( info , timeout ) ) ) }
164171} )
165172
173+ function processAlive ( pid : number ) {
174+ try {
175+ process . kill ( pid , 0 )
176+ return true
177+ } catch {
178+ return false
179+ }
180+ }
181+
166182// 50ms cadence bounded at ~5s, shared by stop escalation and each ensure
167183// discovery window.
168184const poll = ( timing : EnsureTiming ) =>
0 commit comments