Skip to content

Commit a2f492f

Browse files
committed
Harden battlesnake and robotrumble against timeouts
Closes #62
1 parent d761778 commit a2f492f

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

codeclash/games/battlesnake/battlesnake.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import random
3+
import subprocess
34
import time
45
from concurrent.futures import ThreadPoolExecutor, as_completed
56

@@ -58,10 +59,16 @@ def _run_single_simulation(self, player2port: dict[str, int], idx: int) -> str:
5859

5960
cmd = self.run_cmd_round + " " + " ".join(cmd_args) + f" -o {self.log_env / f'sim_{idx}.jsonl'}"
6061

61-
output = self.environment.execute(
62-
cmd,
63-
cwd=f"{self.environment.config.cwd}/game",
64-
)
62+
# https://github.com/emagedoc/CodeClash/issues/62 (timeouts)
63+
try:
64+
output = self.environment.execute(
65+
cmd,
66+
cwd=f"{self.environment.config.cwd}/game",
67+
timeout=120, # this should rarely ever reach this timeout
68+
)
69+
except subprocess.TimeoutError:
70+
self.logger.warning(f"Battlesnake simulation timed out: {cmd}")
71+
return ""
6572
if output["returncode"] != 0:
6673
self.logger.warning(
6774
f"Battlesnake simulation failed with exit code {output['returncode']}:\n{output['output']}"

codeclash/games/robotrumble/robotrumble.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import shlex
2+
import subprocess
23
from collections import Counter
34
from concurrent.futures import ThreadPoolExecutor, as_completed
45

@@ -22,7 +23,12 @@ def _run_single_simulation(self, agents: list[Player], idx: int) -> str:
2223
args = [f"/{agent.name}/robot.py" for agent in agents]
2324
cmd = f"{self.run_cmd_round} {shlex.join(args)} > {self.log_env / f'sim_{idx}.txt'}"
2425

25-
output = self.environment.execute(cmd)
26+
# https://github.com/emagedoc/CodeClash/issues/62 (timeouts)
27+
try:
28+
output = self.environment.execute(cmd, timeout=120)
29+
except subprocess.TimeoutError:
30+
self.logger.warning(f"RobotRumble simulation {idx} timed out: {cmd}")
31+
return ""
2632
if output["returncode"] != 0:
2733
self.logger.warning(
2834
f"RobotRumble simulation {idx} failed with exit code {output['returncode']}:\n{output['output']}"

0 commit comments

Comments
 (0)