fix(service): surface a service core that stopped for good

When the service watchdog gave up or the core vanished, the owner
monitor marked the core stopped without telling the user, and on macOS
left the system proxy on the dead mixed port (#8029).

- On SameOwnerFailure, clear the proxy through the helper while the
  session is still ours; displaced or unreachable owners still leave the
  machine-wide setting alone.
- Retain the stop as a core failure with its own message, recorded
  under the lifecycle lock and shown now or on next focus; a running
  core resets the notice dedup.
- Log each watchdog restart the service reports, since launchd discards
  the service's own log.
This commit is contained in:
wonfen
2026-09-25 00:06:59 +08:00
parent 1f140dfd3a
commit daae17a40c
24 changed files with 174 additions and 45 deletions
@@ -1,6 +1,7 @@
import { beforeEach, expect, it, vi } from 'vitest'
import {
type RunState,
getCoreStartupError,
takeDnsOverrideNotice,
takeServiceRepairNotice,
@@ -53,7 +54,10 @@ beforeEach(async () => {
})
it('does not replay a recovered startup error after WebView recreation', async () => {
vi.mocked(getCoreStartupError).mockResolvedValue('startup failed')
vi.mocked(getCoreStartupError).mockResolvedValue({
kind: 'startFailed',
detail: 'startup failed',
})
handleNoticeMessage('core_start::error', '', (key) => key, vi.fn())
await new Promise((resolve) => setTimeout(resolve, 0))
expect(showNotice.error).toHaveBeenCalledOnce()
@@ -75,7 +79,10 @@ it.each([
const detail = 'startup failed'
nativeWindow.isVisible.mockResolvedValue(visible)
nativeWindow.isMinimized.mockResolvedValue(minimized)
vi.mocked(getCoreStartupError).mockResolvedValue(detail)
vi.mocked(getCoreStartupError).mockResolvedValue({
kind: 'startFailed',
detail,
})
useLayoutEvents(([status, message]) => {
handleNoticeMessage(status, message, (key) => key, vi.fn())
})
@@ -99,7 +106,10 @@ it.each([
it('delivers an early core startup failure once after listeners mount', async () => {
const detail = 'could not open core execution mutex: access denied'
vi.mocked(getCoreStartupError).mockResolvedValue(detail)
vi.mocked(getCoreStartupError).mockResolvedValue({
kind: 'startFailed',
detail,
})
useLayoutEvents(([status, message]) => {
handleNoticeMessage(status, message, (key) => key, vi.fn())
})
@@ -113,6 +123,24 @@ it('delivers an early core startup failure once after listeners mount', async ()
)
})
it('shows the same core failure again after the core has started in between', async () => {
const { useLayoutEvents } = await import('./use-layout-events')
const failure = { kind: 'serviceCoreStopped', detail: 'stopped' } as const
vi.mocked(getCoreStartupError).mockResolvedValue(failure)
useLayoutEvents(([status, message]) => {
handleNoticeMessage(status, message, (key) => key, vi.fn())
})
const [handlers] = vi.mocked(subscribeVergeEvents).mock.calls[0]
handlers['verge://notice-message']?.(['core_start::error', ''])
await new Promise((resolve) => setTimeout(resolve, 0))
// The core started and stopped again before the window handled either event.
handlers['verge://run-state-changed']?.({ mode: 'Service' } as RunState)
handlers['verge://notice-message']?.(['core_start::error', ''])
await new Promise((resolve) => setTimeout(resolve, 0))
expect(showNotice.error).toHaveBeenCalledTimes(2)
})
it('drains a DNS notice after listeners mount without duplicating its live event', async () => {
vi.mocked(takeDnsOverrideNotice)
.mockResolvedValueOnce(true)
@@ -7,6 +7,8 @@ import type { RunState } from '@/services/cmds'
import { subscribeVergeEvents } from '@/services/events'
import { revalidateQueries, setCacheData } from '@/services/query-client'
import { forgetShownStartupError } from '../utils/notification-handlers'
export const useLayoutEvents = (
handleNotice: (payload: [string, string]) => void,
) => {
@@ -58,6 +60,7 @@ export const useLayoutEvents = (
// Transitions carry the full run-state snapshot, so write it directly to cache.
'verge://run-state-changed': (payload) => {
void setCacheData<RunState>(runStateQueryKey, payload)
if (payload.mode !== 'NotRunning') forgetShownStartupError()
},
'verge://notice-message': handleNotice,
},
@@ -15,8 +15,17 @@ type NavigateFunction = (path: string, options?: any) => void
type TranslateFunction = (key: string) => string
let shownStartupError: string | null = null
// Reads can settle out of order; only the newest may act, and a reset counts as one.
let startupErrorReads = 0
let settledStartupErrorRead = 0
let shownOwnerNotice: number | null = null
/** A running core has resolved every failure shown before it. */
export const forgetShownStartupError = () => {
shownStartupError = null
settledStartupErrorRead = ++startupErrorReads
}
export const handleNoticeMessage = (
status: string,
msg: string,
@@ -39,9 +48,12 @@ export const handleNoticeMessage = (
},
'set_config::error': () => showNotice.error(msg),
'core_start::error': () => {
const read = ++startupErrorReads
void getCoreStartupError()
.then(async (error) => {
if (!error) {
.then(async (failure) => {
if (read < settledStartupErrorRead) return
settledStartupErrorRead = read
if (!failure) {
shownStartupError = null
return
}
@@ -50,11 +62,19 @@ export const handleNoticeMessage = (
window.isVisible(),
window.isMinimized(),
])
if (visible && !minimized && shownStartupError !== error) {
shownStartupError = error
const shown = `${failure.kind}:${failure.detail}`
if (
read === settledStartupErrorRead &&
visible &&
!minimized &&
shownStartupError !== shown
) {
shownStartupError = shown
showNotice.error(
'settings.feedback.errors.clash.startFailed',
error,
failure.kind === 'serviceCoreStopped'
? 'settings.feedback.errors.clash.serviceCoreStopped'
: 'settings.feedback.errors.clash.startFailed',
failure.detail,
)
}
})