fix: acknowledge fresh supervisor restart before CLI polling

This commit is contained in:
abaishu0730 committed 2026-10-19 08:59:08 +08:00
1 parent a6b8b53b8d
commit 098fec1007
2 files changed
+18 -2

No files matched your search

+4 -2
View File
@@ -29,9 +29,11 @@ class ServiceSupervisor {
let request;try{request=JSON.parse(raw.split('\n')[0]);if(!request||typeof request!=='object'||Array.isArray(request))throw Error('invalid object');}catch{socket.end('{"status":400,"error":"请求无效。"}\n');return;}
if(!sameToken(request.token,this.store.token)){socket.end('{"status":403,"error":"控制令牌无效。"}\n');return;}
if(!['status','stop','restart'].includes(request.action)||Object.keys(request).some(k=>!['action','token'].includes(k))){socket.end('{"status":400,"error":"操作无效。"}\n');return;}
// restart() publishes acceptance synchronously before its first await, so
// this reply and subsequent polls cannot expose the previous failed attempt.
if(request.action==='restart')this.restart().catch(()=>{});
socket.end(JSON.stringify({state:this.snapshot(),ok:true})+'\n');
if(request.action==='stop')setImmediate(()=>this.stop().catch(()=>{}));
if(request.action==='restart')setImmediate(()=>this.restart().catch(()=>{}));
});
}
async launch(){
@@ -76,7 +78,7 @@ class ServiceSupervisor {
let timer;await Promise.race([closed,new Promise((_,reject)=>{timer=setTimeout(()=>reject(Error('无法确认业务进程已停止。')),8000);})]).finally(()=>clearTimeout(timer));
}
async restart(){
if(this.stopping||this.manualRestart)return;this.manualRestart=true;clearTimeout(this.restartTimer);this.store.event('manual_restart');
if(this.stopping||this.manualRestart)return;this.manualRestart=true;clearTimeout(this.restartTimer);this.update({status:'starting',reason:'manual_restart',nextRestartAt:null});this.store.event('manual_restart');
try{await this.endChild();this.child=null;this.update({restartCount:this.state.restartCount+1,reason:'manual_restart'});this.failures=0;this.restarts=[];await this.launch();}
catch(cause){this.update({status:'failed',childPid:this.child?.pid||null,nextRestartAt:null,reason:cause.code==='EADDRINUSE'?'port_conflict':cause.code==='EINVALIDPORTS'?'config_invalid':'restart_failed'});this.store.event('restart_failed',{reason:this.state.reason});throw cause;}
finally{this.manualRestart=false;}