From 84e55b939be81ce46a1c3380d8374f1d4910e24f Mon Sep 17 00:00:00 2001 From: Yshmeel Date: Sun, 22 Dec 2024 23:59:33 +0500 Subject: [PATCH] Update connect-to-vpn extension - fix spawning zombie process while updating services status in background - Initial commit --- extensions/connect-to-vpn/CHANGELOG.md | 4 +++ extensions/connect-to-vpn/package.json | 3 +- extensions/connect-to-vpn/src/menu-bar.tsx | 4 +-- .../connect-to-vpn/src/network-services.ts | 31 +++++++------------ 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/extensions/connect-to-vpn/CHANGELOG.md b/extensions/connect-to-vpn/CHANGELOG.md index 0f0e02f8078c5..7bd6d1fd240c5 100644 --- a/extensions/connect-to-vpn/CHANGELOG.md +++ b/extensions/connect-to-vpn/CHANGELOG.md @@ -1,5 +1,9 @@ # Connect to VPN Changelog +## [Bug fix] - 2024-12-22 + +- Fixed spawning zombie process while updating services status in background + ## [New Additions] - 2024-11-13 - Menu bar icon updates in the background diff --git a/extensions/connect-to-vpn/package.json b/extensions/connect-to-vpn/package.json index c820268db0e02..81279607c195d 100644 --- a/extensions/connect-to-vpn/package.json +++ b/extensions/connect-to-vpn/package.json @@ -8,7 +8,8 @@ "contributors": [ "sato11", "stonerl", - "vyacheslav" + "vyacheslav", + "yshmeel" ], "license": "MIT", "commands": [ diff --git a/extensions/connect-to-vpn/src/menu-bar.tsx b/extensions/connect-to-vpn/src/menu-bar.tsx index 34a8f0c8bfbe8..ee76b7e65ac81 100644 --- a/extensions/connect-to-vpn/src/menu-bar.tsx +++ b/extensions/connect-to-vpn/src/menu-bar.tsx @@ -17,10 +17,10 @@ export default function Command() { const isChecking = useRef(false); useEffect(() => { - if (environment.launchType === LaunchType.Background) { + if (environment.launchType === LaunchType.Background && !isLoading) { startRefresh(); } - }, [environment.launchType, favoriteServices, otherServices, invalidServices]); + }, [environment.launchType, favoriteServices.length, otherServices.length, invalidServices.length]); const startRefresh = async () => { if (!isChecking.current) { diff --git a/extensions/connect-to-vpn/src/network-services.ts b/extensions/connect-to-vpn/src/network-services.ts index e6e24702baa24..1c5eb8dc8b4d5 100644 --- a/extensions/connect-to-vpn/src/network-services.ts +++ b/extensions/connect-to-vpn/src/network-services.ts @@ -1,6 +1,7 @@ import { useState, useEffect, useMemo } from "react"; import { exec } from "child_process"; import { Icon, LocalStorage, Toast, getPreferenceValues, showToast } from "@raycast/api"; +import { execSync } from "node:child_process"; type Preferences = { hideInvalidDevices: boolean; @@ -70,6 +71,8 @@ export function useNetworkServices() { ...currentServices, [service.id]: { ...service, status }, })); + + return status; } catch (err) { console.error(`Error fetching service status for ${service.name}:`, err); setError(err as Error); @@ -338,27 +341,15 @@ const saveFavoriteOrder = async (order: Record) => { const execPromise = (command: string): Promise => new Promise((resolve, reject) => { - const child = exec(command, (err, stdout, stderr) => { - if (err) { - console.error(`Error executing command: ${command}`, err); - reject(err); - } else if (stderr) { - console.warn(`Command stderr: ${stderr}`); - resolve(stdout.trim()); - } else { - resolve(stdout.trim()); - } - }); - - // Ensure the child process is cleaned up - child.on("exit", (code) => { - console.log(`Command exited with code: ${code}`); - }); + try { + const child = execSync(command); + const result = child.toString(); - child.on("error", (err) => { - console.error(`Failed to start command: ${command}`, err); - reject(err); - }); + resolve(result.trim()); + } catch (e) { + console.error(`Command ${command} thrown an error`, e); + reject(e); + } }); const listNetworkServiceOrder = (): Promise => execPromise("/usr/sbin/networksetup -listnetworkserviceorder");