tc043 commited on
Commit
ef6005f
·
1 Parent(s): 4505a0c

fix: resolve port 7860 conflicts from zombie processes during restart

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py CHANGED
@@ -1,10 +1,54 @@
1
  import asyncio
2
  import json
3
  import os
 
4
  from pathlib import Path
5
  from dotenv import load_dotenv
6
  load_dotenv()
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  import gradio as gr
9
 
10
  # --- Gradio 5.x BugFix Monkeypatch ---
 
1
  import asyncio
2
  import json
3
  import os
4
+ import signal
5
  from pathlib import Path
6
  from dotenv import load_dotenv
7
  load_dotenv()
8
 
9
+ def kill_process_on_port(port):
10
+ try:
11
+ # Hex representation of port (e.g. 7860 -> 1EB4)
12
+ port_hex = f"{port:04X}"
13
+ inodes = []
14
+ if os.path.exists("/proc/net/tcp"):
15
+ with open("/proc/net/tcp", "r") as f:
16
+ for line in f:
17
+ parts = line.split()
18
+ if len(parts) > 9:
19
+ local_addr = parts[1]
20
+ local_port = local_addr.split(":")[-1]
21
+ if local_port == port_hex:
22
+ inodes.append(parts[9])
23
+
24
+ if inodes:
25
+ my_pid = os.getpid()
26
+ for pid_str in os.listdir("/proc"):
27
+ if not pid_str.isdigit():
28
+ continue
29
+ pid = int(pid_str)
30
+ if pid == my_pid:
31
+ continue
32
+ fd_dir = f"/proc/{pid_str}/fd"
33
+ if not os.path.exists(fd_dir):
34
+ continue
35
+ try:
36
+ for fd in os.listdir(fd_dir):
37
+ link = os.readlink(f"{fd_dir}/{fd}")
38
+ for inode in inodes:
39
+ if f"socket:[{inode}]" in link:
40
+ print(f"Self-Healing: Killing zombie process {pid} using port {port}", flush=True)
41
+ os.kill(pid, signal.SIGKILL)
42
+ break
43
+ except Exception:
44
+ continue
45
+ except Exception as e:
46
+ print(f"Error checking/killing process on port {port}: {e}", flush=True)
47
+
48
+ # Free port 7860 before trying to launch Gradio
49
+ kill_process_on_port(7860)
50
+
51
+
52
  import gradio as gr
53
 
54
  # --- Gradio 5.x BugFix Monkeypatch ---