Skip to content

Commit ec42b46

Browse files
EgorBoCopilot
andauthored
Update src/coreclr/scripts/superpmi_aspnet2.py
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 743b38d commit ec42b46

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/coreclr/scripts/superpmi_aspnet2.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,20 @@ def kill_port(port):
5555
if pid:
5656
subprocess.run(["kill", "-9", pid], check=False)
5757
else: # Linux
58-
# Linux: fuser is the most direct "vanilla" tool
59-
subprocess.run(["fuser", "-k", f"{port}/tcp"], check=True)
58+
# Linux: Prefer fuser if available; fall back to lsof
59+
if shutil.which("fuser") is not None:
60+
subprocess.run(["fuser", "-k", f"{port}/tcp"], check=True)
61+
elif shutil.which("lsof") is not None:
62+
pids_output = subprocess.check_output(
63+
["lsof", "-t", f"-iTCP:{port}", "-sTCP:LISTEN"]
64+
).decode().strip()
65+
if pids_output:
66+
for pid in pids_output.splitlines():
67+
os.system(f"kill -9 {pid}")
68+
else:
69+
raise FileNotFoundError(
70+
"Neither 'fuser' nor 'lsof' is available to kill processes by port."
71+
)
6072
print(f"Port {port} cleared.")
6173
except Exception:
6274
print(f"Port {port} is already free or permission denied.")

0 commit comments

Comments
 (0)