Skip to content

Commit 5bdb321

Browse files
authored
Race conditions handle (#3)
* Try to fix race conditions * timeout server2 termination
1 parent 0a16802 commit 5bdb321

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

scripts_of_tribute/game.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def _cleanup_processes(self):
115115

116116
self.processes.clear()
117117

118-
def _download_game_runner(self, version="v1.0.0"):
118+
def _download_game_runner(self, version=None):
119119
system = platform.system()
120120
if system == "Windows":
121121
zip_name = f"GameRunner-win-x64.zip"
@@ -125,6 +125,13 @@ def _download_game_runner(self, version="v1.0.0"):
125125
zip_name = f"GameRunner-osx-x64.zip"
126126
else:
127127
raise NotImplementedError(f"Unsupported platform: {system}")
128+
129+
if version is None:
130+
print("Fetching latest GameRunner version...")
131+
response = requests.get("https://api.github.com/repos/ScriptsOfTribute/ScriptsOfTribute-Core/releases/latest")
132+
response.raise_for_status()
133+
version = response.json()["tag_name"]
134+
print(f"Latest version: {version}")
128135

129136
url = f"https://github.com/ScriptsOfTribute/ScriptsOfTribute-Core/releases/download/{version}/{zip_name}"
130137

@@ -135,9 +142,8 @@ def _download_game_runner(self, version="v1.0.0"):
135142

136143
print(f"Downloading GameRunner from {url}...")
137144
response = requests.get(url)
138-
response.raise_for_status() # Raise an error for bad status codes
145+
response.raise_for_status()
139146

140-
# Save the zip file
141147
with open(zip_path, "wb") as f:
142148
f.write(response.content)
143149

scripts_of_tribute/runner.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ def run_game_runner(path: Path, bot1: str, bot2: str, runs=1, threads=1, enable_
2121
cwd=str(path.parent)
2222
)
2323
print(f"{result.stdout}")
24+
if result.stderr:
25+
print(result.stderr)
2426
except subprocess.CalledProcessError as e:
2527
print(f"Error running GameRunner:\n{e.stderr}")
2628
raise RuntimeError(e)

scripts_of_tribute/server.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
from typing import Tuple
23
import grpc
34
from concurrent import futures
@@ -43,10 +44,17 @@ def GameEnd(self, request, context):
4344
return main_pb2.Empty()
4445

4546
def CloseServer(self, request, context):
46-
print("Received CloseServer request. Shutting down server...")
47-
self.server_instance.bot_disconnected()
47+
print(f"Received CloseServer request from {self.ai.bot_name}. Shutting down server...")
4848
context.set_code(grpc.StatusCode.OK)
4949
context.set_details(f"Bot {self.ai.bot_name}'s connection closed.")
50+
def delayed_shutdown():
51+
time.sleep(0.1)
52+
self.server_instance.bot_disconnected()
53+
print(f"Bot {self.ai.bot_name}'s connection closed.")
54+
55+
import threading
56+
threading.Thread(target=delayed_shutdown, daemon=True).start()
57+
5058
return main_pb2.Empty()
5159

5260

@@ -55,6 +63,7 @@ class Server:
5563
def __init__(self):
5664
self.active_bots = 0
5765
self.server = None
66+
5867
def add_bot(self):
5968
self.active_bots += 1
6069

@@ -99,6 +108,12 @@ def run_grpc_server(
99108
server2.server.start()
100109

101110
if bot1 is not None:
102-
server1.server.wait_for_termination()
111+
try:
112+
server1.server.wait_for_termination(timeout=2)
113+
except grpc.FutureTimeoutError:
114+
print("Server didn't terminate cleanly in time.")
103115
if bot2 is not None:
104-
server2.server.wait_for_termination()
116+
try:
117+
server2.server.wait_for_termination(timeout=2)
118+
except grpc.FutureTimeoutError:
119+
print("Server didn't terminate cleanly in time.")

0 commit comments

Comments
 (0)