Skip to content

Self-improving AI Employees for everyone

fix(cli): stop reported bindable ports as in use and skipped the dev backend port

trohitg0200ef6aabce
diff --git a/cli/commands/stop.py b/cli/commands/stop.py
index 518a4760..55040a49 100644
--- a/cli/commands/stop.py
+++ b/cli/commands/stop.py
@@ -13,6 +13,7 @@ import typer
from cli._common import free_all_ports, preflight
from cli.colors import console
+from cli.config import load_dev_overrides
from cli.platform_ import platform_name
from cli.ports import (
kill_by_pattern,
@@ -21,6 +22,9 @@ from cli.ports import (
def stop_command() -> None:
+ # Same port layout ``company dev`` frees (client 5678 + backend 5679);
+ # production binds only 5678, which that set already covers.
+ load_dev_overrides()
cfg, root = preflight()
console.print()
diff --git a/cli/ports.py b/cli/ports.py
index 381d5d45..b948c406 100644
--- a/cli/ports.py
+++ b/cli/ports.py
@@ -63,18 +63,28 @@ def _ancestor_pids() -> set[int]:
def find_pids_by_port(port: int) -> set[int]:
- """Find PIDs listening on ``port`` via psutil's native APIs."""
+ """Find PIDs with a LISTENING socket on ``port`` via psutil's native APIs.
+
+ Only listeners block a fresh ``bind()``. Half-closed connections left
+ by a browser tab after the server dies (``CLOSE_WAIT`` on a dead PID)
+ must not count, or ``company stop`` reports a bindable port as in use.
+ """
pids: set[int] = set()
try:
for conn in psutil.net_connections(kind="inet"):
- if conn.laddr and conn.laddr.port == port and conn.pid:
+ if (
+ conn.laddr
+ and conn.laddr.port == port
+ and conn.pid
+ and conn.status == psutil.CONN_LISTEN
+ ):
pids.add(conn.pid)
except psutil.AccessDenied:
# macOS: net_connections() requires root, fall back to lsof.
if sys.platform == "darwin":
try:
output = subprocess.check_output(
- ["lsof", "-ti", f":{port}"],
+ ["lsof", "-ti", f"tcp:{port}", "-sTCP:LISTEN"],
text=True,
stderr=subprocess.DEVNULL,
)
diff --git a/cli/tests/test_ports.py b/cli/tests/test_ports.py
index 7412d369..ee587a6d 100644
--- a/cli/tests/test_ports.py
+++ b/cli/tests/test_ports.py
@@ -31,6 +31,21 @@ def test_kill_pid_force_kills_on_timeout():
proc.kill.assert_called_once()
+def test_find_pids_by_port_counts_only_listeners():
+ """A dead server's half-closed sockets must not make a bindable port
+ look occupied (the 'Port still in use' false positive on stop)."""
+ from types import SimpleNamespace as NS
+
+ conns = [
+ NS(laddr=NS(port=5678), pid=111, status=psutil.CONN_LISTEN),
+ NS(laddr=NS(port=5678), pid=222, status=psutil.CONN_CLOSE_WAIT),
+ NS(laddr=NS(port=5678), pid=333, status=psutil.CONN_ESTABLISHED),
+ NS(laddr=NS(port=9999), pid=444, status=psutil.CONN_LISTEN),
+ ]
+ with patch.object(psutil, "net_connections", return_value=conns):
+ assert ports.find_pids_by_port(5678) == {111}
+
+
def test_kill_port_excludes_self():
"""The function must never kill its own PID."""
import os
diff --git a/docs-internal/performance.md b/docs-internal/performance.md
index 53513002..d308398c 100644
--- a/docs-internal/performance.md
+++ b/docs-internal/performance.md
@@ -34,6 +34,26 @@ I/O plus the items in "Open follow-ups" — not bytecode compilation,
which now happens at build time (`[tool.uv] compile-bytecode = true`
+ the `-O`-less compileall step below).
+### Re-measured after the pnpm → bun migration (2026-09-02)
+
+Warm boots on the same Windows machine, taken from the supervisor's
+timestamped console output right after the switch to [email protected]:
+
+| Metric | Pre-bun (table above) | Post-bun |
+|---|---|---|
+| `company start` — Application startup complete | 2.90 s | ~3.6 s |
+| `company start` — `ready on port 5678` | 3.17 s | 4.56 s |
+| `company dev` — Vite `ready in` | — | 8.4 s |
+| `company dev` — backend Application startup complete | — | 12.4 s |
+
+No change attributable to bun: it only installs the workspace and
+launches the `company` CLI; everything after spawn (uvicorn imports,
+lifespan, Vite on Node 22) is byte-identical. The sub-second prod delta
+is machine-load variance, and the dev-mode rows were never benchmarked
+before (Vite and uvicorn compete for I/O during a dev boot, so they are
+not comparable to the prod numbers). The headline baselines above still
+stand.
+
## Optimisation history
In chronological order. Each row links to the commit and the