spacexerq преди 2 дни
родител
ревизия
d7b3884c59
променени са 1 файла, в които са добавени 22 реда и са изтрити 13 реда
  1. 22 13
      apps/gui/src/tabs/scanner_tab.py

+ 22 - 13
apps/gui/src/tabs/scanner_tab.py

@@ -109,24 +109,33 @@ class _SpectrometerRestartWorker(QThread):
                 capture_output=True,
             )
 
-            # 2. Kill Django runserver on port 1818 (find PID via netstat)
+            # 2. Kill Django runserver on port 1818.
+            # Use netstat (always in System32, like taskkill) instead of
+            # PowerShell — its directory is a separate PATH entry that is
+            # often missing. Match the listening socket by foreign address
+            # "0.0.0.0:0"/"[::]:0" so we don't depend on the (localised)
+            # state column text.
             self.log.emit("Stopping Django runserver…")
             try:
                 r = subprocess.run(
-                    ["powershell", "-Command",
-                     "Get-NetTCPConnection -LocalPort 1818 -State Listen "
-                     "-ErrorAction SilentlyContinue | "
-                     "Select-Object -ExpandProperty OwningProcess"],
+                    ["netstat", "-ano", "-p", "TCP"],
                     capture_output=True, text=True, timeout=10,
                 )
-                for pid_str in r.stdout.strip().splitlines():
-                    pid_str = pid_str.strip()
-                    if pid_str.isdigit():
-                        subprocess.run(
-                            ["taskkill", "/f", "/pid", pid_str],
-                            capture_output=True,
-                        )
-                        self.log.emit(f"  Killed PID {pid_str}")
+                pids = set()
+                for line in r.stdout.splitlines():
+                    parts = line.split()
+                    # Proto  Local:1818  Foreign(0.0.0.0:0)  State  PID
+                    if (len(parts) >= 5 and parts[0] == "TCP"
+                            and parts[1].endswith(":1818")
+                            and parts[2] in ("0.0.0.0:0", "[::]:0")
+                            and parts[-1].isdigit()):
+                        pids.add(parts[-1])
+                for pid_str in pids:
+                    subprocess.run(
+                        ["taskkill", "/f", "/pid", pid_str],
+                        capture_output=True,
+                    )
+                    self.log.emit(f"  Killed PID {pid_str}")
             except Exception as e:
                 self.log.emit(f"  (port kill skipped: {e})")