mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2026-02-10 19:50:28 +08:00
fix: resolve restartCore race condition and add retry mechanism
This commit is contained in:
parent
c7190a311e
commit
3f7b85afc1
@ -134,7 +134,7 @@ interface CoreConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 准备核心配置
|
// 准备核心配置
|
||||||
async function prepareCore(detached: boolean): Promise<CoreConfig> {
|
async function prepareCore(detached: boolean, skipStop = false): Promise<CoreConfig> {
|
||||||
const [appConfig, mihomoConfig] = await Promise.all([
|
const [appConfig, mihomoConfig] = await Promise.all([
|
||||||
getAppConfig(),
|
getAppConfig(),
|
||||||
getControledMihomoConfig()
|
getControledMihomoConfig()
|
||||||
@ -168,7 +168,9 @@ async function prepareCore(detached: boolean): Promise<CoreConfig> {
|
|||||||
// generateProfile 返回实际使用的 current
|
// generateProfile 返回实际使用的 current
|
||||||
const current = await generateProfile()
|
const current = await generateProfile()
|
||||||
await checkProfile(current, core, diffWorkDir)
|
await checkProfile(current, core, diffWorkDir)
|
||||||
await stopCore()
|
if (!skipStop) {
|
||||||
|
await stopCore()
|
||||||
|
}
|
||||||
await cleanupSocketFile()
|
await cleanupSocketFile()
|
||||||
|
|
||||||
// 设置 DNS
|
// 设置 DNS
|
||||||
@ -320,8 +322,8 @@ function setupCoreListeners(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 启动核心
|
// 启动核心
|
||||||
export async function startCore(detached = false): Promise<Promise<void>[]> {
|
export async function startCore(detached = false, skipStop = false): Promise<Promise<void>[]> {
|
||||||
const config = await prepareCore(detached)
|
const config = await prepareCore(detached, skipStop)
|
||||||
child = spawnCoreProcess(config)
|
child = spawnCoreProcess(config)
|
||||||
|
|
||||||
if (detached) {
|
if (detached) {
|
||||||
@ -374,11 +376,34 @@ export async function restartCore(): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
isRestarting = true
|
isRestarting = true
|
||||||
|
let retryCount = 0
|
||||||
|
const maxRetries = 3
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await startCore()
|
// 先显式停止核心,确保状态干净
|
||||||
} catch (e) {
|
await stopCore()
|
||||||
managerLogger.error('restart core failed', e)
|
|
||||||
throw e
|
// 尝试启动核心,失败时重试
|
||||||
|
while (retryCount < maxRetries) {
|
||||||
|
try {
|
||||||
|
// skipStop=true 因为我们已经在上面停止了核心
|
||||||
|
await startCore(false, true)
|
||||||
|
return // 成功启动,退出函数
|
||||||
|
} catch (e) {
|
||||||
|
retryCount++
|
||||||
|
managerLogger.error(`restart core failed (attempt ${retryCount}/${maxRetries})`, e)
|
||||||
|
|
||||||
|
if (retryCount >= maxRetries) {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重试前等待一段时间
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 1000 * retryCount))
|
||||||
|
// 确保清理干净再重试
|
||||||
|
await stopCore()
|
||||||
|
await cleanupSocketFile()
|
||||||
|
}
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
isRestarting = false
|
isRestarting = false
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user