@@ -12,6 +12,7 @@ import {
1212} from "../../daemon/systemd.js" ;
1313import { callGatewayCli } from "../../gateway/call.js" ;
1414import { probeGateway } from "../../gateway/probe.js" ;
15+ import { readActiveGatewayLockPort } from "../../infra/gateway-lock.js" ;
1516import {
1617 findVerifiedGatewayListenerPidsOnPortSync ,
1718 formatGatewayPidList ,
@@ -334,9 +335,14 @@ export async function runDaemonRestart(opts: DaemonLifecycleOptions = {}): Promi
334335 let restartedWithoutServiceManager = false ;
335336 const restartIntent = resolveGatewayRestartIntentOptions ( opts ) ;
336337 const configuredPort = await resolveExplicitGatewayConfigPort ( ) ;
337- const restartPort =
338+ const managedRestartPort =
338339 configuredPort ??
339340 ( await resolveGatewayLifecyclePort ( service ) . catch ( ( ) => resolveGatewayPortFallback ( ) ) ) ;
341+ // An unmanaged run loop keeps its lock port across in-process restarts, even
342+ // when config changes underneath it. Use that port for both the signal and
343+ // health proof or a valid CLI/env override looks like a failed restart.
344+ const unmanagedPort =
345+ ( await readActiveGatewayLockPort ( ) . catch ( ( ) => undefined ) ) ?? managedRestartPort ;
340346 const restartHealthAttempts = postRestartHealthAttempts ( ) ;
341347 const restartWaitMs = restartHealthAttempts * POST_RESTART_HEALTH_DELAY_MS ;
342348 const restartWaitSeconds = Math . round ( restartWaitMs / 1000 ) ;
@@ -369,7 +375,7 @@ export async function runDaemonRestart(opts: DaemonLifecycleOptions = {}): Promi
369375 return recovered ;
370376 }
371377 }
372- const handled = await restartGatewayWithoutServiceManager ( restartPort , restartIntent ) ;
378+ const handled = await restartGatewayWithoutServiceManager ( unmanagedPort , restartIntent ) ;
373379 if ( handled ) {
374380 restartedWithoutServiceManager = true ;
375381 return handled ;
@@ -380,7 +386,7 @@ export async function runDaemonRestart(opts: DaemonLifecycleOptions = {}): Promi
380386 if ( restartedWithoutServiceManager ) {
381387 // SIGUSR1 restarts have no service-manager state to watch; use listener health only.
382388 const health = await waitForGatewayHealthyListener ( {
383- port : restartPort ,
389+ port : unmanagedPort ,
384390 attempts : restartHealthAttempts ,
385391 delayMs : POST_RESTART_HEALTH_DELAY_MS ,
386392 } ) ;
@@ -389,7 +395,7 @@ export async function runDaemonRestart(opts: DaemonLifecycleOptions = {}): Promi
389395 }
390396
391397 const diagnostics = renderGatewayPortHealthDiagnostics ( health ) ;
392- const timeoutLine = `Timed out after ${ restartWaitSeconds } s waiting for gateway port ${ restartPort } to become healthy.` ;
398+ const timeoutLine = `Timed out after ${ restartWaitSeconds } s waiting for gateway port ${ unmanagedPort } to become healthy.` ;
393399 if ( ! jsonOutput ) {
394400 defaultRuntime . log ( theme . warn ( timeoutLine ) ) ;
395401 for ( const line of diagnostics ) {
@@ -409,7 +415,7 @@ export async function runDaemonRestart(opts: DaemonLifecycleOptions = {}): Promi
409415
410416 let health = await waitForGatewayHealthyRestart ( {
411417 service,
412- port : restartPort ,
418+ port : managedRestartPort ,
413419 attempts : restartHealthAttempts ,
414420 delayMs : POST_RESTART_HEALTH_DELAY_MS ,
415421 includeUnknownListenersAsStale : process . platform === "win32" ,
@@ -432,7 +438,7 @@ export async function runDaemonRestart(opts: DaemonLifecycleOptions = {}): Promi
432438 }
433439 health = await waitForGatewayHealthyRestart ( {
434440 service,
435- port : restartPort ,
441+ port : managedRestartPort ,
436442 attempts : restartHealthAttempts ,
437443 delayMs : POST_RESTART_HEALTH_DELAY_MS ,
438444 includeUnknownListenersAsStale : process . platform === "win32" ,
@@ -446,12 +452,12 @@ export async function runDaemonRestart(opts: DaemonLifecycleOptions = {}): Promi
446452 const diagnostics = renderRestartDiagnostics ( health ) ;
447453 const failure = formatRestartFailure ( {
448454 health,
449- port : restartPort ,
455+ port : managedRestartPort ,
450456 timeoutSeconds : restartWaitSeconds ,
451457 } ) ;
452458 const runningNoPortLine =
453459 health . runtime . status === "running" && health . portUsage . status === "free"
454- ? `Gateway process is running but port ${ restartPort } is still free (startup hang/crash loop or very slow VM startup).`
460+ ? `Gateway process is running but port ${ managedRestartPort } is still free (startup hang/crash loop or very slow VM startup).`
455461 : null ;
456462 if ( ! jsonOutput ) {
457463 defaultRuntime . log ( theme . warn ( failure . statusLine ) ) ;
0 commit comments