From 19604e7f4716bede04ae51ed67abba51cef502cb Mon Sep 17 00:00:00 2001 From: -Astraia- <91442300+Doge2077@users.noreply.github.com> Date: Tue, 10 Mar 2026 18:52:25 +0800 Subject: [PATCH] fix: improve port detection in WSL (#1061) --- scripts/wait-for-port.sh | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/scripts/wait-for-port.sh b/scripts/wait-for-port.sh index 4afe99d..198c3d0 100755 --- a/scripts/wait-for-port.sh +++ b/scripts/wait-for-port.sh @@ -20,7 +20,34 @@ SERVICE="${3:-Service}" elapsed=0 interval=1 -while ! lsof -i :"$PORT" -sTCP:LISTEN -t >/dev/null 2>&1; do +is_port_listening() { + if command -v lsof >/dev/null 2>&1; then + if lsof -nP -iTCP:"$PORT" -sTCP:LISTEN -t >/dev/null 2>&1; then + return 0 + fi + fi + + if command -v ss >/dev/null 2>&1; then + if ss -ltn "( sport = :$PORT )" 2>/dev/null | tail -n +2 | grep -q .; then + return 0 + fi + fi + + if command -v netstat >/dev/null 2>&1; then + if netstat -ltn 2>/dev/null | awk '{print $4}' | grep -Eq "(^|[.:])${PORT}$"; then + return 0 + fi + fi + + if command -v timeout >/dev/null 2>&1; then + timeout 1 bash -c "exec 3<>/dev/tcp/127.0.0.1/$PORT" >/dev/null 2>&1 + return $? + fi + + return 1 +} + +while ! is_port_listening; do if [ "$elapsed" -ge "$TIMEOUT" ]; then echo "" echo "✗ $SERVICE failed to start on port $PORT after ${TIMEOUT}s"