Skip to content

Commit 3e4fc73

Browse files
committed
fix(scripts): wait for port to be ready in launch script
1 parent 2764640 commit 3e4fc73

3 files changed

Lines changed: 60 additions & 12 deletions

File tree

scripts/balatro-linux.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ def find_proton(steam_path: Path) -> Path | None:
5151
return None
5252

5353

54+
def wait_for_port(host: str, port: int, timeout: float = 30.0) -> bool:
55+
"""Wait for port to be ready to accept connections."""
56+
import socket
57+
58+
start = time.time()
59+
while time.time() - start < timeout:
60+
try:
61+
with socket.create_connection((host, port), timeout=1):
62+
return True
63+
except (ConnectionRefusedError, OSError):
64+
time.sleep(0.5)
65+
return False
66+
67+
5468
def kill_port(port: int):
5569
"""Kill processes using the specified port."""
5670
print(f"Killing processes on port {port}...")
@@ -156,10 +170,12 @@ def start(args):
156170
stderr=subprocess.STDOUT,
157171
)
158172

159-
# Wait and verify process started
160-
time.sleep(3)
161-
if process.poll() is not None:
162-
print(f"ERROR: Balatro failed to start. Check {log_file}")
173+
# Wait for port to be ready
174+
print(f"Waiting for port {args.port} to be ready...")
175+
if not wait_for_port(args.host, args.port, timeout=30):
176+
print(f"ERROR: Port {args.port} not ready after 30s. Check {log_file}")
177+
if process.poll() is not None:
178+
print("Balatro process has exited.")
163179
sys.exit(1)
164180

165181
print("Balatro started successfully!")

scripts/balatro-macos.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@
1515
LOGS_DIR = Path("logs")
1616

1717

18+
def wait_for_port(host: str, port: int, timeout: float = 30.0) -> bool:
19+
"""Wait for port to be ready to accept connections."""
20+
import socket
21+
22+
start = time.time()
23+
while time.time() - start < timeout:
24+
try:
25+
with socket.create_connection((host, port), timeout=1):
26+
return True
27+
except (ConnectionRefusedError, OSError):
28+
time.sleep(0.5)
29+
return False
30+
31+
1832
def kill_port(port: int):
1933
"""Kill processes using the specified port."""
2034
print(f"Killing processes on port {port}...")
@@ -90,10 +104,12 @@ def start(args):
90104
stderr=subprocess.STDOUT,
91105
)
92106

93-
# Wait and verify process started
94-
time.sleep(3)
95-
if process.poll() is not None:
96-
print(f"ERROR: Balatro failed to start. Check {log_file}")
107+
# Wait for port to be ready
108+
print(f"Waiting for port {args.port} to be ready...")
109+
if not wait_for_port(args.host, args.port, timeout=30):
110+
print(f"ERROR: Port {args.port} not ready after 30s. Check {log_file}")
111+
if process.poll() is not None:
112+
print("Balatro process has exited.")
97113
sys.exit(1)
98114

99115
print("Balatro started successfully!")

scripts/balatro-windows.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@
1818
LOGS_DIR = Path("logs")
1919

2020

21+
def wait_for_port(host: str, port: int, timeout: float = 30.0) -> bool:
22+
"""Wait for port to be ready to accept connections."""
23+
import socket
24+
25+
start = time.time()
26+
while time.time() - start < timeout:
27+
try:
28+
with socket.create_connection((host, port), timeout=1):
29+
return True
30+
except (ConnectionRefusedError, OSError):
31+
time.sleep(0.5)
32+
return False
33+
34+
2135
def find_game_path() -> Path | None:
2236
"""Find the Balatro installation path."""
2337
for path in STEAM_PATHS:
@@ -133,10 +147,12 @@ def start(args):
133147
creationflags=subprocess.CREATE_NO_WINDOW,
134148
)
135149

136-
# Wait and verify process started
137-
time.sleep(3)
138-
if process.poll() is not None:
139-
print(f"ERROR: Balatro failed to start. Check {log_file}")
150+
# Wait for port to be ready
151+
print(f"Waiting for port {args.port} to be ready...")
152+
if not wait_for_port(args.host, args.port, timeout=30):
153+
print(f"ERROR: Port {args.port} not ready after 30s. Check {log_file}")
154+
if process.poll() is not None:
155+
print("Balatro process has exited.")
140156
sys.exit(1)
141157

142158
print("Balatro started successfully!")

0 commit comments

Comments
 (0)