From df4d09167d2ed04a76bfc6e3ddd704fac6d03fa6 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:58:48 +0800 Subject: [PATCH] fix(sysproxy): read the live verge config in the toggle drain loop The drain loop captured the render-time verge snapshot, so a config revalidation landing mid-loop left later iterations deciding connection closing and rollback with stale values. The confirmed-state seed and the in-loop auto-close check now read through a latest ref. --- src/hooks/use-system-proxy-state.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/hooks/use-system-proxy-state.ts b/src/hooks/use-system-proxy-state.ts index 75ab9c6bc..9c169ac5a 100644 --- a/src/hooks/use-system-proxy-state.ts +++ b/src/hooks/use-system-proxy-state.ts @@ -48,10 +48,14 @@ export const useSystemProxyState = () => { // Coalesce rapid clicks so only the latest requested state is applied. const pendingRef = useRef(null) const busyRef = useRef(false) + // The drain loop outlives the render it started in; reading the config + // through a ref keeps mid-loop revalidations visible to later iterations. + const vergeRef = useRef(verge) + vergeRef.current = verge const toggleSystemProxy = async (enabled: boolean) => { // Roll failed optimistic writes back to the latest confirmed state. - let confirmed = verge?.enable_system_proxy ?? false + let confirmed = vergeRef.current?.enable_system_proxy ?? false mutateVerge((prev) => prev ? { ...prev, enable_system_proxy: enabled } : prev, ) @@ -69,7 +73,7 @@ export const useSystemProxyState = () => { errorNotice: false, }) confirmed = target - if (!target && verge?.auto_close_connection) { + if (!target && vergeRef.current?.auto_close_connection) { await mutate(() => closeAllConnections(), { id: 'close-all-connections', errorNotice: false,