Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(dev): free stale port 8234 before starting frontend (#62487)

Oliver Browne71b631a54305
diff --git a/bin/start-frontend b/bin/start-frontend
index b70f93d7dbaf..389b537b36f1 100755
--- a/bin/start-frontend
+++ b/bin/start-frontend
@@ -11,6 +11,44 @@ export DEBUG=0
# Auto-accept corepack download prompts (hangs in non-interactive contexts)
export COREPACK_ENABLE_DOWNLOAD_PROMPT=0
+# The Vite dev server listens on a fixed port the rest of the app expects.
+VITE_PORT=8234
+
+# Print the PID(s) currently listening on $VITE_PORT, or nothing if the port is
+# free (or no probing tool is available). Single source of truth so detection
+# stays consistent between the initial check and the post-kill poll.
+vite_port_listeners() {
+ if command -v lsof >/dev/null 2>&1; then
+ lsof -ti "tcp:$VITE_PORT" -sTCP:LISTEN 2>/dev/null
+ elif command -v fuser >/dev/null 2>&1; then
+ fuser "$VITE_PORT/tcp" 2>/dev/null
+ fi
+ # lsof/fuser exit nonzero when nothing matches; force success so the caller's
+ # `pids=$(...)` doesn't abort the script under `set -e` when the port is free.
+ return 0
+}
+
+# Vite isn't configured with strictPort, so if a stale process from a previous
+# session is still squatting on $VITE_PORT, Vite silently falls back to the next
+# free port — and the app, which hardcodes $VITE_PORT everywhere, fails to load.
+# Reclaim the port up front so we always come up where we're expected.
+free_vite_port() {
+ local pids
+ pids=$(vite_port_listeners)
+ [ -z "$pids" ] && return 0
+ echo "Port $VITE_PORT is in use by stale process(es): $pids — killing them"
+ # shellcheck disable=SC2086
+ kill -TERM $pids 2>/dev/null || true
+ for _ in 1 2 3 4 5 6 7 8 9 10; do
+ pids=$(vite_port_listeners)
+ [ -z "$pids" ] && return 0
+ sleep 0.3
+ done
+ # shellcheck disable=SC2086
+ kill -KILL $pids 2>/dev/null || true
+}
+free_vite_port
+
# Recursively collect every descendant PID of the given pid, deepest first.
# turbo's launcher re-parents the vite subtree into its own process group, so the
# sibling `kill -- -$$` process-group idiom (start-go-service/start-rust-service)